<s> import couchdb <EOL> import argparse <EOL> import requests <EOL> import logging <EOL> import os <EOL> import sys <EOL> import json <EOL> import re <EOL> import datetime <EOL> import calendar <EOL> from totalimpact import provider_batch_data <EOL> logging . basicConfig ( <EOL> stream = sys . stdout , <EOL> level = logging . DEBUG , <EOL> format = '<STR_LIT>' <EOL> ) <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> def last_minute_of_a_month ( year , month ) : <EOL> first_day = datetime . datetime ( year , month , <NUM_LIT:1> ) <EOL> number_of_days = calendar . monthrange ( year , month ) [ <NUM_LIT:1> ] <EOL> first_day_next_month = first_day + datetime . timedelta ( days = number_of_days ) <EOL> last_day_last_minute = first_day_next_month - datetime . timedelta ( microseconds = <NUM_LIT:1> ) <EOL> return last_day_last_minute <EOL> def build_batch_dict ( page , options_dict ) : <EOL> pmids = re . findall ( '<STR_LIT>' , page ) <EOL> year = int ( options_dict [ "<STR_LIT>" ] ) <EOL> month = int ( options_dict [ "<STR_LIT>" ] ) <EOL> min_date = datetime . datetime ( year , month , <NUM_LIT:1> ) <EOL> max_date = last_minute_of_a_month ( year , month ) <EOL> batch_dict = { <EOL> "<STR_LIT>" : "<STR_LIT>" . format ( ** options_dict ) , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : datetime . datetime . now ( ) . isoformat ( ) , <EOL> "<STR_LIT>" : min_date . isoformat ( ) , <EOL> "<STR_LIT>" : max_date . isoformat ( ) , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : pmids <EOL> } , <EOL> "<STR_LIT>" : page <EOL> } <EOL> return batch_dict <EOL> def get_pmc_stats_page ( options_dict ) : <EOL> if len ( str ( options_dict [ "<STR_LIT>" ] ) ) == <NUM_LIT:1> : <EOL> options_dict [ "<STR_LIT>" ] = "<STR_LIT:0>" + str ( options_dict [ "<STR_LIT>" ] ) <EOL> pmc_download_template = "<STR_LIT>" <EOL> url = pmc_download_template . format ( ** options_dict ) <EOL> print url <EOL> resp = requests . get ( url ) <EOL> page = resp . text <EOL> return page <EOL> def write_batch_dict ( batch_dict ) : <EOL> logger . info ( "<STR_LIT>" + os . getenv ( "<STR_LIT>" ) ) <EOL> new_object = provider_batch_data . create_objects_from_doc ( batch_dict ) <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> matches = provider_batch_data . ProviderBatchData . query . filter_by ( provider = "<STR_LIT>" ) . order_by ( "<STR_LIT>" ) . all ( ) <EOL> for match in matches : <EOL> print match <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> parser = argparse . ArgumentParser ( description = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , default = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , required = True ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , default = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , default = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , required = True ) <EOL> options_dict = vars ( parser . parse_args ( ) ) <EOL> print options_dict <EOL> page = get_pmc_stats_page ( options_dict ) <EOL> batch_dict = build_batch_dict ( page , options_dict ) <EOL> print ( json . dumps ( batch_dict , indent = <NUM_LIT:4> ) ) <EOL> if batch_dict [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> write_batch_dict ( batch_dict ) <EOL> else : <EOL> print "<STR_LIT>" </s>
