# Path: newsApp/dbItem.py
# class DbItem:
#   """
#   Represents a generic item which can be added/updated/deleted/retrieved to a
#   database.
# 
#   Each dbItem consists of a unique identifier and a set of tags(which are simple key-value pairs).
#   """
# 
#   def __init__(self, id, tags=None):
#     """
#     Instantiates a new dbItem object.
# 
#     Requires a 'id' parameter which should be a simple representing a unique
#     identifier for the dbItem.
# 
#     Optionally accepts a 'tags' parameter which should be a dictionary of
#     key-value pairs. e.g. you can have a tag for the language of dbItem.
#     """
# 
#     self.id = id
#     self.tags = tags
# 
# Path: newsApp/webPageLoader.py
# def loadPageAndGetHtml(url):
#   pageHtml = ""
# 
#   try:
#     with warnings.catch_warnings():
#       #ignore PhantomJS deprecated warning.
#       warnings.simplefilter("ignore")
# 
#       driver = webdriver.PhantomJS(executable_path='node_modules/phantomjs-prebuilt/bin/phantomjs')
#       driver.implicitly_wait(30)
#       driver.set_page_load_timeout(30)
# 
#     try:
#       driver.get(url)
#       pageHtml = driver.page_source
#     finally:
#       try:
#         driver.service.process.send_signal(signal.SIGTERM)
#         driver.quit()
#       except:
#         pass
#   except:
#     logger.info("Could not load page with url %s through selenium", url)
#     pass
# 
#   if not pageHtml:
#     pageHtml = getHtmlStatic(url)
# 
#   return pageHtml
# 
# def getHtmlStatic(url, max_retries = 1):
#     nRetries = 0
#     headers = {'User-Agent': 'newsbot'}
#     while (True):
#         try:
#             return requests.get(url, headers = headers, timeout = 20).text
#         except Exception as e:
#             if (nRetries >= max_retries):
#                 raise e
#             else:
#                 time.sleep(5)
#                 nRetries = nRetries + 1


from .dbItem import DbItem
from .webPageLoader import loadPageAndGetHtml, getHtmlStatic
import time
import requests
  result = _openUrlWithRetries(url)
  return result.url

class Link(DbItem):
  """
  Represents a link to a webPage.

  Each wlink consists of a unique identifier(the url)
  and a set of tags(key-value pairs).
  """

  def __init__(self, id, tags=None):
    """
    Instantiates a link object representing a link to a web page.
    """

    DbItem.__init__(self, id, tags)

  def checkExistence(self):
    try:
      _openUrlWithRetries(self.id)
    except:
      return False

    return True

  def getFinalRedirect(self):
    return getIdentifierUrl(self.id)

  def getHtmlDynamic(self):

# Path: newsApp/dbItem.py
# class DbItem:
#   """
#   Represents a generic item which can be added/updated/deleted/retrieved to a
#   database.
# 
#   Each dbItem consists of a unique identifier and a set of tags(which are simple key-value pairs).
#   """
# 
#   def __init__(self, id, tags=None):
#     """
#     Instantiates a new dbItem object.
# 
#     Requires a 'id' parameter which should be a simple representing a unique
#     identifier for the dbItem.
# 
#     Optionally accepts a 'tags' parameter which should be a dictionary of
#     key-value pairs. e.g. you can have a tag for the language of dbItem.
#     """
# 
#     self.id = id
#     self.tags = tags
# 
# Path: newsApp/webPageLoader.py
# def loadPageAndGetHtml(url):
#   pageHtml = ""
# 
#   try:
#     with warnings.catch_warnings():
#       #ignore PhantomJS deprecated warning.
#       warnings.simplefilter("ignore")
# 
#       driver = webdriver.PhantomJS(executable_path='node_modules/phantomjs-prebuilt/bin/phantomjs')
#       driver.implicitly_wait(30)
#       driver.set_page_load_timeout(30)
# 
#     try:
#       driver.get(url)
#       pageHtml = driver.page_source
#     finally:
#       try:
#         driver.service.process.send_signal(signal.SIGTERM)
#         driver.quit()
#       except:
#         pass
#   except:
#     logger.info("Could not load page with url %s through selenium", url)
#     pass
# 
#   if not pageHtml:
#     pageHtml = getHtmlStatic(url)
# 
#   return pageHtml
# 
# def getHtmlStatic(url, max_retries = 1):
#     nRetries = 0
#     headers = {'User-Agent': 'newsbot'}
#     while (True):
#         try:
#             return requests.get(url, headers = headers, timeout = 20).text
#         except Exception as e:
#             if (nRetries >= max_retries):
#                 raise e
#             else:
#                 time.sleep(5)
#                 nRetries = nRetries + 1


# Path: newsApp/link.py
from .dbItem import DbItem
from .webPageLoader import loadPageAndGetHtml, getHtmlStatic
import time
import requests

  result = _openUrlWithRetries(url)
  return result.url

class Link(DbItem):
  """
  Represents a link to a webPage.

  Each wlink consists of a unique identifier(the url)
  and a set of tags(key-value pairs).
  """

  def __init__(self, id, tags=None):
    """
    Instantiates a link object representing a link to a web page.
    """

    DbItem.__init__(self, id, tags)

  def checkExistence(self):
    try:
      _openUrlWithRetries(self.id)
    except:
      return False

    return True

  def getFinalRedirect(self):
    return getIdentifierUrl(self.id)

  def getHtmlDynamic(self):

    return loadPageAndGetHtml(self.id)