Searching iTunes API with a .csv

This is the place for queries that don't fit in any of the other categories.

Searching iTunes API with a .csv

Postby Oxcat » Tue Mar 26, 2013 6:33 pm

Hi,

I have a list of UPCs stored in a .csv that I am trying to search the iTunes API with. The search result is to be saved, ideally in a file named after the UPC. So far I have this, I am able to get a single search result to download but it stops after the first search.

Code: Select all
import urllib.request
import csv
with open('upc.csv', newline='') as f:
     reader = csv.reader(f)
     for row in reader:
           s = urllib.request.urlretrieve("https://itunes.apple.com/lookup?upc="+'row',"hold")


Any input on how to get the script fully functioning? Here's a list of sample UPCs.

803680281857
803680282052
669910370858
669910372555
669910371459

(There are quite a few more)

Thanks!
Oxcat
 
Posts: 3
Joined: Tue Mar 26, 2013 6:26 pm

Re: Searching iTunes API with a .csv

Postby snippsat » Tue Mar 26, 2013 8:12 pm

You should use an simple print function,to see what you actually send to urlretrieve().
'row' and "hold" are just strings.
Remove '' from row and you need something that increase "hold" or the filename is the same for all downloads,can use enumerate().
The format you get back from a lookup is json,you need to specify fileformat for second argument in urlretrieve().

So a test i did,i get back 5 json files named hold1...5.
Code: Select all
import urllib.request

with open('upc.csv') as f:
    for index,row in enumerate(f, 1):
        s = urllib.request.urlretrieve("https://itunes.apple.com/lookup?upc={}".format(row.strip()), "hold{}.json".format(index))
User avatar
snippsat
 
Posts: 85
Joined: Thu Feb 21, 2013 12:04 am

Re: Searching iTunes API with a .csv

Postby Oxcat » Tue Mar 26, 2013 8:38 pm

Thanks! The code executes but another problem occurred....all of them return as :
{
"resultCount":0,
"results": []
}

despite for example UPC 803680281857 existing

https://itunes.apple.com/lookup?upc=803680281857
Oxcat
 
Posts: 3
Joined: Tue Mar 26, 2013 6:26 pm

Re: Searching iTunes API with a .csv

Postby snippsat » Tue Mar 26, 2013 8:55 pm

Thanks! The code executes but another problem occurred....all of them return as :

Alle files return correct result for me.
My 'upc.csv' is just a copy i did of 5 number you posted.
Is your 'upc.csv' just as you posted?

Have you tested with a print function as i advised you,many time people forget simple stuff like this can be a very powerful debugging method ;)
Look like this for me.
Code: Select all
import urllib.request

with open('upc.csv') as f:
    for index,row in enumerate(f, 1):
        #s = urllib.request.urlretrieve("https://itunes.apple.com/lookup?upc={}".format(row.strip()), "hold{}.json".format(index))
        print("https://itunes.apple.com/lookup?upc={}".format(row.strip()), "hold{}.json".format(index))

'''Output-->
https://itunes.apple.com/lookup?upc=803680281857 hold1.json
https://itunes.apple.com/lookup?upc=803680282052 hold2.json
https://itunes.apple.com/lookup?upc=669910370858 hold3.json
https://itunes.apple.com/lookup?upc=669910372555 hold4.json
https://itunes.apple.com/lookup?upc=669910371459 hold5.json
'''
User avatar
snippsat
 
Posts: 85
Joined: Thu Feb 21, 2013 12:04 am

Re: Searching iTunes API with a .csv

Postby Oxcat » Tue Mar 26, 2013 9:38 pm

Whoops! I thought my upc.csv was listed exactly like that, but the print function showed that the numbers were changed in format automatically by excel. I never really noticed the print function, but I'm definitely going to keep that one in mind for the future!

Thanks so much!!!! :)
Oxcat
 
Posts: 3
Joined: Tue Mar 26, 2013 6:26 pm

Re: Searching iTunes API with a .csv

Postby Yoriz » Tue Mar 26, 2013 9:56 pm

Print is usually what most people use in there very first bit of code Hello World! :D
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 565
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Searching iTunes API with a .csv

Postby snippsat » Tue Mar 26, 2013 10:09 pm

Print is usually what most people use in there very first bit of code Hello World!

Yes,and then later in many cases forget to use print when is not so obvious as wanted output :?
User avatar
snippsat
 
Posts: 85
Joined: Thu Feb 21, 2013 12:04 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 1 guest