Guest User

Untitled

a guest
Dec 11th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. import requests
  2. import os
  3. import json
  4.  
  5.  
  6. URL_TRACK = 'https://api.spotify.com/v1/me/player/currently-playing'
  7. URL_STATUS = "https://api.vk.com/method/status.set"
  8. VK_TOKEN = os.environ.get('VK_TOKEN')
  9. SP_TOKEN = os.environ.get('SP_TOKEN')
  10.  
  11.  
  12. def set_status():
  13. params = {
  14. 'user_id': 8573490,
  15. 'v': 5.92,
  16. 'access_token': VK_TOKEN,
  17. 'text': current_track()
  18. }
  19.  
  20. status = requests.get(url=URL_STATUS, params=params)
  21.  
  22.  
  23. def track_data():
  24. headers = {
  25. 'Accept': 'application/json',
  26. 'Content-Type': 'application/json',
  27. 'Authorization': f'Bearer {SP_TOKEN}',
  28. }
  29.  
  30. return requests.get(url=URL_TRACK, headers=headers)
  31.  
  32.  
  33. def track_is_playing():
  34. return track_data().status_code == 200
  35.  
  36.  
  37. def current_track():
  38. data = track_data().json()
  39. artist_path = data["item"]['artists'][0]['name']
  40. track_path = data["item"]['name']
  41.  
  42. return(f'{artist_path} - {track_path}')
  43.  
  44.  
  45. def run_script():
  46. if track_is_playing():
  47. set_status()
  48. print(current_track())
  49. else:
  50. print('Not playing')
  51.  
  52.  
  53. if __name__ == "__main__":
  54. run_script()
Add Comment
Please, Sign In to add comment