Esempio n. 1
0
def _check_credentials():
    """
    Check credentials and spawn server and browser if not
    """
    credentials = get_credentials()
    if not credentials or 'https://www.googleapis.com/auth/drive' not in credentials.config['google']['scope']:
        try:
            with open(os.devnull, 'w') as fnull:
                print 'Credentials were not found or permissions were not correct. Automatically opening a browser to authenticate with Google.'
                gunicorn = find_executable('gunicorn')
                process = subprocess.Popen([gunicorn, '-b', '0.0.0.0:8888', 'app:wsgi_app'], stdout=fnull, stderr=fnull, cwd=app_config.PROJECT_DIR)
                print 'Visit newsdev3:8888/oauth'
                # webbrowser.open_new('http://127.0.0.1:8888/oauth')
                print 'Waiting...'
                while not credentials:
                    try:
                        credentials = get_credentials()
                        sleep(1)
                    except ValueError:
                        continue
                print 'Successfully authenticated!'
                process.terminate()
        except KeyboardInterrupt:
            print '\nCtrl-c pressed. Later, skater!'
            exit()
def check_credentials():
    """
    Check credentials and spawn server and browser if not
    """
    credentials = get_credentials()
    if not credentials or "https://www.googleapis.com/auth/drive" not in credentials.config["google"]["scope"]:
        try:
            with open(os.devnull, "w") as fnull:
                print "Credentials were not found or permissions were not correct. Automatically opening a browser to authenticate with Google."
                gunicorn = find_executable("gunicorn")
                process = subprocess.Popen(
                    [gunicorn, "-b", "127.0.0.1:8888", "app:wsgi_app"], stdout=fnull, stderr=fnull
                )
                webbrowser.open_new("http://127.0.0.1:8888/oauth")
                print "Waiting..."
                while not credentials:
                    try:
                        credentials = get_credentials()
                        sleep(1)
                    except ValueError:
                        continue
                print "Successfully authenticated!"
                process.terminate()
        except KeyboardInterrupt:
            print "\nCtrl-c pressed. Later, skater!"
            exit()
Esempio n. 3
0
def check_credentials():
    """
    Check credentials and spawn server and browser if not
    """
    credentials = get_credentials()
    if not credentials:
        try:
            with open(os.devnull, 'w') as fnull:
                logger.info('Credentials were not found or permissions were not correct. Automatically opening a browser to authenticate with Google.')
                gunicorn = find_executable('gunicorn')
                process = subprocess.Popen([gunicorn, '-b', '127.0.0.1:8888', 'app:wsgi_app'], stdout=fnull, stderr=fnull)
                webbrowser.open_new('http://127.0.0.1:8888/oauth')
                logger.info('Waiting...')
                while not credentials:
                    try:
                        credentials = get_credentials()
                        sleep(1)
                    except ValueError:
                        continue
                logger.info('Successfully authenticated!')
                process.terminate()
        except KeyboardInterrupt:
            logger.info('\nCtrl-c pressed. Later, skater!')
            exit()
    return credentials
Esempio n. 4
0
def _check_credentials():
    """
    Check credentials and spawn server and browser if not
    """
    credentials = get_credentials()
    if not credentials or 'https://www.googleapis.com/auth/drive' not in credentials.config[
            'google']['scope']:
        try:
            with open(os.devnull, 'w') as fnull:
                print 'Credentials were not found or permissions were not correct. Automatically opening a browser to authenticate with Google.'
                gunicorn = find_executable('gunicorn')
                process = subprocess.Popen(
                    [gunicorn, '-b', '127.0.0.1:8888', 'app:wsgi_app'],
                    stdout=fnull,
                    stderr=fnull)
                webbrowser.open_new('http://127.0.0.1:8888/oauth')
                print 'Waiting...'
                while not credentials:
                    try:
                        credentials = get_credentials()
                        sleep(1)
                    except ValueError:
                        continue
                print 'Successfully authenticated!'
                process.terminate()
        except KeyboardInterrupt:
            print '\nCtrl-c pressed. Later, skater!'
            exit()
