Get favourite artist and tag for each user

This commit is contained in:
Thomas Wade 2018-09-30 11:03:48 +09:30
parent 93e7a01698
commit f00be5782f

View File

@ -42,6 +42,7 @@ from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
from collections import Counter
##### Set up NetworkX #######################################
@ -304,6 +305,21 @@ def Human_Str_To_Int(string):
return num
# Iterates through a set of track URLs and finds the modal tag and artist
def Favourite_Tag_And_Artist(track_urls, track_db):
tags = []
artists = []
for track in track_urls:
if not track in track_db.keys():
continue
tags.append(track_db[track].tag)
artists.append(track_db[track].artist)
# Return the first mode of the list, originally by Christian Witts and Rory Daulton
# Found at https://stackoverflow.com/a/10797913, accessed on 2018/09/30 at 01:14 UTC
return [Counter(tags).most_common(1)[0][0], Counter(artists).most_common(1)[0][0]]
##### Scrape ################################################
user_dict = {}
@ -411,6 +427,8 @@ for _, user in user_dict.items():
if not user.processed:
continue
favourite_tag, favourite_artist = Favourite_Tag_And_Artist(user.likes, track_dict)
graph.add_node( \
user.url_username, \
type='user', \