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()









share|improve this question






















  • 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















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()









share|improve this question






















  • 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













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()









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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










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.






share|improve this answer





















    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 9 hours ago









        Reinderien

        1,694616




        1,694616






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Ellipse (mathématiques)

            Quarter-circle Tiles

            Mont Emei