import datetime as dt import sys import logging import os import tweepy print(tweepy.__version__) import NewsSocialSignaling import json import os class Helper: @staticmethod def is_linux(): return sys.platform == "linux" or sys.platform == "linux2" @staticmethod def initialize_logger(log_path): logger = logging.getLogger('news_social_signaling') logger.setLevel(logging.DEBUG) # create console handler and set level info handler = logging.StreamHandler() handler.setLevel(logging.INFO) logger.addHandler(handler) # create file handler handler = logging.FileHandler(log_path, mode='w') handler.setLevel(logging.DEBUG) logger.addHandler(handler) return logger #@staticmethod #def tweepy_api(auth_dic, wait_on_rate_limit): # # Instantiate API # auth = Helper.tweepy_auth(auth_dic) # auth.set_access_token(auth_dic['ACCESS_TOKEN'], auth_dic['ACCESS_TOKEN_SECRET']) # api = tweepy.API(auth, wait_on_rate_limit=wait_on_rate_limit, wait_on_rate_limit_notify=True) # return api #@staticmethod #def tweepy_auth(auth_dic): # consumer_key = auth_dic['CONSUMER_KEY'] # consumer_secret = auth_dic['CONSUMER_SECRET'] # auth = tweepy.OAuthHandler(consumer_key, consumer_secret) # return auth @staticmethod def month_diff(d1, d2): """ Returns d1 - d2 in months :param d1: end date :param d2: start date :return: int (months) """ return (d1.year - d2.year) * 12 + d1.month - d2.month @staticmethod def load_keys(): with open(NewsSocialSignaling.Config.key_pathApiV2, 'r') as f: keys = json.load(f) # set google credential path os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = NewsSocialSignaling.Config.google_credentials_path return keys @staticmethod def merge_pagination(master, using, page_ix): for d in (master, using): assert list(d.keys()) == ['data', 'includes', 'errors', 'meta'] assert isinstance(d['data'], list) == True assert isinstance(d['includes'], dict) == True assert isinstance(d['errors'], list) == True assert isinstance(d['meta'], dict) == True assert list(d['includes'].keys()) == ['tweets'] assert isinstance(d['includes']['tweets'], list) == True output = dict() output['data'] = master['data'] + using['data'] output['includes'] = dict() output['includes']['tweets'] = master['includes']['tweets'] + using['includes']['tweets'] output['errors'] = master['errors'] + using['errors'] output['meta'] = {} for key, value in master['meta'].items(): if not key[-1].isdigit(): output['meta'][key + str(page_ix)] = value else: output['meta'][key] = value for key, value in using['meta'].items(): output['meta'][key + str(page_ix + 1)] = value return output assert Helper.month_diff(dt.date.today(), dt.date(2020, 1, 1)) > 0