2018-02-23 20:56:54 +01:00
|
|
|
import re
|
2018-02-15 21:07:26 +01:00
|
|
|
import sys
|
2018-02-15 21:02:51 +01:00
|
|
|
from time import sleep
|
|
|
|
|
2019-12-25 21:33:31 +01:00
|
|
|
import sentry_sdk
|
2018-02-15 21:02:51 +01:00
|
|
|
import spotipy
|
|
|
|
from spotipy.oauth2 import SpotifyClientCredentials
|
|
|
|
|
2018-09-17 21:18:24 +02:00
|
|
|
import config
|
2018-02-15 21:02:51 +01:00
|
|
|
from config import spotify
|
|
|
|
from models import *
|
|
|
|
|
2018-09-17 21:18:24 +02:00
|
|
|
if config.sentryDSN:
|
2019-12-25 21:33:31 +01:00
|
|
|
client = sentry_sdk.init(dsn=config.sentryDSN)
|
2018-09-17 21:18:24 +02:00
|
|
|
|
2018-02-15 21:02:51 +01:00
|
|
|
crm = SpotifyClientCredentials(**spotify)
|
|
|
|
sp = spotipy.Spotify(client_credentials_manager=crm)
|
|
|
|
|
2018-02-15 21:07:26 +01:00
|
|
|
if len(sys.argv) > 1:
|
2018-02-15 21:09:03 +01:00
|
|
|
limit = int(sys.argv[1])
|
2018-02-15 21:07:26 +01:00
|
|
|
else:
|
|
|
|
limit = 50
|
2018-02-15 21:02:51 +01:00
|
|
|
|
2018-02-23 20:56:54 +01:00
|
|
|
query = Song.select().where((Song.show == 0))
|
2018-02-23 21:02:23 +01:00
|
|
|
if not len(sys.argv) > 3 or sys.argv[2] != "force":
|
2018-02-23 20:56:54 +01:00
|
|
|
query = query.where(Song.spotify_data.is_null())
|
|
|
|
else:
|
2018-02-23 21:03:04 +01:00
|
|
|
starting = int(sys.argv[3])
|
2018-02-23 21:02:23 +01:00
|
|
|
print("fetching empty starting from {id}".format(id=starting))
|
|
|
|
query = query.where((Song.spotify_data == 0) & (Song.id >= starting))
|
2018-02-23 20:56:54 +01:00
|
|
|
for song in query.limit(limit):
|
|
|
|
song.title = song.title.replace("+", " ")
|
2018-02-15 21:02:51 +01:00
|
|
|
print(song.title)
|
|
|
|
if song.artist.isupper():
|
|
|
|
song.artist = song.artist.title()
|
|
|
|
if song.title.isupper():
|
|
|
|
song.title = song.title.title()
|
2018-02-17 10:27:46 +01:00
|
|
|
print(song.id)
|
2018-02-15 21:02:51 +01:00
|
|
|
sleep(0.1)
|
2018-02-23 20:56:54 +01:00
|
|
|
searchtitle = re.sub("[\(\[].*?[\)\]]", "", song.title)
|
|
|
|
searchartist = song.artist.replace("&", "").replace("Feat.", "")
|
2018-12-18 13:21:01 +01:00
|
|
|
results = sp.search(q=searchtitle + ' ' + searchartist, type='track', limit=1)
|
2018-02-15 21:02:51 +01:00
|
|
|
if len(results["tracks"]["items"]) == 0:
|
|
|
|
song.spotify_data = False
|
2018-02-23 21:00:27 +01:00
|
|
|
print("not found")
|
2018-02-15 21:02:51 +01:00
|
|
|
else:
|
2018-02-23 21:00:27 +01:00
|
|
|
print("found")
|
2018-02-15 21:02:51 +01:00
|
|
|
track = results["tracks"]["items"][0]
|
|
|
|
song.spotify_url = track["external_urls"]["spotify"]
|
|
|
|
song.preview_url = track["preview_url"]
|
|
|
|
images = track["album"]["images"]
|
2018-02-15 21:45:43 +01:00
|
|
|
if len(images):
|
|
|
|
song.image_large = images[0]["url"]
|
|
|
|
song.image_small = images[-1]["url"]
|
2018-02-15 21:02:51 +01:00
|
|
|
song.spotify_data = True
|
|
|
|
|
|
|
|
# print(song.title)
|
|
|
|
# print(track["name"])
|
|
|
|
# print(song.artist)
|
|
|
|
# print(", ".join([a["name"] for a in track["artists"]]))
|
|
|
|
|
|
|
|
song.save()
|