Created
March 2, 2016 20:38
-
-
Save pinkeshbadjatiya/d16d1819f44925ba9c9c to your computer and use it in GitHub Desktop.
Lastfm scrapper + ListenBrainz scrobbler | Python
This file contains 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
#!/usr/bin/env python | |
# coding: utf-8 | |
##################################################################################################### | |
# How to use | |
# ################################################################################################### | |
# | |
# python get_and_post.py | |
# -> It will push the listens from lastfm to listenbrainz. | |
# -> Successful working with resolve issues LB-68, LB-87, LB-88 and may be more. | |
##################################################################################################### | |
from lastplayed import * | |
from post_listen import * | |
import pylast | |
# not needed | |
import string | |
import random | |
import time | |
USERNAME = 'armalcolite' | |
#r = get_recent_tracks(USERNAME) | |
#r = get_organised_list(r) | |
#print(r) | |
#for i in r: | |
# post_single(i[0].encode('utf-8'), i[1].encode('utf-8'), i[2].encode('utf-8')) | |
def id_generator(size=6, chars=string.ascii_uppercase + string.digits): | |
return ''.join(random.choice(chars) for _ in range(size)) | |
for i in range(50): | |
A = id_generator() | |
B = id_generator() | |
C = int(time.time()) | |
C += int(random.random()*100) | |
print A,B,C | |
post_single(A,B,C) |
This file contains 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
#!/usr/bin/python | |
""" | |
Show 20 last played tracks, or all the last played tracks of an artist | |
(and optionally track) | |
""" | |
from __future__ import print_function | |
import argparse | |
import sys | |
from mylast import * | |
def get_recent_tracks(username, number=-1): | |
if number != -1: | |
recent_tracks = lastfm_network.get_user(username).get_recent_tracks(limit=number) # To get limited no of listens | |
return recent_tracks | |
# if no number is given then give all tracks | |
recent_tracks = lastfm_network.get_user( | |
username).get_recent_tracks() | |
return recent_tracks | |
# | |
#def get_artist_tracks(username, artist, title): | |
# if TRACK_SEPARATOR in artist: | |
# (artist, title) = split_artist_track(artist) | |
# | |
# print("Searching Last.fm library...\r",) | |
# try: | |
# tracks = lastfm_network.get_user( | |
# username).get_artist_tracks(artist=artist) | |
# except Exception as e: | |
# sys.exit("Exception: " + str(e)) | |
# | |
# total = 0 | |
# | |
# print("\t\t\t\t\r"), # clear line | |
# if title is None: # print all | |
# for track in tracks: | |
# print_track(track) | |
# total = len(tracks) | |
# | |
# else: # print matching titles | |
# find_track = pylast.Track(artist, title, lastfm_network) | |
# for track in tracks: | |
# if str(track.track).lower() == str(find_track).lower(): | |
# print_track(track) | |
# total += 1 | |
# | |
# print("Total:", total) | |
# return total | |
# | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser( | |
description="Show 20 last played tracks, or all the last played " | |
"tracks of an artist (and optionally track)", | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
parser.add_argument( | |
'artist', nargs='?', | |
help="Artist, or 'artist - track'") | |
parser.add_argument( | |
'track', nargs='?', | |
help="Track") | |
parser.add_argument( | |
'-u', '--username', | |
help="Last.fm username") | |
parser.add_argument( | |
'-n', '--number', default=20, type=int, | |
help="Number of tracks to show (when no artist given)") | |
args = parser.parse_args() | |
if not args.username: | |
args.username = lastfm_username | |
if not args.number: | |
args.number = -1 | |
# if args.artist: | |
# text = args.username + " last played " + args.artist | |
# if args.track: | |
# text += " - " + args.track | |
# text += ":" | |
# print(text) | |
# | |
# total = get_artist_tracks(args.username, args.artist, args.track) | |
# | |
# if total == 0: | |
# # Perhaps they meant to search for a user | |
# args.username = args.artist | |
# args.artist = None | |
if not args.artist: | |
print(args.username + " last played:") | |
try: | |
recent_tracks = get_recent_tracks(args.username, args.number) | |
except pylast.WSError as e: | |
print("Error: " + str(e)) | |
tracks_list = get_organised_list(recent_tracks) | |
for i in tracks_list: | |
print(i[0],i[1],i[2]) | |
def get_organised_list(track_data): | |
l = [] | |
for trk in track_data: | |
t = str(trk.track) | |
t = t.split(' - ') | |
artist = t[0] | |
track = t[1] | |
time = trk.timestamp | |
#print(artist, track, time) | |
l.append([artist,track,time]) | |
return l | |
# End of file |
This file contains 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
#!/usr/bin/env python | |
# coding: utf-8 | |
from __future__ import print_function | |
import os | |
import pylast | |
import sys | |
# You have to have your own unique two values for API_KEY and API_SECRET | |
# Obtain yours from http://www.last.fm/api/account for Last.fm | |
try: | |
API_KEY = os.environ['LASTFM_API_KEY'] | |
API_SECRET = os.environ['LASTFM_API_SECRET'] | |
except KeyError: | |
API_KEY = '6465483896828ef7a7385a395a8f9a14' | |
API_SECRET = 'fcd64d42cb6656646a14b7327da60f01' | |
try: | |
lastfm_username = os.environ['LASTFM_USERNAME'] | |
lastfm_password_hash = os.environ['LASTFM_PASSWORD_HASH'] | |
except KeyError: | |
# In order to perform a write operation you need to authenticate yourself | |
lastfm_username = "armalcolite" | |
# You can use either use the password, or find the hash once and use that | |
lastfm_password_hash = "bf3e13254014099783347032deefd22e" | |
#print(lastfm_password_hash) | |
lastfm_network = pylast.LastFMNetwork( | |
api_key=API_KEY, api_secret=API_SECRET, | |
username=lastfm_username, password_hash=lastfm_password_hash) | |
proxy_enabled = False | |
proxy_host = 'proxy.iiit.ac.in' | |
proxy_port = 8080 | |
if proxy_enabled: | |
lastfm_network.enable_proxy(host = proxy_host, port = proxy_port) | |
# Windows cmd.exe cannot do Unicode so encode first | |
def print_it(text): | |
print(text.encode('utf-8')) | |
def unicode_track_and_timestamp(track): | |
unicode_track = unicode(str(track.track), 'utf8') | |
return track.playback_date + "\t" + unicode_track | |
def print_track(track): | |
print_it(unicode_track_and_timestamp(track)) | |
TRACK_SEPARATOR = u" - " | |
def split_artist_track(artist_track): | |
artist_track = artist_track.replace(u" – ", " - ") | |
artist_track = artist_track.replace(u"“", "\"") | |
artist_track = artist_track.replace(u"”", "\"") | |
(artist, track) = artist_track.split(TRACK_SEPARATOR) | |
artist = artist.strip() | |
track = track.strip() | |
print_it("Artist:\t\t'" + artist + "'") | |
print_it("Track:\t\t'" + track + "'") | |
# Validate | |
if len(artist) is 0 and len(track) is 0: | |
sys.exit("Error: Artist and track are blank") | |
if len(artist) is 0: | |
sys.exit("Error: Artist is blank") | |
if len(track) is 0: | |
sys.exit("Error: Track is blank") | |
return (artist, track) | |
# End of file |
This file contains 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
#!/usr/bin/python | |
# coding: utf-8 | |
import os | |
import requests | |
import json | |
ROOT_URL="https://api.listenbrainz.org" | |
USERNAME = "armalcolite" | |
AUTHORIZATION_TOKEN = "e5cbceff-f102-4d24-9a49-43435ed7a834" | |
def post_single (track_name, artist_name, timestamp):#, release_mbid, artist_mbid, recording_mbid, tags): | |
""" Post a single listen to listenbrainz """ | |
data = { | |
"listen_type": "single", | |
"payload": [ | |
{ | |
"listened_at": timestamp, | |
"track_metadata": { | |
#"additional_info": { | |
# "release_mbid": release_mbid, | |
# "artist_mbids": [ | |
# artist_mbid | |
# ], | |
# "recording_mbid": recording_mbid, | |
# "tags": tags | |
#}, | |
"artist_name": artist_name, | |
"track_name": track_name, | |
#"release_name": release_name | |
} | |
} | |
] | |
} | |
url = ROOT_URL + "/1/submit-listens" | |
headers = { | |
"Content-Type": "JSON(application/json)", | |
#"Authorization": "Authorization:" + AUTHORIZATION_TOKEN | |
"Authorization": "Authorization: e5cbceff-f102-4d24-9a49-43435ed7a834" | |
} | |
#print data | |
r = requests.post(url, data=json.dumps(data), headers=headers) | |
if r.status_code != 200: | |
print r.status_code, r.reason | |
exit(1) | |
print r.status_code | |
#return json.dumps(r.json(), indent=2) | |
def get_listens(): | |
url = ROOT_URL + "/1/user/" + USERNAME + "/listens" | |
def run_get(): | |
url = "https://api.github.com/users/pinkeshbadjatiya" | |
r = requests.get(url) | |
return json.dumps(r.json(), indent=4) | |
def run_post(): | |
url = "http://google.org/post" | |
data = {"a": 10, "b": [{"c": True, "d": False}, None]} | |
headers = {"Content-Type": "application/json"} | |
r = requests.post(url, data=json.dumps(data), headers=headers) | |
return json.dumps(r.json(), indent=4) | |
if __name__ == "__main__": | |
#t = [ "you", "just", "got", "rick rolled!"] | |
post_single("Never Gonna Up", "Rick", 1456942363) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment