def __init__(self, CACHE_FILE): self.tz = timezone('US/Pacific') print(datetime.now(self.tz), " - Log: __init__") self.LAST = datetime(year=2017, month=1, day=17, tzinfo=self.tz) self.CACHE_FILE = CACHE_FILE self.reupload = True conf = ET.parse('res/OAuth.xml') confRoot = conf.getroot() self._consumer_key = confRoot[0].text self._consumer_key_secret = confRoot[1].text self._access_token = confRoot[2].text self._access_token_secret = confRoot[3].text auth = tweepy.OAuthHandler(self._consumer_key, self._consumer_key_secret) auth.set_access_token(self._access_token, self._access_token_secret) self.api = tweepy.API(auth) try: self.api.verify_credentials() except Exception as e: print(datetime.now(self.tz), " - Error: Twitter auth failed. Exception: ", e) exit(-1) # Exit code -1: Twitter auth error print(datetime.now(self.tz), " - Log: Auth succeeded") self.cache = ET.parse(self.CACHE_FILE)
def lambda_handler(event, context): time = datetime.now(timezone('US/Pacific')) callHour = time.hour callMin = time.minute timeRange = range( callMin - 4, callMin + 5) # Account for delays, which can apparently be in the minutes on AWS bot.main(0) if callHour % 2 == 0 and 0 in timeRange: bot.main(1) return {'statusCode': 200}
def main(op): """ Entry Point :param op: 0 for check. 1 for scheduled post :return: none """ tz = timezone('US/Pacific') BUCKET_NAME = 'jaimewherecache' FILE_NAME_REMOTE = 'cache.xml' FILE_NAME_LOCAL = '/tmp/cache.xml' s3 = boto3.resource('s3') try: s3.Bucket(BUCKET_NAME).download_file(FILE_NAME_REMOTE, FILE_NAME_LOCAL) except botocore.exceptions.ClientError as e: if e.response['Error']['Code'] == "404": print(" - Error: The s3 object does not exist") else: print( " - Error: s3 download failed for unknown reason. Exception: ", e) exit(-4) # Exit code -3: Couldn't download cache bot = Bot(FILE_NAME_LOCAL) if op == 0: # Check print(datetime.now(tz), " - Log: Started in op: 0") bot.check() elif op == 1: # Scheduled Post print(datetime.now(tz), " - Log: Started in op: 1") bot.post() else: print( datetime.now(tz), " - Error: Unrecognized arg. You should never see this. Called with ", type(op), ": ", op) bot.reupload = False exit(-2) # Exit code -2: Bad Arg if bot.reupload: try: s3.meta.client.upload_file(FILE_NAME_LOCAL, BUCKET_NAME, FILE_NAME_REMOTE) except Exception as e: print(" - Error: Reupload failed for unknown reason. Exception: ", e)
def get_datetime(cls, datetime): local_timezone = pytz.timezone('Europe/Ljubljana') return pytz.utc.localize(datetime).astimezone(local_timezone)