Esempio n. 5
0
    def autodiscover_stories(self):
        rows = []

        api_url = 'https://www.googleapis.com/analytics/v3/data/ga'
        credentials = get_credentials()

        params = {
            'ids': 'ga:{0}'.format(app_config.GA_ORGANIZATION_ID),
            'start-date': '5daysAgo',  # start_date.strftime('%Y-%m-%d'),
            'end-date': 'today',
            'metrics': 'ga:totalEvents',
            'dimensions': 'ga:eventCategory',
            'max-results': app_config.GA_RESULT_SIZE,
            'samplingLevel': app_config.GA_SAMPLING_LEVEL,
            'start-index': 1,
        }

        resp = app_config.authomatic.access(credentials,
                                            api_url,
                                            params=params)
        data = resp.data

        if not data.get('rows'):
            logger.info('No rows found, done.')

        for row in resp.data['rows']:
            rows.append(row)

        return rows
Esempio n. 6
0
def _create_spreadsheet(title):
    """
    Copy the COPY spreadsheet
    """
    kwargs = {
        'credentials': get_credentials(),
        'url':
        SPREADSHEET_COPY_URL_TEMPLATE % env.post_config.COPY_GOOGLE_DOC_KEY,
        'method': 'POST',
        'headers': {
            'Content-Type': 'application/json'
        },
        'body': json.dumps({
            'title': title,
        }),
    }

    resp = app_config.authomatic.access(**kwargs)
    if resp.status == 200:
        spreadsheet_key = resp.data['id']
        spreadsheet_url = SPREADSHEET_VIEW_TEMPLATE % spreadsheet_key
        print 'New spreadsheet created successfully!'
        print 'View it online at %s' % spreadsheet_url
        return spreadsheet_key
    else:
        print 'Error creating spreadsheet (status code %s) with message %s' % (
            resp.status, resp.reason)
        return None
Esempio n. 7
0
def update():
    """
    Downloads a Google Doc as an Excel file.
    """
    if app_config.COPY_GOOGLE_DOC_KEY == None:
        logger.warn(
            'You have set COPY_GOOGLE_DOC_KEY to None. If you want to use a Google Sheet, set COPY_GOOGLE_DOC_KEY  to the key of your sheet in app_config.py'
        )
        return

    credentials = get_credentials()
    if not credentials:
        print logger.warn('No Google OAuth credentials file found.')
        print logger.warn(
            'Run `fab app` and visit `http://localhost:8000` to generate credentials.'
        )
        return

    get_document(app_config.COPY_GOOGLE_DOC_KEY, app_config.COPY_PATH)
    get_document(app_config.NAVBAR_GOOGLE_DOC_KEY, app_config.NAVBAR_PATH)

    get_document(app_config.BRIEFING_GOOGLE_DOC_KEY, app_config.BRIEFING_PATH)
    with open('www/data/extra_data/state-briefings.json', 'w') as f:
        copy = copytext.Copy(app_config.BRIEFING_PATH)
        output = copy.json()
        f.write(output)
Esempio n. 8
0
def copy_spreadsheet(slug):
    """
    Copy the COPY spreadsheet
    """
    _check_credentials()

    config_path = '%s/%s/graphic_config.py' % (app_config.GRAPHICS_PATH, slug)
    graphic_config = imp.load_source('graphic_config', config_path)

    kwargs = {
        'credentials': get_credentials(),
        'url': SPREADSHEET_COPY_URL_TEMPLATE % graphic_config.COPY_GOOGLE_DOC_KEY,
        'method': 'POST',
        'headers': {'Content-Type': 'application/json'},
        'body': json.dumps({
            'title': '%s GRAPHIC COPY' % slug,
        }),
    }

    resp = app_config.authomatic.access(**kwargs)
    if resp.status == 200:
        spreadsheet_key = resp.data['id']
        spreadsheet_url = SPREADSHEET_VIEW_TEMPLATE % spreadsheet_key
        print 'New spreadsheet created successfully!'
        print 'View it online at %s' % spreadsheet_url
        utils.replace_in_file(config_path, graphic_config.COPY_GOOGLE_DOC_KEY, spreadsheet_key)
    else:
        print 'Error creating spreadsheet (status code %s) with message %s' % (resp.status, resp.reason)
        return None
Esempio n. 9
0
def create_spreadsheet(title):
    """
    Copy the COPY spreadsheet
    """
    kwargs = {
        'credentials': get_credentials(),
        'url': SPREADSHEET_COPY_URL_TEMPLATE % app_config.COPY_GOOGLE_DOC_KEY,
        'method': 'POST',
        'headers': {
            'Content-Type': 'application/json'
        },
        'body': json.dumps({
            'title': title,
        }),
    }

    resp = app_config.authomatic.access(**kwargs)
    if resp.status == 200:
        spreadsheet_key = resp.data['id']
        logger.info('New spreadsheet created with key %s' % spreadsheet_key)
        return spreadsheet_key
    else:
        logger.info(
            'Error creating spreadsheet (status code %s) with message %s' %
            (resp.status, resp.reason))
        return None
Esempio n. 10
0
    def autodiscover_stories(self):
        rows = []

        api_url = 'https://www.googleapis.com/analytics/v3/data/ga'
        credentials = get_credentials()

        params = {
            'ids': 'ga:{0}'.format(app_config.GA_ORGANIZATION_ID),
            'start-date': '5daysAgo', # start_date.strftime('%Y-%m-%d'),
            'end-date': 'today',
            'metrics': 'ga:totalEvents',
            'dimensions': 'ga:eventCategory',
            'max-results': app_config.GA_RESULT_SIZE,
            'samplingLevel': app_config.GA_SAMPLING_LEVEL,
            'start-index': 1,
        }

        resp = app_config.authomatic.access(credentials, api_url, params=params)
        data = resp.data

        if not data.get('rows'):
            logger.info('No rows found, done.')

        for row in resp.data['rows']:
            rows.append(row)

        return rows
Esempio n. 11
0
 def query_ga(self, params):
     api_url = 'https://www.googleapis.com/analytics/v3/data/ga'
     credentials = get_credentials()
     resp = app_config.authomatic.access(credentials,
                                         api_url,
                                         params=params)
     data = resp.data
     return data
Esempio n. 12
0
def copy_spreadsheet(slug):
    """
    Copy the COPY spreadsheet
    """
    _check_credentials()

    config_path = '%s/%s/' % (app_config.GRAPHICS_PATH, slug)
    graphic_config = load_graphic_config(config_path)

    if not hasattr(
            graphic_config,
            'COPY_GOOGLE_DOC_KEY') or not graphic_config.COPY_GOOGLE_DOC_KEY:
        print 'Skipping spreadsheet creation. (COPY_GOOGLE_DOC_KEY is not defined in %s/graphic_config.py.)' % slug
        return

    metadata = {'title': '%s GRAPHIC COPY' % slug}
    try:
        if app_config.DRIVE_SPREADSHEETS_FOLDER:
            metadata['parents'] = [{
                'id': app_config.DRIVE_SPREADSHEETS_FOLDER
            }]
    except AttributeError:
        pass

    kwargs = {
        'credentials': get_credentials(),
        'url':
        SPREADSHEET_COPY_URL_TEMPLATE % graphic_config.COPY_GOOGLE_DOC_KEY,
        'method': 'POST',
        'headers': {
            'Content-Type': 'application/json'
        },
        'body': json.dumps(metadata),
    }

    resp = app_config.authomatic.access(**kwargs)

    if resp.status == 200:
        spreadsheet_key = resp.data['id']
        spreadsheet_url = SPREADSHEET_VIEW_TEMPLATE % spreadsheet_key
        print 'New spreadsheet created successfully!'
        print 'View it online at %s' % spreadsheet_url
        utils.replace_in_file('%s/graphic_config.py' % config_path,
                              graphic_config.COPY_GOOGLE_DOC_KEY,
                              spreadsheet_key)

        return True
    else:
        utils.replace_in_file('%s/graphic_config.py' % config_path,
                              graphic_config.COPY_GOOGLE_DOC_KEY, '')
    print 'Error creating spreadsheet (status code %s) with message %s' % (
        resp.status, resp.reason)
    if resp.status == 404:
        print 'Please make sure you modify the DRIVE_SPREADSHEETS_FOLDER in app_config.py. Check the configuration section on the README.'
    return False
Esempio n. 13
0
def download():
    """
    Downloads the speeches CSV from google docs.
    """
    credentials = get_credentials()
    url = SPREADSHEET_URL_TEMPLATE % app_config.DATA_GOOGLE_DOC_KEY
    print url
    response = app_config.authomatic.access(credentials, url)

    with codecs.open('data/data.csv', 'wb', 'utf-8') as writefile:
        writefile.write(response.content)
Esempio n. 14
0
def post_listings(listings, sheet_name, username=None, password=None):
    if username or password:
        google = gspread.login(username, password)
    else:
        credentials = get_credentials()
        try:
            google = gspread.authorize(credentials)
        except Exception, e:
            print "Exception trying to authenticate with google: %s" % e
            print "Clearing any credentials saved in keyring..."
            clear_credentials()
            sys.exit(1)
