make a best-effort guess at a youtube link for tracks
This commit is contained in:
parent
3a1f3321bf
commit
46719ac9ba
3 changed files with 42 additions and 9 deletions
|
|
@ -1,6 +1,11 @@
|
||||||
[np]
|
[np]
|
||||||
class = nowplaying.NowplayingBot
|
class = nowplaying.NowplayingBot
|
||||||
domain = ; FILL IN ;
|
domain = ; fill in ;
|
||||||
client_id = ; FILL IN ;
|
client_id = ; fill in ;
|
||||||
client_secret = ; FILL IN ;
|
client_secret = ; fill in ;
|
||||||
access_token = ; FILL IN ;
|
access_token = ; fill in ;
|
||||||
|
lastfm_api_key = ; fill in ;
|
||||||
|
lastfm_api_secret = ; fill in ;
|
||||||
|
lastfm_username = ; fill in ;
|
||||||
|
lastfm_password_hash = ; fill in ;
|
||||||
|
youtube_key = ; fill in ;
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,29 @@
|
||||||
|
import requests
|
||||||
from pylast import LastFMNetwork
|
from pylast import LastFMNetwork
|
||||||
from ananas import PineappleBot, ConfigurationError, interval
|
from ananas import PineappleBot, ConfigurationError, interval
|
||||||
|
|
||||||
|
def search_youtube(q, max_results=1, key=None):
|
||||||
|
if not key:
|
||||||
|
raise ValueError('search_youtube() requires a key to authorize with the Youtube v3 API')
|
||||||
|
|
||||||
|
r = requests.get('https://www.googleapis.com/youtube/v3/search',
|
||||||
|
params={'q': q, 'type': 'video', 'maxResults': max_results,
|
||||||
|
'part': 'snippet', 'key': key})
|
||||||
|
|
||||||
|
print(r.json())
|
||||||
|
|
||||||
|
items = []
|
||||||
|
for item in r.json()['items']:
|
||||||
|
items.append({'id': item['id']['videoId'], 'title': item['snippet']['title']})
|
||||||
|
|
||||||
|
return items
|
||||||
|
|
||||||
class NowplayingBot(PineappleBot):
|
class NowplayingBot(PineappleBot):
|
||||||
def init(self):
|
def init(self):
|
||||||
self.last_posted_track = None
|
self.last_posted_track = None
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
for k in ['lastfm_api_key', 'lastfm_api_secret', 'lastfm_username', 'lastfm_password_hash']:
|
for k in ['lastfm_api_key', 'lastfm_api_secret', 'lastfm_username', 'lastfm_password_hash', 'youtube_key']:
|
||||||
if k not in self.config:
|
if k not in self.config:
|
||||||
raise ConfigurationError(f"NowplayingBot requires a '{k}'")
|
raise ConfigurationError(f"NowplayingBot requires a '{k}'")
|
||||||
|
|
||||||
|
|
@ -28,8 +45,18 @@ class NowplayingBot(PineappleBot):
|
||||||
else:
|
else:
|
||||||
self.last_posted_track = currently_playing.__hash__()
|
self.last_posted_track = currently_playing.__hash__()
|
||||||
|
|
||||||
post = '''\
|
# make a best-effort guess at the youtube link for this track
|
||||||
#np #nowplaying {artist} - {track}'''.format(artist=currently_playing.get_artist().get_name(properly_capitalized=True),
|
yt_search = search_youtube(str(currently_playing), key=self.config.youtube_key)
|
||||||
track=currently_playing.get_title())
|
yt_link = f"https://www.youtube.com/watch?v={yt_search[0]['id']}"
|
||||||
|
|
||||||
|
# template the post
|
||||||
|
post_template = '''\
|
||||||
|
#np #nowplaying #fediplay {artist} - {track}
|
||||||
|
|
||||||
|
{yt_link}'''
|
||||||
|
|
||||||
|
post = post_template.format(artist=currently_playing.get_artist().get_name(properly_capitalized=True),
|
||||||
|
track=currently_playing.get_title(), yt_link=yt_link)
|
||||||
|
|
||||||
|
# do the thing
|
||||||
self.mastodon.toot(post)
|
self.mastodon.toot(post)
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
pylast==2.1.0
|
pylast==2.1.0
|
||||||
ananas==1.0.0b5
|
ananas==1.0.0b5
|
||||||
|
requests==2.18.4
|
||||||
Loading…
Reference in a new issue