Source Code for Me (s-c.me)

Allows you to paste souce code to blogs! Adapted for Twitter! Here is Search Form in case you missed your code.
Tags: TSQL, Created At: 10/24/2016 8:20:17 PMViews:

HTML view:
Copy Source | Copy HTML
  1. #!/usr/bin/env python
  2. #-*- coding: utf-8 -*-
  3. """usage: last2libre.py USER_LASTFM USER_LIBRE PASSWORD_LIBRE"""
  4. import urllib
  5. try:
  6.     from BeautifulSoup_307a import BeautifulSoup
  7. except:
  8.     print "Downloading BeautifulSoup 3.0.7a to avoid problems with other versions..."
  9.     urllib.urlretrieve(r"http://www.crummy.com/software/BeautifulSoup/download/3.x/BeautifulSoup-3.0.7a.py", \
  10.                     "BeautifulSoup_307a.py")
  11.     print "Done!"
  12.     from BeautifulSoup_307a import BeautifulSoup
  13.  
  14. import sys
  15. import time
  16. import codecs
  17. import urllib2
  18. import re
  19.  
  20. sys.stdout = codecs.lookup('utf-8')[-1](sys.stdout)
  21.  
  22. def get_count(user):
  23.     url = 'http://last.fm/user/%s' % user
  24.     page = urllib2.urlopen(url).read()
  25.     page = page.split("<span class=\"count\">")[1].split("&nbsp")[0]
  26.     return int(page.replace("<span class=\"flip\">","").replace("</span>","").split("&nbsp")[0])
  27.  
  28. def parse_page(page):
  29.     """Parse a page of recently listened tracks and return a list."""
  30.     soup = BeautifulSoup(urllib2.urlopen(page), convertEntities=BeautifulSoup.HTML_ENTITIES)
  31.     for row in soup.find('table', 'candyStriped tracklist').findAll('tr'):
  32.         artist, track, timestamp = parse_track(row)
  33.         # Tracks submitted before 2005 have no timestamp
  34.         if artist and track:
  35.             yield (artist, track, timestamp)
  36.  
  37.  
  38. def parse_track(row):
  39.     """Return a tuple containing track data."""
  40.     try:
  41.         track_info = row.find('td', 'subjectCell')
  42.         artist, track = track_info.findAll('a')
  43.         timestamp = row.find('abbr')['title']
  44.         artist = artist.contents[0].strip()
  45.         track = track.contents[0].strip()
  46.         return (artist, track, timestamp)
  47.     except:
  48.         # Parsing failed
  49.         print 'parsing failed'
  50.         return (None, None, None)
  51.  
  52.  
  53. def fetch_tracks(user, request_delay=0.5):
  54.     """Fetch all tracks from a profile page and return a list."""
  55.     url = 'http://last.fm/user/%s/tracks' % user
  56.     soup = BeautifulSoup(urllib2.urlopen(url), convertEntities=BeautifulSoup.HTML_ENTITIES)
  57.     try:
  58.         num_pages = int(soup.find('a', 'lastpage').contents[0])
  59.     except:
  60.         num_pages = 1
  61.     for cur_page in range(1, num_pages + 1):
  62.         tracks = parse_page(url + '?page=' + str(cur_page))
  63.         for artist, track, timestamp in tracks:
  64.             yield (artist, track, timestamp)
  65.         if cur_page < num_pages:
  66.             time.sleep(request_delay)
  67.  
  68. def str_progress(percent):
  69.     if percent == 0:
  70.         return "["+20*" "+"]"
  71.     if percent == 100:
  72.         return "["+20*"="+"]"
  73.     k = 20*percent/100
  74.     return "["+k*"="+">"+(20-k-1)*" "+"]"
  75.  
  76. def remove_duplicates(filename):
  77.     f = open(filename, "r")
  78.     s = f.read()
  79.     f.close()
  80.     import re
  81.     r = re.compile(r"^(.*)\n\1$",re.MULTILINE)
  82.     f = open(filename,"w+")
  83.     f.write(r.sub(r"\1",s))
  84.  
  85. def main(*args):
  86.     if len(args) == 4:
  87.         all = get_count(args[1])
  88.         print "Getting list of tracks from Last.FM..."
  89.         num = 0
  90.         # Write to file
  91.         FILENAME = "lastfm_dump.txt"
  92.         f = codecs.open(FILENAME, 'w', 'utf-8')
  93.         for artist, track, timestamp in fetch_tracks(args[1]):
  94.             f.write(u'%s\t%s\t%s\n' % (artist, track, timestamp))
  95.             num += 1
  96.             sys.stdout.write(str_progress(num*100/all)+"\r")
  97.             sys.stdout.flush()
  98.         f.close()
  99.         remove_duplicates(FILENAME)
  100.         print "Done!"+20*" "
  101.         print "Uploading tracks to Libre.FM..."
  102. #-----------------------------------------------
  103.         from datetime import datetime
  104.         import getpass
  105.         # The md5 module is deprecated since Python 2.5
  106.         try:
  107.             import hashlib
  108.             md5hash = hashlib.md5
  109.         except ImportError:
  110.             import md5
  111.             md5hash = md5.new
  112.         from optparse import OptionParser
  113.         import time
  114.         from urllib import urlencode
  115.         from urllib2 import urlopen
  116.  
  117.         username,server,data = args[2],r"http://turtle.libre.fm",FILENAME
  118.         password = args[3]
  119.  
  120.         timestamp = int(time.time())
  121.         token = md5hash(md5hash(password).hexdigest() + str(timestamp)).hexdigest()
  122.         auth_url = "%s/?hs=true&p=1.2&u=%s&t=%d&a=%s&c=imp" % (server, username,
  123.                                                        timestamp, token)
  124.         response = urlopen(auth_url).read()
  125.         lines = response.split("\n")
  126.         if lines[0] != "OK":
  127.             print "Server returned: %s" % (response,)
  128.             sys.exit()
  129.         session_id = lines[1]
  130.         submit_url = lines[3]
  131.  
  132.         def post(post_data):
  133.             post_data.append(('s', session_id))
  134.             response = urlopen(submit_url, urlencode(post_data)).read()
  135.             if response != "OK\n":
  136.                 print"Server returned: %s" % (response,)
  137.                 sys.exit()
  138.  
  139.         i = 0
  140.         total = 0
  141.         post_data = []
  142.         for line in file(data):
  143.             artist,track,timestamp = line.strip().split("\t")
  144.             dt = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%SZ")
  145.             timestamp = str(int(time.mktime(dt.utctimetuple())))
  146.             post_data += [('a[%d]' % i, artist), ('t[%d]' % i, track),
  147.                   ('i[%d]' % i, timestamp)]
  148.             i += 1
  149.             total += 1
  150.             sys.stdout.write(str_progress(total*100/all)+"\r")
  151.             sys.stdout.flush()
  152.             if i > 49:
  153.                 print "Posting..."
  154.                 post(post_data)
  155.                 i = 0
  156.                 post_data = []
  157.                 time.sleep(1)
  158.  
  159.         if len(post_data) > 0:
  160.             post(post_data)
  161.         print "Done! Enjoy Libre.FM :-)"
  162.     else:
  163.         print __doc__
  164.  
  165.  
  166. if __name__ == '__main__':
  167.     sys.exit(main(*sys.argv))
  168.  

Based on Manoli.Net's CodeFormatter. Made by Topbot (c) 2008-2017