Esempio n. 15
0
def post_listings(listings, sheet_name, username=None, password=None):
    if username or password:
        google = gspread.login(username, password)
    else:
        credentials = get_credentials()
        try:
            google = gspread.authorize(credentials)
        except Exception, e:
            print "Exception trying to authenticate with google: %s" % e
            print "Clearing any credentials saved in keyring..."
            clear_credentials()
            sys.exit(1)
Esempio n. 16
0
def update_songs(verify='true'):
    credentials = get_credentials()
    spreadsheet_url = SONGS_SPREADSHEET_URL_TEMPLATE % app_config.SONGS_GOOGLE_DOC_KEY

    resp = app_config.authomatic.access(credentials, spreadsheet_url)

    with codecs.open(app_config.SONGS_CSV_DATA_PATH, 'w', 'utf-8') as f:
        f.write(resp.content)

    output = clean_songs(verify == 'true')

    with codecs.open('data/songs.json', 'w', 'utf-8') as f:
        json.dump(output, f)
Esempio n. 17
0
def update_songs(verify='true'):
    credentials = get_credentials()
    spreadsheet_url = SONGS_SPREADSHEET_URL_TEMPLATE % app_config.SONGS_GOOGLE_DOC_KEY

    resp = app_config.authomatic.access(credentials, spreadsheet_url)

    with codecs.open(app_config.SONGS_CSV_DATA_PATH, 'w', 'utf-8') as f:
        f.write(resp.content)

    output = clean_songs(verify == 'true')

    with codecs.open('data/songs.json', 'w', 'utf-8') as f:
        json.dump(output, f)
Esempio n. 18
0
def update():
    """
    Downloads a Google Doc as an Excel file.
    """
    if app_config.COPY_GOOGLE_DOC_KEY == None:
        print colored('You have set COPY_GOOGLE_DOC_KEY to None. If you want to use a Google Sheet, set COPY_GOOGLE_DOC_KEY  to the key of your sheet in app_config.py', 'blue')
        return
    credentials = get_credentials()
    if not credentials:
        print colored('No Google OAuth credentials file found.', 'yellow')
        print colored('Run `fab app` and visit `http://localhost:8000` to generate credentials.', 'yellow')
        return

    get_document(app_config.COPY_GOOGLE_DOC_KEY, app_config.COPY_PATH)
Esempio n. 19
0
def update_copytext():
    """
    Downloads a Google Doc as an Excel file.
    """
    if app_config.COPY_GOOGLE_DOC_KEY == None:
        logger.warn('You have set COPY_GOOGLE_DOC_KEY to None. If you want to use a Google Sheet, set COPY_GOOGLE_DOC_KEY  to the key of your sheet in app_config.py')
        return

    credentials = get_credentials()
    if not credentials:
        print logger.warn('No Google OAuth credentials file found.')
        print logger.warn('Run `fab app` and visit `http://localhost:8000` to generate credentials.')
        return

    get_spreadsheet(app_config.COPY_GOOGLE_DOC_KEY, app_config.COPY_PATH)
Esempio n. 20
0
def update():
    """
    Downloads a Google Doc as an Excel file.
    """
    if app_config.COPY_GOOGLE_DOC_KEY == None:
        logger.warn('You have set COPY_GOOGLE_DOC_KEY to None. If you want to use a Google Sheet, set COPY_GOOGLE_DOC_KEY  to the key of your sheet in app_config.py')
        return

    credentials = get_credentials()
    if not credentials:
        print logger.warn('No Google OAuth credentials file found.')
        print logger.warn('Run `fab app` and visit `http://localhost:8000` to generate credentials.')
        return

    get_document(app_config.COPY_GOOGLE_DOC_KEY, app_config.COPY_PATH)
