Last active
December 19, 2015 04:18
-
-
Save robmathers/5896165 to your computer and use it in GitHub Desktop.
Grabs full size iTunes artwork for search parameters on the command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import httplib | |
import json | |
import re | |
import sys | |
from urllib import quote_plus | |
def iTunesData(keywords, media = 'all'): | |
iTunesDomain = 'itunes.apple.com' | |
if media not in ['movie', 'podcast', 'music', 'musicVideo', 'audiobook', 'shortFilm', 'tvShow', 'software', 'ebook', 'all']: | |
media = 'all' | |
queryString = '/search?media=' + media + '&term=' + quote_plus(keywords) | |
connection = httplib.HTTPSConnection(iTunesDomain) | |
connection.request('GET', queryString) | |
response = connection.getresponse() | |
return response.read() | |
def jsonParse(data): | |
decoder = json.JSONDecoder(strict = False) | |
return decoder.decode(data) | |
def artworkURL(iTunesJSON, resultIndex = 0): | |
if iTunesJSON['resultCount'] > 0: | |
result = iTunesJSON['results'][resultIndex] | |
return re.sub(r'\.\d+x\d+-\d+', '', result['artworkUrl100']) | |
else: | |
print 'No results found' | |
return None | |
def main(keywords = None): | |
if keywords != None: | |
pass | |
elif len(sys.argv) > 1: | |
keywords = ' '.join(sys.argv[1:]) | |
else: | |
print 'No search keywords supplied' | |
sys.exit(1) | |
data = iTunesData(keywords) | |
myresults = jsonParse(data) | |
print artworkURL(myresults) | |
if __name__ == '__main__': | |
main() | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment