简体   繁体   中英

Downloading csv data from an API

I am attempting to download csv data from an API which I will then edit I am struggling to get the different functions to work together.

ie passing the export link through to download the file and then through to opening it.

    '''
File name: downloadAWR.py
Author: Harry&Joe
Date created: 3/10/17
Date last modified: 5/10/17
Version: 3.6
'''

import requests
import json
import urllib2
import zipfile
import io
import csv
import os
from urllib2 import urlopen, URLError, HTTPError

geturl() is used to create a download link for the csv data, one link will be created with user input data in this case the name and dates, this will then create a link that we can use to download the data. the link is stored in export_link

def geturl():

#getProjectName
project_name = 'BIMM'

#getApiToken
api_token = "API KEY HERE"

#getStartDate
start_date = '2017-01-01'

#getStopDate
stop_date = '2017-09-01'

url = "https://api.awrcloud.com/get.php?action=export_ranking&project=%s&token=%s&startDate=%s&stopDate=%s" % (project_name,api_token,start_date,stop_date)

export_link = requests.get(url).content

return export_link

dlfile is used to actually use the link a get a file we can manipulate and edit eg removing columns and some of the data.

def dlfile(export_link):
# Open the url
try:
    f = urlopen(export_link)
    print ("downloading " + export_link)

    # Open our local file for writing
    with open(os.path.basename(export_link), "wb") as local_file:
        local_file.write(f.read())

#handle errors
except HTTPError as e:
    print ("HTTP Error:", e.code, export_link)
except URLError as e:
    print ("URL Error:", e.reason, export_link)

return f

readdata is used to go into the file and open it for us to use.

def readdata():
    with zipfile.ZipFile(io.BytesIO(zipdata)) as z:
        for f in z.filelist:
            csvdata = z.read(f)

#reader = csv.reader(io.StringIO(csvdata.decode()))



def main():
#Do something with the csv data

    export_link = (geturl())


    data = dlfile(export_link)

    csvdata = data.readdata()



if __name__ == '__main__':
    main()

Generally I'm finding that the code works independently but struggles when I try to put it all together synchronously.

You need to clean up and call your code appropriately. It seems you copy pasted from different sources and now you have some salad bowl of code that isn't mixing well.

If the task is just to read and open a remote file to do something to it:

import io
import zipfile
import requests

def get_csv_file(project, api_token, start_date, end_date):
   url = "https://api.awrcloud.com/get.php"
   params = {'action': 'export_ranking',
             'project': project,
             'token': api_token,
             'startDate': start_date,
             'stopDate': end_date}

   r = requests.get(url, params)
   r.raise_for_status()

   return zipfile.ZipFile(io.BytesIO(request.get(r.content).content))

def process_csv_file(zip_file):
    contents = zip_file.extractall()
    # do stuff with the contents

if __name__ == '__main__':
   process_zip_file(get_csv_file('BIMM', 'api-key', '2017-01-01', '2017-09-01'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM