-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessing.py
More file actions
139 lines (94 loc) · 4.03 KB
/
processing.py
File metadata and controls
139 lines (94 loc) · 4.03 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import musicbrainzngs
import sqlite3
import country_converter as coco
import math
from countryinfo import CountryInfo
conn = sqlite3.connect('artist.db')
c = conn.cursor()
def diversity_calc(map_data):
nums = []
for key, value in map_data.items():
if key != "Unknown":
nums.append(value[0])
total = sum(nums)
probs = [num / total for num in nums]
H = -sum(p*math.log2(p) for p in probs if p > 0)
k = len(nums)
H_norm = H / math.log2(k) if k > 1 else 0
return round(H_norm, 3)
def get_country(id):
result = musicbrainzngs.get_area_by_id(id, includes=['area-rels'])['area']
if 'iso-3166-2-code-list' in result:
return result['iso-3166-2-code-list'][0][:2]
elif result['type'] == 'Country':
return result['name']
else:
for area in result['area-relation-list']:
if area['type'] == 'part of' and area['direction'] == 'backward':
return get_country(area['area']['id'])
return 'Unknown'
def add_artist(artist, country, data):
if country not in data:
data[country] = [0, []]
data[country][0]+= 1
data[country][1].append(artist)
return None
def process_artists(initial):
conn = sqlite3.connect('artist.db')
c = conn.cursor()
data = []
#0 = map data
#1 = topFiveList
#2 = lowest popularity
#3 = lowest population
#4 = list of countries
musicbrainzngs.set_useragent('globify', version='v1', contact=None)
artists_data = [{'name': a['name'], 'spotify_id': a['id'], 'popularity': a["popularity"]} for a in initial['items']]
map_data = {}
for i, entry in enumerate(artists_data):
name = entry['name']
spotify_id = entry['spotify_id']
c.execute("SELECT * FROM artists WHERE id = ?", (spotify_id,))
lookup = c.fetchone()
if lookup:
add_artist(name, lookup[2], map_data)
artists_data[i].update({"country": lookup[2]})
else:
found_country = 'Unknown'
try:
mb_result = musicbrainzngs.search_artists(query=name, artist=name, limit=1, strict=False)
mb_artist = mb_result['artist-list'][0]
if 'country' in mb_artist:
found_country = mb_artist['country']
elif 'area' in mb_artist:
found_country = get_country(mb_artist['area']['id'])
elif 'begin-area' in mb_artist:
found_country = get_country(mb_artist['begin-area']['id'])
elif 'end-area' in mb_artist:
found_country = get_country(mb_artist['end-area']['id'])
except (IndexError, KeyError, musicbrainzngs.WebServiceError):
pass
artists_data[i].update({"country": found_country})
add_artist(name, found_country, map_data)
c.execute("INSERT OR IGNORE INTO artists VALUES (?, ?, ?)", (spotify_id, name, found_country))
conn.commit()
top_5 = artists_data[0:5]
lowest_popularity = min(artists_data, key=lambda a: a["popularity"])
iso2 = list(map_data.keys())
country_list = coco.convert(names=iso2, to='name_short')
country_names = [item for item in country_list if item != "not found"]
lowest_population = country_names[0]
for i in country_names:
try:
if CountryInfo(i).population() < CountryInfo(lowest_population).population():
lowest_population = i
except:
lowest_population = lowest_population
conn.close()
return [map_data, top_5, lowest_popularity, lowest_population, country_names, diversity_calc(map_data)]
#0 = map data
#1 = topFiveList
#2 = lowest popularity
#3 = lowest population
#4 = list of countries
#5 = diversity