Esempio n. 21
0
def copy_spreadsheet(slug):
    """
    Copy the COPY spreadsheet
    """
    _check_credentials()

    config_path = '%s/%s/' % (app_config.GRAPHICS_PATH, slug)
    graphic_config = load_graphic_config(config_path)

    if not hasattr(
            graphic_config,
            'COPY_GOOGLE_DOC_KEY') or not graphic_config.COPY_GOOGLE_DOC_KEY:
        print 'Skipping spreadsheet creation. (COPY_GOOGLE_DOC_KEY is not defined in %s/graphic_config.py.)' % slug
        return

    kwargs = {
        'credentials': get_credentials(),
        'url':
        SPREADSHEET_COPY_URL_TEMPLATE % graphic_config.COPY_GOOGLE_DOC_KEY,
        'method': 'POST',
        'headers': {
            'Content-Type': 'application/json'
        },
        'body': json.dumps({
            'title': '%s GRAPHIC COPY' % slug,
        }),
    }

    resp = app_config.authomatic.access(**kwargs)

    if resp.status == 200:
        spreadsheet_key = resp.data['id']
        spreadsheet_url = SPREADSHEET_VIEW_TEMPLATE % spreadsheet_key
        print 'New spreadsheet created successfully!'
        print 'View it online at %s' % spreadsheet_url
        utils.replace_in_file('%s/graphic_config.py' % config_path,
                              graphic_config.COPY_GOOGLE_DOC_KEY,
                              spreadsheet_key)

        return True
    else:
        utils.replace_in_file(config_path, graphic_config.COPY_GOOGLE_DOC_KEY,
                              '')

    print 'Error creating spreadsheet (status code %s) with message %s' % (
        resp.status, resp.reason)
    return False
Esempio n. 22
0
    def query_ga(params):
        api_url = 'https://www.googleapis.com/analytics/v3/data/ga'
        credentials = get_credentials()
        resp = app_config.authomatic.access(credentials, api_url, params=params)
        data = resp.data

        # Handle errors better
        if data.get('error'):
            try:
                if data.get('error')['code'] == 403:
                    logger.error("Google Analytics permissions error. Check your GA organization ID and oauth permissions.")
            except:
                pass

            logger.error(data)

        return data
Esempio n. 23
0
def copy_spreadsheet(slug):
    """
    Copy the COPY spreadsheet
    """
    _check_credentials()

    config_path = '%s/%s/' % (app_config.GRAPHICS_PATH, slug)
    graphic_config = load_graphic_config(config_path)

    if not hasattr(graphic_config, 'COPY_GOOGLE_DOC_KEY') or not graphic_config.COPY_GOOGLE_DOC_KEY:
        print 'Skipping spreadsheet creation. (COPY_GOOGLE_DOC_KEY is not defined in %s/graphic_config.py.)' % slug
        return

    metadata = {'title': '%s GRAPHIC COPY' % slug}
    try:
        if app_config.DRIVE_SPREADSHEETS_FOLDER:
            metadata['parents'] = [{
                'id': app_config.DRIVE_SPREADSHEETS_FOLDER}]
    except AttributeError:
        pass

    kwargs = {
        'credentials': get_credentials(),
        'url': SPREADSHEET_COPY_URL_TEMPLATE % graphic_config.COPY_GOOGLE_DOC_KEY,
        'method': 'POST',
        'headers': {'Content-Type': 'application/json'},
        'body': json.dumps(metadata),
    }

    resp = app_config.authomatic.access(**kwargs)

    if resp.status == 200:
        spreadsheet_key = resp.data['id']
        spreadsheet_url = SPREADSHEET_VIEW_TEMPLATE % spreadsheet_key
        print 'New spreadsheet created successfully!'
        print 'View it online at %s' % spreadsheet_url
        utils.replace_in_file('%s/graphic_config.py' % config_path , graphic_config.COPY_GOOGLE_DOC_KEY, spreadsheet_key)

        return True
    else:
        utils.replace_in_file('%s/graphic_config.py' % config_path, graphic_config.COPY_GOOGLE_DOC_KEY, '')
    print 'Error creating spreadsheet (status code %s) with message %s' % (resp.status, resp.reason)
    if resp.status == 404:
        print 'Please make sure you modify the DRIVE_SPREADSHEETS_FOLDER in app_config.py. Check the configuration section on the README.'
    return False
def create_spreadsheet(title):
    """
    Copy the COPY spreadsheet
    """
    kwargs = {
        "credentials": get_credentials(),
        "url": SPREADSHEET_COPY_URL_TEMPLATE % app_config.COPY_GOOGLE_DOC_KEY,
        "method": "POST",
        "headers": {"Content-Type": "application/json"},
        "body": json.dumps({"title": title}),
    }

    resp = app_config.authomatic.access(**kwargs)
    if resp.status == 200:
        spreadsheet_key = resp.data["id"]
        print "New spreadsheet created with key %s" % spreadsheet_key
        return spreadsheet_key
    else:
        print "Error creating spreadsheet (status code %s) with message %s" % (resp.status, resp.reason)
        return None
