Skip to content

Instantly share code, notes, and snippets.

@1337god
Last active March 5, 2020 22:16
Show Gist options
  • Select an option

  • Save 1337god/1c7f1f0711908c402e5ec9317af2fcbf to your computer and use it in GitHub Desktop.

Select an option

Save 1337god/1c7f1f0711908c402e5ec9317af2fcbf to your computer and use it in GitHub Desktop.
Last.FM to Twitter (w/ Spotify URLs)
import spotipy
import spotipy.util as util
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy.oauth2 as oauth2
import urllib.request
import json
import time
import tweepy
from TwitterCredentials import *
from SpotifyCredentials import *
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
token = util.prompt_for_user_token(username, scope, client_id=CLIENT_ID, client_secret=CLIENT_SECRET, redirect_uri=redirect_uri)
spotify = spotipy.Spotify(auth=token)
old_uts = 0
def tweet_song(tweet):
api = tweepy.API(auth)
status = api.update_status(tweet)
while True:
try:
json_data = urllib.request.urlopen("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=banebiddix&limit=1&api_key="+ LASTFM +"&format=json").read()
obj = json.loads(json_data.decode())
current_track = spotify.current_user_playing_track();
new_uts = (obj['recenttracks']['track'][1]['date'])
if new_uts != old_uts:
tweet_contents = "#NowListening to " + obj['recenttracks']['track'][0]['name'] + " by " + obj['recenttracks']['track'][0]['artist']['#text'] + "\n" + str(current_track['item']['external_urls']['spotify'])
tweet_song(tweet_contents)
else:
print("same song is playing ", obj['recenttracks']['track'][1]['date'])
old_uts = new_uts
time.sleep(60)
except:
print("no song playing")
#The authentication tokens and keys for Spotify can be found when you create an app on https://developer.spotify.com/
CLIENT_ID = "05306617e49d45bd94702b3820a7f51a"
CLIENT_SECRET = "486505a033134516b9b3a3eea77dfcbf"
scope = 'user-read-currently-playing'
username = "banebiddix"
redirect_uri = "http://localhost:8888/callback"
LASTFM = "3d4fbc2bf6ba1427fe946d76fd31ff6f"
#The authetication tokens and keys for Twitter can be found when you create an app on https://developer.twitter.com/
CONSUMER_KEY = "CeRQeOIQyBqyoLA87KdWV56xF"
CONSUMER_SECRET = "dpnlu8aCOh1iJFlqMIBbxVEQAiDcddscEPL6Zmcopqyizszo6q"
ACCESS_KEY = "3375777135-doRzD0p7JwRl2IdzJeFYeesbV339Do6gdL2TMJT"
ACCESS_SECRET = "3QX9usiIXcIXlGRvwnNsvXvUJ3BHrtmxn6ngBggVXRsUQ"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment