def time_on_page(post_id, subreddit_id, page_type, rank_limit=100, start_time=datetime.datetime.min, end_time=datetime.datetime.max): values = {"total_time": 0, "gap_size": None, "num_gaps": 0, "sum_gaps": 0} # connect to database. assuming this file is in a folder like app. so you have to do "../config" to get to config BASE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../") ENV = os.environ['CS_ENV'] db_session = DbEngine( os.path.join(BASE_DIR, "config") + "/{env}.json".format(env=ENV)).new_session() pages = [] if not subreddit_id: # then query FrontPage pages = db_session.query(FrontPage).filter( FrontPage.page_type == page_type.value, FrontPage.created_at >= start_time, FrontPage.created_at <= end_time) else: pages = db_session.query(SubredditPage).filter( SubredditPage.page_type == page_type.value, SubredditPage.created_at >= start_time, SubredditPage.created_at <= end_time, SubredditPage.subreddit_id == subreddit_id) rank_vectors = construct_rank_vectors(pages) if post_id not in rank_vectors: # post_id not present in this time interval return values values["gap_size"] = calculate_gap(rank_vectors) my_rank_vectors = rank_vectors[post_id] previous_time = None previous_rank = None for time in sorted(my_rank_vectors.keys()): current_rank = rank_vectors[post_id][time] if previous_time and current_rank <= rank_limit: time_delta = (time - previous_time).total_seconds() if time_delta <= values["gap_size"]: values["total_time"] += time_delta else: values["num_gaps"] += 1 values["sum_gaps"] += time_delta previous_time = time previous_rank = current_rank return values # test: #print(time_on_page("4rgka4", None, PageType.TOP, 15))
def time_on_page(post_id, subreddit_id, page_type, rank_limit=100, start_time=datetime.datetime.min, end_time=datetime.datetime.max): values = { "total_time": 0, "gap_size": None, "num_gaps": 0, "sum_gaps": 0 } # connect to database. assuming this file is in a folder like app. so you have to do "../config" to get to config BASE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../") ENV = os.environ['CS_ENV'] db_session = DbEngine(os.path.join(BASE_DIR, "config") + "/{env}.json".format(env=ENV)).new_session() pages = [] if not subreddit_id: # then query FrontPage pages = db_session.query(FrontPage).filter(FrontPage.page_type == page_type.value, FrontPage.created_at >= start_time, FrontPage.created_at <= end_time) else: pages = db_session.query(SubredditPage).filter(SubredditPage.page_type == page_type.value, SubredditPage.created_at >= start_time, SubredditPage.created_at <= end_time, SubredditPage.subreddit_id == subreddit_id) rank_vectors = construct_rank_vectors(pages) if post_id not in rank_vectors: # post_id not present in this time interval return values values["gap_size"] = calculate_gap(rank_vectors) my_rank_vectors = rank_vectors[post_id] previous_time = None previous_rank = None for time in sorted(my_rank_vectors.keys()): current_rank = rank_vectors[post_id][time] if previous_time and current_rank <= rank_limit: time_delta = (time - previous_time).total_seconds() if time_delta <= values["gap_size"]: values["total_time"] += time_delta else: values["num_gaps"] += 1 values["sum_gaps"] += time_delta previous_time = time previous_rank = current_rank return values # test: #print(time_on_page("4rgka4", None, PageType.TOP, 15))
class Connect: # this initializer accepts a database session # and if it doesn't exist, initializes one # this may become a pattern that we should break out # into its own class eventually def __init__(self, db_session = None, base_dir="", env=None): praw_patch = PrawPatch() if praw_patch.required: praw_patch.ensure_applied() self.base_dir = base_dir if(env): self.env = env else: self.env = ENV ## LOAD DATABASE OBJECT if db_session is None: ### LOAD SQLALCHEMY SESSION self.db_session = DbEngine(os.path.join(self.base_dir, "config","{env}.json".format(env=self.env))).new_session() else: self.db_session = db_session def connect(self, controller="Main"): r = None #Praw Connection Object handler = MultiprocessHandler() # Check the Database for a Stored Praw Key db_praw_id = PrawKey.get_praw_id(self.env, controller) pk = self.db_session.query(PrawKey).filter_by(id=db_praw_id).first() r = praw.Reddit(user_agent="Test version of CivilServant by u/natematias", handler=handler) access_information = {} if(pk is None): with open(os.path.join(self.base_dir, "config","access_information_{environment}.pickle".format(environment=self.env)), "rb") as fp: access_information = pickle.load(fp) else: access_information['access_token'] = pk.access_token access_information['refresh_token'] = pk.refresh_token access_information['scope'] = json.loads(pk.scope) r.set_access_credentials(**access_information) new_access_information = {} update_praw_key = False # SAVE OR UPDATE THE ACCESS TOKEN IF NECESSARY if(pk is None): pk = PrawKey(id=db_praw_id, access_token = r.access_token, refresh_token = r.refresh_token, scope = json.dumps(list(r._authentication)), authorized_username = r.user.json_dict['name'], authorized_user_id = r.user.json_dict['id']) self.db_session.add(pk) elif(access_information['access_token'] != r.access_token or access_information['refresh_token'] != r.refresh_token): pk.access_token = r.access_token pk.refresh_token = r.refresh_token pk.scope = json.dumps(list(r._authentication)) self.db_session.commit() return r
import os, sys BASE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../", "../") sys.path.append(BASE_DIR) import reddit.connection from app.models import PrawKey from utils.common import DbEngine from praw.handlers import MultiprocessHandler import praw import simplejson as json ENV = os.environ['CS_ENV'] db_session = DbEngine(os.path.join(BASE_DIR, "config") + "/{env}.json".format(env=ENV)).new_session() for pk in db_session.query(PrawKey).all(): handler = MultiprocessHandler() r = praw.Reddit(user_agent="Test version of CivilServant by u/natematias", handler=handler) access_information = {} access_information['access_token'] = pk.access_token access_information['refresh_token'] = pk.refresh_token access_information['scope'] = json.loads(pk.scope) r.set_access_credentials(**access_information) print("USER {0}, ID {1}".format(r.user.json_dict['name'], r.user.json_dict['id'])) pk.authorized_username = r.user.json_dict['name'] pk.authorized_user_id = r.user.json_dict['id'] db_session.commit()
"../") sys.path.append(BASE_DIR) import reddit.connection from app.models import PrawKey from utils.common import DbEngine from praw.handlers import MultiprocessHandler import praw import simplejson as json ENV = os.environ['CS_ENV'] db_session = DbEngine( os.path.join(BASE_DIR, "config") + "/{env}.json".format(env=ENV)).new_session() for pk in db_session.query(PrawKey).all(): handler = MultiprocessHandler() r = praw.Reddit(user_agent="Test version of CivilServant by u/natematias", handler=handler) access_information = {} access_information['access_token'] = pk.access_token access_information['refresh_token'] = pk.refresh_token access_information['scope'] = json.loads(pk.scope) r.set_access_credentials(**access_information) print("USER {0}, ID {1}".format(r.user.json_dict['name'], r.user.json_dict['id'])) pk.authorized_username = r.user.json_dict['name'] pk.authorized_user_id = r.user.json_dict['id']
class Connect: # this initializer accepts a database session # and if it doesn't exist, initializes one # this may become a pattern that we should break out # into its own class eventually def __init__(self, db_session=None, base_dir="", env=None): self.base_dir = base_dir if (env): self.env = env else: self.env = ENV ## LOAD DATABASE OBJECT if db_session is None: ### LOAD SQLALCHEMY SESSION self.db_session = DbEngine( os.path.join(self.base_dir, "config", "{env}.json".format(env=self.env))).new_session() else: self.db_session = db_session def connect(self, controller="Main"): r = None #Praw Connection Object handler = MultiprocessHandler() # Check the Database for a Stored Praw Key db_praw_id = PrawKey.get_praw_id(self.env, controller) pk = self.db_session.query(PrawKey).filter_by(id=db_praw_id).first() r = praw.Reddit( user_agent="Test version of CivilServant by u/natematias", handler=handler) access_information = {} if (pk is None): with open( os.path.join( self.base_dir, "config", "access_information_{environment}.pickle".format( environment=self.env)), "rb") as fp: access_information = pickle.load(fp) else: access_information['access_token'] = pk.access_token access_information['refresh_token'] = pk.refresh_token access_information['scope'] = json.loads(pk.scope) r.set_access_credentials(**access_information) new_access_information = {} update_praw_key = False # SAVE OR UPDATE THE ACCESS TOKEN IF NECESSARY if (pk is None): pk = PrawKey(id=db_praw_id, access_token=r.access_token, refresh_token=r.refresh_token, scope=json.dumps(list(r._authentication)), authorized_username=r.user.json_dict['name'], authorized_user_id=r.user.json_dict['id']) self.db_session.add(pk) elif (access_information['access_token'] != r.access_token or access_information['refresh_token'] != r.refresh_token): pk.access_token = r.access_token pk.refresh_token = r.refresh_token pk.scope = json.dumps(list(r._authentication)) self.db_session.commit() return r