Esempio n. 25
0
def update_copy():
    """
    Downloads a Google Doc as an Excel file.
    """

    print "DOING THINGS"

    if app_config.COPY_GOOGLE_DOC_KEY == None:
        print colored(
            "You have set COPY_GOOGLE_DOC_KEY to None. If you want to use a Google Sheet, set COPY_GOOGLE_DOC_KEY  to the key of your sheet in app_config.py",
            "blue",
        )
        return

    credentials = get_credentials()
    if not credentials:
        print colored("No Google OAuth credentials file found.", "yellow")
        print colored("Run `fab app` and visit `http://localhost:8000` to generate credentials.", "yellow")
        return

    get_document(app_config.COPY_GOOGLE_DOC_KEY, app_config.COPY_PATH)
Esempio n. 26
0
    def query_ga(params):
        api_url = 'https://www.googleapis.com/analytics/v3/data/ga'
        credentials = get_credentials()
        resp = app_config.authomatic.access(credentials,
                                            api_url,
                                            params=params)
        data = resp.data

        # Handle errors better
        if data.get('error'):
            try:
                if data.get('error')['code'] == 403:
                    logger.error(
                        "Google Analytics permissions error. Check your GA organization ID and oauth permissions."
                    )
            except:
                pass

            logger.error(data)

        return data
def create_spreadsheet(title):
    """
    Copy the COPY spreadsheet
    """
    kwargs = {
        'credentials': get_credentials(),
        'url': SPREADSHEET_COPY_URL_TEMPLATE % app_config.COPY_GOOGLE_DOC_KEY,
        'method': 'POST',
        'headers': {'Content-Type': 'application/json'},
        'body': json.dumps({
            'title': title,
        }),
    }

    resp = app_config.authomatic.access(**kwargs)
    if resp.status == 200:
        spreadsheet_key = resp.data['id']
        print 'New spreadsheet created with key %s' % spreadsheet_key
        return spreadsheet_key
    else:
        print 'Error creating spreadsheet (status code %s) with message %s' % (resp.status, resp.reason)
        return None
Esempio n. 28
0
    def scrape_google_analytics(self, start_date):
        rows = []

        api_url = 'https://www.googleapis.com/analytics/v3/data/ga'
        credentials = get_credentials()

        metrics = ','.join(['ga:{0}'.format(metric) for metric in app_config.GA_METRICS])
        dimensions = ','.join(['ga:{0}'.format(dimensions) for dimensions in app_config.GA_DIMENSIONS])

        params = {
            'ids': 'ga:{0}'.format(app_config.GA_ORGANIZATION_ID),
            'end-date': 'yesterday',
            'start-date': start_date.strftime('%Y-%m-%d'),
            'metrics': 'ga:sessions,ga:pageviews',
            'dimensions': 'ga:pagePath,ga:source,ga:deviceCategory',
            'max-results': app_config.GA_RESULT_SIZE,
            'samplingLevel': app_config.GA_SAMPLING_LEVEL,
            'start-index': 1,
        }

        while True:
            resp = app_config.authomatic.access(credentials, api_url, params=params)
            data = resp.data

            logger.info('Processing rows {0} - {1}'.format(params['start-index'], params['start-index'] + app_config.GA_RESULT_SIZE - 1))

            if not data.get('rows'):
                logger.info('No rows found, done.')
                break

            for row in resp.data['rows']:
                analytics_row = GoogleAnalyticsRow(row, app_config.GA_METRICS, app_config.GA_DIMENSIONS, data)
                rows.append(analytics_row.serialize())

            params['start-index'] += app_config.GA_RESULT_SIZE

        #import ipdb; ipdb.set_trace();
        return rows
