-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.py
47 lines (43 loc) · 1.74 KB
/
metadata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import requests
api_url = 'https://itunes.apple.com/search?media=audiobook&term='
class Audiobookmeta:
"""Retrieves metadata from Source"""
searchterm = ""
collectionName = ""
artistName = ""
artworkUrl = ""
releaseDate = ""
primaryGenreName = ""
description = ""
def __init__(self, search):
self.searchterm = search
self.artistName = self.searchterm.split("-")[0]
self.collectionName = self.searchterm.split("-")[-1]
self.searchterm.replace('-', '')
self.searchterm.replace(':', '')
self.searchterm.replace(' ', ' ')
self.searchterm.replace(' ', '%20')
def tryRetrieveFromITunes(self):
if(len(self.searchterm)==0):
print ("Searchterm is empty")
return False
try:
response = requests.get(api_url+self.searchterm).json()
# print(api_url+self.searchterm)
if(len(response["results"])<1):
print ("No audiobook found")
return False
result = response["results"][0]
# print(result)
self.collectionName = result["collectionName"]
self.artistName = result["artistName"]
self.artworkUrl = result["artworkUrl100"]
self.releaseDate = result["releaseDate"]
self.primaryGenreName = result["primaryGenreName"]
self.description = result["description"]
self.artworkUrl = self.artworkUrl.replace('100x100bb.jpg', '600x600bb.jpg')
print("audiobook found: "+self.artistName+" - "+self.collectionName)
return True
except requests.exceptions.RequestException as e:
print ("An error occured while retrieving metadata")
return False