예제 #1
0
def main():
    # setup logging
    logging.basicConfig(level=logging.INFO, format='%(levelname)s:%(message)s')

    # Get an API connection
    credentials = van_api.ClientCredentialsGrant(API_KEY, API_SECRET)
    api = van_api.API('api.metropublisher.com', credentials)

    fields = ['url', 'location_types']
    start_url = '/{}/locations?fields={}&rpp=100'.format(INSTANCE_ID, '-'.join(fields))

    csv_file = None
    count = 0
    for loc_url, location_types in get_all_items(api, start_url):
        count += 1
        # get full location info
        loc = api.GET(loc_url)
        add_geoname_data(loc)
        # cast everything to a string
        loc = to_csv_value(loc)
        if csv_file is None:
            # create our csv file writer if none already exists
            fieldnames = sorted(loc.keys())
            csv_file = csv.DictWriter(sys.stdout, fieldnames)
            # write headers
            headers = dict([(k, k) for k in loc])
            csv_file.writerow(headers)
        # write out one line of the CSV
        csv_file.writerow(loc)
    logging.info('Exported {} locations'.format(count))
    return 0
예제 #2
0
def main():
    # setup logging
    logging.basicConfig(level=logging.INFO, format='%(levelname)s:%(message)s')

    # Get an API connection
    credentials = van_api.ClientCredentialsGrant(API_KEY, API_SECRET)
    api = van_api.API('api.metropublisher.com', credentials)

    fields = ['url', 'title']
    # page=2&rpp=2'
    start_url = '/{}/files?groups=image&fields={}&page=1&rpp=100'.format(
        INSTANCE_ID, '-'.join(fields))
    # print url

    csv_file = None
    count = 0

    download_filename, download_url = get_all_items(api, start_url)

    # create images folder in the current directory
    try:
        if not os.path.exists('images'):
            os.makedirs('images')
    except OSError as exc:  # Guard against race condition
        if exc.errno != errno.EEXIST:
            raise

    f = open('all_files.csv', 'wb')
    f.write(u'\ufeff'.encode(
        'utf8'))  # BOM (optional...Excel needs it to open UTF-8 file properly)

    fieldnames = ['Filename', 'Download URL']
    writer = csv.DictWriter(f, fieldnames=fieldnames)

    writer.writeheader()

    for filename, url in zip(download_filename, download_url):
        count += 1

        # Download the jpg image file
        with open('./images/' + filename, 'wb') as handler:
            api.GET(url, handler)
            bytes_written = handler.tell()
            logging.info("Downloaded " + filename +
                         " %s bytes" % bytes_written)

        writer.writerow({
            'Filename': filename.encode('utf8'),
            'Download URL': url.encode('utf8')
        })

    logging.info('Exported {} file'.format(count))
    return 0
예제 #3
0
def main():
    # setup logging
    logging.basicConfig(level=logging.INFO, format='%(levelname)s:%(message)s')

    # Get an API connection
    credentials = van_api.ClientCredentialsGrant(API_KEY, API_SECRET)
    api = van_api.API('api.metropublisher.com', credentials)

    fields = ['url', 'title']
    start_url = '/{}/files?groups=image&fields={}&rpp=100'.format(
        INSTANCE_ID, '-'.join(fields))

    csv_file = None
    count = 0

    for download_url, filename in get_all_items(api, start_url):
        count += 1
        # get full file info
        file = api.GET(download_url)

        # cast everything to a string
        file = to_csv_value(file)
        if csv_file is None:

            # create our csv file writer if none already exists
            fieldnames = sorted(file.keys())
            print filename
            #fieldnames = 'fileation_uuid'
            csv_file = csv.DictWriter(sys.stdout, fieldnames)
            # write headers
            headers = dict([(k, k) for k in file])
            csv_file.writerow(headers)
        # write out one line of the CSV
        csv_file.writerow(file)
    logging.info('Exported {} files'.format(count))
    return 0
예제 #4
0
import urllib
import os
import logging
import tempfile
from pprint import pprint

import van_api

# Configuration
API_KEY = os.environ["APIKEY"]
API_SECRET = os.environ["APISECRET"]
INSTANCE_ID = os.environ["APIID"]

logging.basicConfig(level=logging.INFO, format='%(levelname)s:%(message)s')

# Get an API connection
credentials = van_api.ClientCredentialsGrant(API_KEY, API_SECRET)
api = van_api.API('api.metropublisher.com', credentials)

start_url = '/{}'.format(INSTANCE_ID)

# get list of files
logging.info("Getting top-level catalog of resources in the instance/site.")
result = api.GET(start_url)
pprint(result)

logging.info("Getting top-level list of sections")
#result = api.GET('/282/sections?fields=title-urlname-url')
result = api.GET(start_url + '/sections?fields=title-urlname-url')
pprint(result)