def setUp(self): self.parser = get_argparser() self.default_creds = os.path.join(os.path.expanduser('~'), '.medium_creds.ini')
def main(): ## PARSE ARGS parser = get_argparser() args = parser.parse_args() # TODO - make this a plain path; not a directory argument command = args.command ## EXECUTE COMMANDS if command == 'fetch_cookies': from medium_stats.cookie_fetcher import MediumAuthorizer email, password = unpack_email_pwd(args) me = MediumAuthorizer(args.u, email, password) me.sign_in() me.save_cookies(args.creds) print(section_break) elif command in ['scrape_user', 'scrape_publication']: args = parse_scraper_args(args, parser) if args.creds: cfg = MediumConfigHelper(args.creds, args.u) sid, uid = cfg.sid, cfg.uid else: sid, uid = args.sid, args.uid modes = list(args.mode) get_folders = lambda x: [x[m]['folder'] for m in modes] print('\nGetting Preliminary Data...', end='\n\n') if command == 'scrape_user': username = args.u sg = StatGrabberUser(username, sid, uid, args.start, args.end, already_utc=True) folders = get_folders(user_mode_attrs) sub_dir = create_directories(args.output_dir, sg.slug, folders) # get summary stats to derive article_ids and user creation_time data = sg.get_summary_stats() articles = sg.get_article_ids(data) if 'summary' in modes: write_stats(sg, data, 'summary', sg.now, sub_dir) else: url = args.s sg = StatGrabberPublication(url, sid, uid, args.start, args.end, already_utc=True) folders = get_folders(pub_mode_attrs) sub_dir = create_directories(args.output_dir, sg.slug, folders) data = sg.get_all_story_overview() articles = sg.get_article_ids(data) if 'story_overview' in modes: write_stats(sg, data, 'story_overview', sg.now, sub_dir) # go through remainder of modes remaining = [ m for m in modes if m not in ('summary', 'story_overview') ] for m in remaining: if m == 'events': data = get_stats(sg, m, sg.now) else: data = get_stats(sg, m, sg.now, articles) write_stats(sg, data, m, sg.now, sub_dir) print('All done!')