Esempio n. 29
0
def copy_spreadsheet(slug):
    """
    Copy the COPY spreadsheet
    """
    _check_credentials()

    config_path = '%s/%s/' % (app_config.GRAPHICS_PATH, slug)
    graphic_config = load_graphic_config(config_path)

    if not hasattr(graphic_config, 'COPY_GOOGLE_DOC_KEY') or not graphic_config.COPY_GOOGLE_DOC_KEY:
        print 'Skipping spreadsheet creation. (COPY_GOOGLE_DOC_KEY is not defined in %s/graphic_config.py.)' % slug
        return

    kwargs = {
        'credentials': get_credentials(),
        'url': SPREADSHEET_COPY_URL_TEMPLATE % graphic_config.COPY_GOOGLE_DOC_KEY,
        'method': 'POST',
        'headers': {'Content-Type': 'application/json'},
        'body': json.dumps({
            'title': '%s GRAPHIC COPY' % slug,
            'parents': [ {"kind": "drive#fileLink", "id": "0B2CR1DddJUTbMXNzN3VNUVNKQjQ"}],
        }),
    }
    resp = app_config.authomatic.access(**kwargs)

    if resp.status == 200:
        spreadsheet_key = resp.data['id']
        spreadsheet_url = SPREADSHEET_VIEW_TEMPLATE % spreadsheet_key
        print 'New spreadsheet created successfully!'
        print 'View it online at %s' % spreadsheet_url
        utils.replace_in_file('%s/graphic_config.py' % config_path , graphic_config.COPY_GOOGLE_DOC_KEY, spreadsheet_key)
        return (True, spreadsheet_url)
    else:
        utils.replace_in_file(config_path, graphic_config.COPY_GOOGLE_DOC_KEY, '')

    print 'Error creating spreadsheet (status code %s) with message %s' % (resp.status, resp.reason)
    return (False, 'Nope')
Esempio n. 30
0
def copy_spreadsheet(slug):
    """
    Copy the COPY spreadsheet
    """
    _check_credentials()

    config_path = "%s/%s/graphic_config.py" % (app_config.GRAPHICS_PATH, slug)
    graphic_config = _graphic_config(slug)

    if not hasattr(graphic_config, "COPY_GOOGLE_DOC_KEY") or not graphic_config.COPY_GOOGLE_DOC_KEY:
        print "Skipping spreadsheet creation. (COPY_GOOGLE_DOC_KEY is not defined in %s/graphic_config.py.)" % slug
        return

    kwargs = {
        "credentials": get_credentials(),
        "url": SPREADSHEET_COPY_URL_TEMPLATE % graphic_config.COPY_GOOGLE_DOC_KEY,
        "method": "POST",
        "headers": {"Content-Type": "application/json"},
        "body": json.dumps({"title": "%s GRAPHIC COPY" % slug}),
    }

    resp = app_config.authomatic.access(**kwargs)

    if resp.status == 200:
        spreadsheet_key = resp.data["id"]
        spreadsheet_url = SPREADSHEET_VIEW_TEMPLATE % spreadsheet_key
        print "New spreadsheet created successfully!"
        print "View it online at %s" % spreadsheet_url
        utils.replace_in_file(config_path, graphic_config.COPY_GOOGLE_DOC_KEY, spreadsheet_key)

        return True

        utils.replace_in_file(config_path, graphic_config.COPY_GOOGLE_DOC_KEY, "")

    print "Error creating spreadsheet (status code %s) with message %s" % (resp.status, resp.reason)
    return False
Esempio n. 31
0
def update():
    """
    Downloads a Google Doc as an Excel file.
    """
    if app_config.COPY_GOOGLE_DOC_KEY == None:
        logger.warn(
            'You have set COPY_GOOGLE_DOC_KEY to None. If you want to use a Google Sheet, set COPY_GOOGLE_DOC_KEY  to the key of your sheet in app_config.py'
        )
        return

    credentials = get_credentials()
    if not credentials:
        print logger.warn('No Google OAuth credentials file found.')
        print logger.warn(
            'Run `fab app` and visit `http://localhost:8000` to generate credentials.'
        )
        return

    get_document(app_config.COPY_GOOGLE_DOC_KEY, app_config.COPY_PATH)
    with open('www/js/songs.json', 'w') as f:
        sheet = copytext.Copy(app_config.COPY_PATH)
        all_lists = list(sheet['best_lists']) + list(sheet['deeper_lists'])
        all_songs = {}

        for row in all_lists:
            if row['slug']:
                slug = row['slug'].replace('-', '_')
                songs = sheet[slug]
                for song in songs:
                    if all_songs.get(song['song_slug']):
                        continue

                    song_obj = {}
                    song_obj['sort'] = song['sort']
                    song_obj['artist'] = song['artist']
                    song_obj['title'] = song['title']

                    if song['song_slug'] == 'big-thief-masterpiece' and song[
                            'type'] == 'album':
                        song_obj['song_slug'] = 'big-thief-masterpiece-album'
                    elif song['song_slug'] == 'big-thief-masterpiece' and song[
                            'type'] == 'song':
                        song_obj['song_slug'] = 'big-thief-masterpiece-song'
                    else:
                        song_obj['song_slug'] = song['song_slug']

                    song_obj['description'] = song['description']
                    song_obj['art'] = song['art']
                    song_obj['author'] = song['author']
                    song_obj['author_twitter'] = song['author_twitter']
                    song_obj['affiliation'] = song['affiliation']
                    song_obj['explicit'] = song['explicit']
                    song_obj['filed_under'] = song['filed_under']
                    song_obj['type'] = song['type']
                    song_obj['smarturl'] = song['smarturl']
                    song_obj['embed'] = song['embed'].replace(
                        'watch?v=', 'embed/')
                    song_obj['embed_type'] = song['embed_type']
                    song_obj['embed_caption'] = song['embed_caption']
                    all_songs[song_obj['song_slug']] = song_obj

        output = json.dumps(all_songs)
        f.write(output)
