Last active
June 15, 2018 00:43
-
-
Save Shiroizu/9da3dca06cc45e4363e305a92266cfb6 to your computer and use it in GitHub Desktop.
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
import requests | |
import time | |
import os | |
# --- # --- # --- # --- # | |
userID1 = 1 # replace | |
userID2 = 2 # replace | |
# --- # --- # --- # --- # | |
if not os.path.exists("/cache"): | |
os.mkdir("/cache") | |
def get_followed_artists_by_user(userID): | |
try: | |
with open(f"cache/{userID}.txt") as f: | |
return f.read().splitlines() | |
except FileNotFoundError: | |
url = f"https://vocadb.net/api/users/{userID}/followedArtists" | |
artists = [] | |
index = 0 | |
page = 0 | |
totalcount_request = requests.get(url + "?getTotalCount=true") | |
totalcount = int(totalcount_request.json()["totalCount"]) | |
url += "?maxResults=50&start=" | |
while True: | |
try: | |
r = requests.get(f"{url}{index}") | |
items = r.json()["items"] | |
if not items: | |
break | |
artists.extend(items) | |
index += 50 | |
page += 1 | |
time.sleep(1) | |
except Exception as e: | |
print(f"Error - {e}") | |
break | |
id_list = [ar["artist"]["id"] for ar in artists] | |
if len(id_list): | |
with open(f"cache/{userID}.txt", "w") as f: | |
id_list = [str(r) for r in id_list] | |
f.write("\n".join(id_list)) | |
print(f"*Cached user {userID}*") | |
return id_list | |
def compare(user1, user2): | |
match_count = 0 | |
follows_more = user1 | |
follows_less = user2 | |
if len(user2) > len(user1): | |
follows_more = user2 | |
follows_less = user1 | |
for artist_id in follows_less: | |
if artist_id in follows_more: | |
match_count += 1 | |
return match_count / len(follows_less) | |
user1_artists = get_followed_artists_by_user(userID1) | |
print(f"User {userID1} is following {len(user1_artists)} artists in total.") | |
user2_artists = get_followed_artists_by_user(userID2) | |
print(f"User {userID2} is following {len(user2_artists)} artists in total.") | |
try: | |
similarity = compare(user1_artists, user2_artists) | |
print(f"\nThe user similarity is {round(similarity * 100, 2)} %") | |
except ZeroDivisionError: | |
print("One of the two users doesn't seem to follow any artists.") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment