Skip to content

Commit

Permalink
testing playback pausing
Browse files Browse the repository at this point in the history
  • Loading branch information
LimeGradient committed Aug 8, 2024
1 parent a0cc7b4 commit 24e4e6d
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 9 deletions.
8 changes: 5 additions & 3 deletions mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
"developer": "LimeGradient",
"description": "Spotify Integration in GD!",
"resources": {
"files": [
"resources/spotidash.access"
]
"spritesheets": {
"spotidash-sheet": [
"resources/*.png"
]
}
}
}
Binary file added resources/spotidash-button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion resources/spotidash.access

This file was deleted.

2 changes: 1 addition & 1 deletion src/Spotify/AuthWebserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void Webserver::createServer() {
CROW_ROUTE(app, "/login")([=,this](const crow::request &req, crow::response &res) {
std::string state = Webserver::generateRandomString(16);

res.redirect(fmt::format("https://accounts.spotify.com/authorize?response_type=code&client_id={}&scope=user-read-private%20user-read-email%20user-read-playback-state%20user-modify-playback-state&redirect_uri=http%3A%2F%2Flocalhost%3A18080%2Fcallback&state={}",
res.redirect(fmt::format("https://accounts.spotify.com/authorize?response_type=code&client_id={}&scope=user-read-private%20user-read-email%20user-read-playback-state%20user-modify-playback-state%20user-read-currently-playing&redirect_uri=http%3A%2F%2Flocalhost%3A18080%2Fcallback&state={}",
this->m_spotify->clientID,
state
));
Expand Down
19 changes: 18 additions & 1 deletion src/Spotify/Spotify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ void Spotify::init() {
if (web::WebResponse* res = e->getValue()) {
if (res->ok()) {
auto resJson = res->json().unwrap();
log::info("json: {}", resJson);
Mod::get()->setSavedValue<std::string>("access_token", resJson["access_token"].as_string());
Mod::get()->setSavedValue<std::string>("refresh_token", resJson["refresh_token"].as_string());
this->accessToken = resJson["access_token"].as_string();
}
} else if (e->isCancelled()) {
log::error("Error with getting the access token from refresh token");
Expand Down Expand Up @@ -62,6 +63,22 @@ void Spotify::getAccessToken(std::string code) {
this->m_webListener2.setFilter(req.post("https://accounts.spotify.com/api/token"));
}

void Spotify::pausePlayback() {
this->m_webListener.bind([=, this] (web::WebTask::Event* e) {
if (web::WebResponse* res = e->getValue()) {
if (res->ok()) {
log::info("Paused Playback");
}
} else if (e->isCancelled()) {
log::error("Error with pausing playback");
}
});

auto req = web::WebRequest();
req.header("Authorization", fmt::format("Bearer {}", this->accessToken));
this->m_webListener.setFilter(req.put("https://api.spotify.com/v1/me/player/pause"));
}

Playback* Spotify::getCurrentPlayback() {
Playback* playback = new Playback();

Expand Down
3 changes: 3 additions & 0 deletions src/Spotify/Spotify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class Spotify {
void init();
void getAccessToken(std::string code);

// control playback
void pausePlayback();

Playback* getCurrentPlayback();

bool m_isWebserverCreated;
Expand Down
28 changes: 25 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,41 @@ using namespace geode::prelude;
#include <Geode/modify/MenuLayer.hpp>

class $modify(MyMenuLayer, MenuLayer) {
struct Fields {
Spotify* m_spotify;
};

bool init() {
if (!MenuLayer::init()) {
return false;
}

Spotify* spotify = new Spotify();
spotify->init();
spotify->m_isWebserverCreated = true;
m_fields->m_spotify = new Spotify();
m_fields->m_spotify->init();
m_fields->m_spotify->m_isWebserverCreated = true;

/* Playback* currentPlayback = spotify->getCurrentPlayback();
log::info("song name: {}", currentPlayback->songName); */

auto loginButton = CCMenuItemSpriteExtra::create(
CCSprite::createWithSpriteFrameName("spotidash-button.png"_spr),
this,
menu_selector(MyMenuLayer::onLoginButton)
);

auto menu = this->getChildByID("bottom-menu");
menu->addChild(loginButton);

return true;
}

void onLoginButton(CCObject*) {
geode::createQuickPopup("Authorize?", "This will redirect you to Spotify's authorization screen.", "Cancel", "Ok", [this](auto, bool btn2) {
if (btn2) {
web::openLinkInBrowser("http://localhost:18080/login");
} else {
m_fields->m_spotify->pausePlayback();
}
});
}
};

0 comments on commit 24e4e6d

Please sign in to comment.