def oauth(secrets_path, token_path, scope):
    """Generate an OAuth token."""
    from oauth import get_credentials
    get_credentials(scope, secrets_path, token_path)
Esempio n. 33
0
def copy_spreadsheet(slug):
    """
    Copy the template Google spreadsheet into the graphics folder on Drive.
    """
    _check_credentials()

    config_path = '%s/%s/' % (app_config.GRAPHICS_PATH, slug)
    graphic_config = load_graphic_config(config_path)

    new_file_key = graphic_config.COPY_GOOGLE_DOC_KEY

    if not hasattr(graphic_config, 'COPY_GOOGLE_DOC_KEY') or not new_file_key:
        print 'Skipping spreadsheet creation. (COPY_GOOGLE_DOC_KEY is not ' + \
            'defined in %s/graphic_config.py.)' % slug
        return

    req_url = SPREADSHEET_COPY_URL_TEMPLATE % graphic_config.COPY_GOOGLE_DOC_KEY
    kwargs = {
        'credentials': get_credentials(),
        'url': req_url,
        'method': 'POST',
        'headers': {'Content-Type': 'application/json'},
        'body': json.dumps({
            'title': slug
        }),
    }

    resp = app_config.authomatic.access(**kwargs)

    if resp.status == 200:  # Sucessfully created file
        print resp
        spreadsheet_key = resp.data['id']

        # Copy file in Drive from 'templates' to 'graphics' directory.
        graphics_dir_key = '0B95Rq71MRLfHY0gxUURqbS1tWDg'  # TODO: move to cfg
        request_url = 'https://www.googleapis.com/drive/v2/files/' + \
            '%s/parents' % spreadsheet_key
        kwargs = {
            'credentials': get_credentials(),
            'url': request_url,
            'method': 'POST',
            'headers': {'Content-Type': 'application/json'},
            'body': json.dumps({
                'id': graphics_dir_key
            })
        }
        resp2 = app_config.authomatic.access(**kwargs)

        if resp2.status == 200:  # Successfully copied file to 'graphics' folder
            # Delete old file from 'templates' directory.
            templates_dir_key = '0B95Rq71MRLfHeTlSNmtnWmEwQ28'
            request_url = 'https://www.googleapis.com/drive/v2/files/' + \
                '%s/parents/%s' % (spreadsheet_key, templates_dir_key)
            kwargs = {
                'credentials': get_credentials(),
                'url': request_url,
                'method': 'DELETE',
                'headers': {'Content-Type': 'application/json'}
            }
            resp3 = app_config.authomatic.access(**kwargs)

            if resp3.status == 204:  # Successfully deleted old file
                spreadsheet_url = SPREADSHEET_VIEW_TEMPLATE % spreadsheet_key
                print 'New spreadsheet created successfully!'
                # local('open %s' % spreadsheet_url)
                print 'View it online at %s' % spreadsheet_url
                utils.replace_in_file(
                    '%s/graphic_config.py' % config_path,
                    graphic_config.COPY_GOOGLE_DOC_KEY,
                    spreadsheet_key)

                return True

                utils.replace_in_file(
                    config_path, graphic_config.COPY_GOOGLE_DOC_KEY, '')

    print 'Error creating spreadsheet (status code %s) with message %s' % (
        resp.status, resp.reason)
    return False
Esempio n. 34
0
 def query_ga(self, params):
     api_url = 'https://www.googleapis.com/analytics/v3/data/ga'
     credentials = get_credentials()
     resp = app_config.authomatic.access(credentials, api_url, params=params)
     data = resp.data
     return data