Guest User

spotify bot code

a guest
Jul 8th, 2021
1,619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. from datetime import datetime
  2. import spotipy
  3. from spotipy import oauth2
  4. import spotipy.util as util
  5. from spotipy.oauth2 import SpotifyClientCredentials
  6. import tweepy
  7.  
  8. from SpotifyCredentials import *
  9. from TwitterCredentials import *
  10.  
  11. now = datetime.now()
  12. dt_string = now.strftime("%H:%M")
  13.  
  14. auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
  15. auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
  16.  
  17. api = tweepy.API(auth)
  18.  
  19. token = util.prompt_for_user_token(username=username,
  20. scope=scope,
  21. client_id=CLIENT_ID,
  22. client_secret=CLIENT_SECRET,
  23. redirect_uri=redirect_uri)
  24.  
  25. spotify = spotipy.Spotify(auth=token)
  26.  
  27. while True:
  28. try:
  29. current_track = spotify.current_user_playing_track()
  30. current_track_id = current_track['item']['id']
  31.  
  32. if current_track_id is not None:
  33. api.update_status("mimi's spotify " + dt_string + ":" + '\n' + current_track['item']['artists'][0]['name'] + " - " +
  34. current_track['item']['name'] + '\n' + str(
  35. current_track['item']['external_urls']['spotify']) + '\n' + "#" + str(
  36. current_track['item']['artists'][0]['name']).replace(" ", ""))
  37. break
  38. else:
  39. continue
  40. except spotipy.client.SpotifyException:
  41. token = util.prompt_for_user_token(username=username,
  42. scope=scope,
  43. client_id=CLIENT_ID,
  44. client_secret=CLIENT_SECRET,
  45. redirect_uri=redirect_uri)
  46.  
  47. spotify = spotipy.Spotify(auth=token)
  48. except (tweepy.TweepError, TypeError) as e:
  49. pass
  50.  
  51. while True:
  52. try:
  53. new_track = spotify.current_user_playing_track()
  54. new_track_id = new_track['item']['id']
  55.  
  56. if current_track_id is not None and new_track_id != current_track_id:
  57. api.update_status("mimi's spotify " + dt_string + ":" + '\n' + new_track['item']['artists'][0]['name'] + " - " +
  58. new_track['item']['name'] + '\n' + str(
  59. new_track['item']['external_urls']['spotify']) + '\n' + "#" + str(
  60. new_track['item']['artists'][0]['name']).replace(" ", ""))
  61. current_track_id = new_track_id
  62. else:
  63. continue
  64. except spotipy.client.SpotifyException:
  65. token = util.prompt_for_user_token(username=username,
  66. scope=scope,
  67. client_id=CLIENT_ID,
  68. client_secret=CLIENT_SECRET,
  69. redirect_uri=redirect_uri)
  70.  
  71. spotify = spotipy.Spotify(auth=token)
  72. except (tweepy.TweepError, TypeError)as e:
  73. pass
  74.  
Advertisement
Add Comment
Please, Sign In to add comment