-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessing2.py
More file actions
68 lines (55 loc) · 2.14 KB
/
processing2.py
File metadata and controls
68 lines (55 loc) · 2.14 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import musicbrainzngs
musicbrainzngs.set_useragent('globify', version='v1', contact=None)
def getCountry(id):
result = musicbrainzngs.get_area_by_id(id, includes=['area-rels'])['area']
if result['type'] == 'Country':
return result['name']
elif 'iso-3166-2-code-list' in result:
return result['iso-3166-2-code-list'][0][:2]
else:
for area in result['area-relation-list']:
if area['type'] == 'part of' and area['direction'] == 'backward':
return getCountry(area['area']['id'])
return 'Unknown'
print(getCountry("29a709d8-0320-493e-8d0c-f2c386662b7f"))
def process_artists(initial):
musicbrainzngs.set_useragent('globify', version='v1', contact=None)
artists = []
for artist in initial['items']:
name = artist['name']
artists.append(name)
data = []
for name in artists:
try:
artist = musicbrainzngs.search_artists(query= name, artist = name, limit = 1, strict=False)['artist-list'][0]
except:
add_artist(name, 'Unknown', data)
else:
if 'country' in artist:
add_artist(artist['name'], artist['country'], data)
else:
if 'area' in artist:
add_artist(artist['name'], getCountry(artist['area']['id']), data)
elif 'begin-area' in artist:
add_artist(artist['name'], getCountry(artist['begin-area']['id']), data)
elif 'end-area' in artist:
add_artist(artist['name'], getCountry(artist['end-area']['id']), data)
else:
add_artist(artist['name'], 'Unknown', data)
return data
def add_artist(artist, country, data):
for value in data:
if country == value[0]:
value[1] += 1
value[2].append(artist)
return None
data.append([country, 1, [artist]])
return None
test_input = {
'items': [
{'name': 'Queen'},
]
}
# Call the function
result = process_artists(test_input)
print(result)