Spotify API data to vk.com user's status
up vote
2
down vote
favorite
Just a simple script that puts a user's currently playing track(on Spotify) to his account's status on vk.com. And printing info about the track in the console.
import requests
import os
import json
URL_TRACK = 'https://api.spotify.com/v1/me/player/currently-playing'
URL_STATUS = "https://api.vk.com/method/status.set"
VK_TOKEN = os.environ.get('VK_TOKEN')
SP_TOKEN = os.environ.get('SP_TOKEN')
def set_status():
params = {
'user_id': 8573490,
'v': 5.92,
'access_token': VK_TOKEN,
'text': current_track()
}
status = requests.get(url=URL_STATUS, params=params)
def track_data():
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f'Bearer {SP_TOKEN}',
}
return requests.get(url=URL_TRACK, headers=headers)
def track_is_playing():
return track_data().status_code == 200
def current_track():
data = track_data().json()
artist_path = data["item"]['artists'][0]['name']
track_path = data["item"]['name']
return(f'{artist_path} - {track_path}')
def run_script():
if track_is_playing():
set_status()
print(current_track())
else:
print('Not playing')
if __name__ == "__main__":
run_script()
python python-3.x api
add a comment |
up vote
2
down vote
favorite
Just a simple script that puts a user's currently playing track(on Spotify) to his account's status on vk.com. And printing info about the track in the console.
import requests
import os
import json
URL_TRACK = 'https://api.spotify.com/v1/me/player/currently-playing'
URL_STATUS = "https://api.vk.com/method/status.set"
VK_TOKEN = os.environ.get('VK_TOKEN')
SP_TOKEN = os.environ.get('SP_TOKEN')
def set_status():
params = {
'user_id': 8573490,
'v': 5.92,
'access_token': VK_TOKEN,
'text': current_track()
}
status = requests.get(url=URL_STATUS, params=params)
def track_data():
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f'Bearer {SP_TOKEN}',
}
return requests.get(url=URL_TRACK, headers=headers)
def track_is_playing():
return track_data().status_code == 200
def current_track():
data = track_data().json()
artist_path = data["item"]['artists'][0]['name']
track_path = data["item"]['name']
return(f'{artist_path} - {track_path}')
def run_script():
if track_is_playing():
set_status()
print(current_track())
else:
print('Not playing')
if __name__ == "__main__":
run_script()
python python-3.x api
I'm also wondering what are the options to make this thing used by many users so they don't have to deal with command-line, manual input of tokens etc. Now I could think only of web-app, written in Django for example.
– Flynn84
12 hours ago
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Just a simple script that puts a user's currently playing track(on Spotify) to his account's status on vk.com. And printing info about the track in the console.
import requests
import os
import json
URL_TRACK = 'https://api.spotify.com/v1/me/player/currently-playing'
URL_STATUS = "https://api.vk.com/method/status.set"
VK_TOKEN = os.environ.get('VK_TOKEN')
SP_TOKEN = os.environ.get('SP_TOKEN')
def set_status():
params = {
'user_id': 8573490,
'v': 5.92,
'access_token': VK_TOKEN,
'text': current_track()
}
status = requests.get(url=URL_STATUS, params=params)
def track_data():
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f'Bearer {SP_TOKEN}',
}
return requests.get(url=URL_TRACK, headers=headers)
def track_is_playing():
return track_data().status_code == 200
def current_track():
data = track_data().json()
artist_path = data["item"]['artists'][0]['name']
track_path = data["item"]['name']
return(f'{artist_path} - {track_path}')
def run_script():
if track_is_playing():
set_status()
print(current_track())
else:
print('Not playing')
if __name__ == "__main__":
run_script()
python python-3.x api
Just a simple script that puts a user's currently playing track(on Spotify) to his account's status on vk.com. And printing info about the track in the console.
import requests
import os
import json
URL_TRACK = 'https://api.spotify.com/v1/me/player/currently-playing'
URL_STATUS = "https://api.vk.com/method/status.set"
VK_TOKEN = os.environ.get('VK_TOKEN')
SP_TOKEN = os.environ.get('SP_TOKEN')
def set_status():
params = {
'user_id': 8573490,
'v': 5.92,
'access_token': VK_TOKEN,
'text': current_track()
}
status = requests.get(url=URL_STATUS, params=params)
def track_data():
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f'Bearer {SP_TOKEN}',
}
return requests.get(url=URL_TRACK, headers=headers)
def track_is_playing():
return track_data().status_code == 200
def current_track():
data = track_data().json()
artist_path = data["item"]['artists'][0]['name']
track_path = data["item"]['name']
return(f'{artist_path} - {track_path}')
def run_script():
if track_is_playing():
set_status()
print(current_track())
else:
print('Not playing')
if __name__ == "__main__":
run_script()
python python-3.x api
python python-3.x api
asked 15 hours ago
Flynn84
865
865
I'm also wondering what are the options to make this thing used by many users so they don't have to deal with command-line, manual input of tokens etc. Now I could think only of web-app, written in Django for example.
– Flynn84
12 hours ago
add a comment |
I'm also wondering what are the options to make this thing used by many users so they don't have to deal with command-line, manual input of tokens etc. Now I could think only of web-app, written in Django for example.
– Flynn84
12 hours ago
I'm also wondering what are the options to make this thing used by many users so they don't have to deal with command-line, manual input of tokens etc. Now I could think only of web-app, written in Django for example.
– Flynn84
12 hours ago
I'm also wondering what are the options to make this thing used by many users so they don't have to deal with command-line, manual input of tokens etc. Now I could think only of web-app, written in Django for example.
– Flynn84
12 hours ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
Rather than os.environ.get
you can do getenv
.
In track_data
, don't pass Content-Type
, because you aren't passing any body with your request.
Otherwise, it's quite reasonable. You may want to expand error checking and reporting, particularly for track_data
, since you aren't currently checking the status. raise_for_status
is often a good thing to do.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Rather than os.environ.get
you can do getenv
.
In track_data
, don't pass Content-Type
, because you aren't passing any body with your request.
Otherwise, it's quite reasonable. You may want to expand error checking and reporting, particularly for track_data
, since you aren't currently checking the status. raise_for_status
is often a good thing to do.
add a comment |
up vote
1
down vote
Rather than os.environ.get
you can do getenv
.
In track_data
, don't pass Content-Type
, because you aren't passing any body with your request.
Otherwise, it's quite reasonable. You may want to expand error checking and reporting, particularly for track_data
, since you aren't currently checking the status. raise_for_status
is often a good thing to do.
add a comment |
up vote
1
down vote
up vote
1
down vote
Rather than os.environ.get
you can do getenv
.
In track_data
, don't pass Content-Type
, because you aren't passing any body with your request.
Otherwise, it's quite reasonable. You may want to expand error checking and reporting, particularly for track_data
, since you aren't currently checking the status. raise_for_status
is often a good thing to do.
Rather than os.environ.get
you can do getenv
.
In track_data
, don't pass Content-Type
, because you aren't passing any body with your request.
Otherwise, it's quite reasonable. You may want to expand error checking and reporting, particularly for track_data
, since you aren't currently checking the status. raise_for_status
is often a good thing to do.
answered 9 hours ago
Reinderien
1,694616
1,694616
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209423%2fspotify-api-data-to-vk-com-users-status%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm also wondering what are the options to make this thing used by many users so they don't have to deal with command-line, manual input of tokens etc. Now I could think only of web-app, written in Django for example.
– Flynn84
12 hours ago