>Daniel's Homepage

Posts

My posts about programming and things.
Date format is day/month/year because I'm sane.

listen.moe song history

10/1/2019

I had someone who listens to listen.moe complain that they had heard a song they liked but couldn't get the name of because they were driving. The website only gives you the last 3 songs played so I used their API to parse their song list and display every one that was played on a particular day in order. They found the song, and it sure is great.

getsongs.py

#!/usr/bin/python2.7
import json
import urllib2
import codecs

def request(url, header, jsonpayload):
	baseurl = "https://listen.moe/api/{}";
	req = urllib2.Request(baseurl.format(url));
	req.add_header("Content-Type", "application/json");
	req.add_header("Accept", "application/vnd.listen.v4+json");
	if header is not None:
		req.add_header(header[0], header[1]);
	response = urllib2.urlopen(req, json.dumps(jsonpayload) if jsonpayload is not None else None);
	return response;

loginpayload = {
	"username": "name",
	"password": "password"
}

def main():
	# login and get response with our token
	response = request("login", None, loginpayload);
	# extract token
	token = json.loads(response.read());
	token = token["token"];
	# request songs list
	response = request("songs", ["Authorization", "Bearer " + token], None);
	response = json.loads(response.read());
	# dump song list to file
	with codecs.open("songs.json", 'w', encoding="utf-8") as f:
		json.dump(response, f, indent=2, sort_keys=True, ensure_ascii=False);
	exit();

if __name__ == "__main__":
	main();
	
parse.py
#!/usr/bin/python2.7
import json

date = "2019-01-09";
songs = [];

def main():
	infile = "songs.json";
	with open(infile) as f:
		data = json.load(f)
	for song in data["songs"]:
		if date in song["lastPlayed"]:
			songs.append(song);
	# sort songs by timestamp
	songs.sort(key=lambda x:x["lastPlayed"]);
	for song in songs:
		print("{} last played {}".format(song["title"].encode("utf-8"), song["lastPlayed"])); 

if __name__ == "__main__":
	main();
	






RSS feed
FSF member

page generated 19/11/2024 using websitegenerator in C