# Path: newsApp/dbhelper.py
# def getDbTable(connectionString):
#     """
#     Get a dynamo db table object using connection string
#     """
# 
#     connectionParams = parseConnectionString(connectionString)
# 
#     return Table(
#             connectionParams['name'],
#             connection = getDbConnection(connectionParams))
# 
# def decryptSecret(value, encryptionKey):
#     """
#     Helper function to decrypt a secret stored in database.
#     """
# 
#     cipherText = base64.b64decode(value)
#     aesCipher = AES.new(encryptionKey.encode('ascii'), AES.MODE_ECB)
#     decryptedBytes = aesCipher.decrypt(cipherText)
#     return decryptedBytes.decode('ascii').rstrip('{')
# 
# def encryptSecret(value, encryptionKey):
#     """
#     Helper function to encrypt a secret before storing in database.
#     """
# 
#     value = value + (16 - len(value) % 16) * '{'
#     aesCipher = AES.new(encryptionKey.encode('ascii'), AES.MODE_ECB)
#     cipherText = aesCipher.encrypt(value.encode('ascii'))
#     encryptedBytes = base64.b64encode(cipherText)
#     return encryptedBytes.decode('ascii')
# 
# Path: newsApp/notifierBase.py
# class NotifierBase:
#   def __init__(self):
#     self.domainName = os.environ['DOMAIN']
# 
#   def isNightTime(self, locale):
#     # for now we only have cities in india
#     india_tz = timezone('Asia/Kolkata')
#     hour = datetime.now(india_tz).hour
#     return hour >= 2 and hour < 9


import os
import logging
import time
import tweepy
from datetime import datetime
from pytz import timezone
from boto.dynamodb2.table import Table
from .dbhelper import getDbTable, decryptSecret, encryptSecret
from .notifierBase import NotifierBase



logger = logging.getLogger('notifierTwitter')

class NotifierTwitter(NotifierBase):
  def __init__(self):
    NotifierBase.__init__(self)
    self.tableConnString = os.environ['TWITTERHANDLESTABLE_CONNECTIONSTRING']
    self.encryptionKey = os.environ['TWITTERHANDLESTABLE_KEY']
    self.__table = None

  def __getTable(self):
    if not self.__table:
      self.__table = getDbTable(self.tableConnString)

    return self.__table

  def __getKeys(self, handle):
    table = self.__getTable()
    dbRows = list(table.query_2(handle__eq = handle))

    if not dbRows:
      return None

    dbRow = dbRows[0]
    return {
      'handle': dbRow['handle'],

# Path: newsApp/dbhelper.py
# def getDbTable(connectionString):
#     """
#     Get a dynamo db table object using connection string
#     """
# 
#     connectionParams = parseConnectionString(connectionString)
# 
#     return Table(
#             connectionParams['name'],
#             connection = getDbConnection(connectionParams))
# 
# def decryptSecret(value, encryptionKey):
#     """
#     Helper function to decrypt a secret stored in database.
#     """
# 
#     cipherText = base64.b64decode(value)
#     aesCipher = AES.new(encryptionKey.encode('ascii'), AES.MODE_ECB)
#     decryptedBytes = aesCipher.decrypt(cipherText)
#     return decryptedBytes.decode('ascii').rstrip('{')
# 
# def encryptSecret(value, encryptionKey):
#     """
#     Helper function to encrypt a secret before storing in database.
#     """
# 
#     value = value + (16 - len(value) % 16) * '{'
#     aesCipher = AES.new(encryptionKey.encode('ascii'), AES.MODE_ECB)
#     cipherText = aesCipher.encrypt(value.encode('ascii'))
#     encryptedBytes = base64.b64encode(cipherText)
#     return encryptedBytes.decode('ascii')
# 
# Path: newsApp/notifierBase.py
# class NotifierBase:
#   def __init__(self):
#     self.domainName = os.environ['DOMAIN']
# 
#   def isNightTime(self, locale):
#     # for now we only have cities in india
#     india_tz = timezone('Asia/Kolkata')
#     hour = datetime.now(india_tz).hour
#     return hour >= 2 and hour < 9


# Path: newsApp/notifierTwitter.py
import os
import logging
import time
import tweepy
from datetime import datetime
from pytz import timezone
from boto.dynamodb2.table import Table
from .dbhelper import getDbTable, decryptSecret, encryptSecret
from .notifierBase import NotifierBase




logger = logging.getLogger('notifierTwitter')

class NotifierTwitter(NotifierBase):
  def __init__(self):
    NotifierBase.__init__(self)
    self.tableConnString = os.environ['TWITTERHANDLESTABLE_CONNECTIONSTRING']
    self.encryptionKey = os.environ['TWITTERHANDLESTABLE_KEY']
    self.__table = None

  def __getTable(self):
    if not self.__table:
      self.__table = getDbTable(self.tableConnString)

    return self.__table

  def __getKeys(self, handle):
    table = self.__getTable()
    dbRows = list(table.query_2(handle__eq = handle))

    if not dbRows:
      return None

    dbRow = dbRows[0]
    return {
      'handle': dbRow['handle'],

      'consumerKey': decryptSecret(dbRow['consumerKey'], self.encryptionKey),