Created
August 11, 2013 15:32
-
-
Save Visgean/6205378 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SpotifyAPI(): | |
| def __init__(self, login_callback_func=False): | |
| self.auth_server = "play.spotify.com" | |
| self.logged_in_marker = Event() | |
| self.heartbeat_marker = Event() | |
| self.username = None | |
| self.password = None | |
| self.account_type = None | |
| self.country = None | |
| self.settings = None | |
| self.disconnecting = False | |
| self.ws = None | |
| self.ws_lock = Lock() | |
| self.seq = 0 | |
| self.cmd_callbacks = {} | |
| self.login_callback = login_callback_func | |
| self.is_logged_in = False | |
| def auth(self, username, password): | |
| if self.settings is not None: | |
| Logging.warn("You must only authenticate once per API object") | |
| return False | |
| headers = { | |
| "User-Agent": "spotify-websocket-api (Chrome/13.37 compatible-ish)", | |
| } | |
| session = requests.session() | |
| secret_payload = { | |
| "album": "http://open.spotify.com/album/2mCuMNdJkoyiXFhsQCLLqw", | |
| "song": "http://open.spotify.com/track/6JEK0CvvjDjjMUBFoXShNZ", | |
| } | |
| resp = session.get("https://" + self.auth_server + "/redirect/facebook/notification.php", params=secret_payload, headers=headers) | |
| data = resp.text | |
| rx = re.compile("\"csrftoken\":\"(.*?)\"") | |
| r = rx.search(data) | |
| if not r or len(r.groups()) < 1: | |
| Logging.error("There was a problem authenticating, no auth secret found") | |
| self.do_login_callback(False) | |
| return False | |
| secret = r.groups()[0] | |
| login_payload = { | |
| "type": "sp", | |
| "username": username, | |
| "password": password, | |
| "secret": secret, | |
| } | |
| resp = session.post("https://" + self.auth_server + "/xhr/json/auth.php", data=login_payload, headers=headers) | |
| resp_json = resp.json() | |
| if resp_json["status"] != "OK": | |
| Logging.error("There was a problem authenticating, authentication failed") | |
| self.do_login_callback(False) | |
| return False | |
| self.settings = resp.json()["config"] | |
| #Get wss settings | |
| resolver_payload = { | |
| "client": "24:0:0:" + str(self.settings["version"]) | |
| } | |
| resp = session.get('http://' + self.settings["aps"]["resolver"]["hostname"], params=resolver_payload, headers=headers) | |
| resp_json = resp.json() | |
| wss_hostname = resp_json["ap_list"][0].split(":")[0] | |
| self.settings["wss"] = "wss://" + wss_hostname + "/" | |
| return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment