Guest
Public paste!

devlooplyuaorg

By: a guest | Aug 17th, 2008 | Syntax: Python | Size: 7.48 KB | Hits: 178 | Expires: Never
Copy text to clipboard
  1. #!/usr/bin/env python
  2. # devloop.lyua.org 08/2008
  3. # Trying to communicate with Anywhere.fm directly (whithout using the browser)
  4. # Current problems for decoding AMF responses
  5. # Code for developers only
  6. import urllib, urllib2, cookielib, random
  7. import sys, socket, os
  8.  
  9. # >> Please modify account information <<
  10. account={'login':'toto','password':'toto'}
  11.  
  12. cj = cookielib.LWPCookieJar()
  13.  
  14. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  15. urllib2.install_opener(opener)
  16.  
  17. # Get our PHPSESSID
  18. #agent =  {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
  19. req=urllib2.Request("http://www.anywhere.fm/")
  20. socket.setdefaulttimeout(6)
  21. try:
  22.   fd=urllib2.urlopen(req)
  23. except IOError:
  24.   print "Error getting url"
  25.   sys.exit(1)
  26.  
  27. # >> Used only for tracking, commented <<
  28. # Get our __qca and generate __qcb cookies
  29. #req=urllib2.Request("http://edge.quantserve.com/quant.js")
  30. #try:
  31. #  fd=urllib2.urlopen(req)
  32. #  htmlSource=fd.read()
  33. #  dc=htmlSource.split("var dc=\"")[1].split('"')[0]
  34. #except IOError:
  35. #  print "Error getting url"
  36. #  sys.exit(1)
  37. #except socket.timeout:
  38. #  print "timeout error"
  39. #  sys.exit(1)
  40. #
  41. #exp=cj._cookies['.quantserve.com']['/']['uid'].expires
  42. #print "__qca =",dc,"- expires =",exp
  43. #cj.set_cookie(cookielib.Cookie(version=0, name='__qca', value=dc, port=None, port_specified=False, domain='.anywhere.fm', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=exp, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False))
  44. #
  45. #qcb=str(int(random.random()*2147483647))
  46. #print "__qcb =",qcb
  47. #cj.set_cookie(cookielib.Cookie(version=0, name='__qcb', value=qcb, port=None, port_specified=False, domain='.anywhere.fm', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=exp, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False))
  48.  
  49.  
  50. # Hardcoded initial Unique Request ID : works! :)
  51. urid=1218964000
  52. # Open a session with account information
  53. req = urllib2.Request("http://music.anywhere.fm/account/login?_unique_request_id_="+str(urid),urllib.urlencode(account))
  54. try:
  55.   fd=urllib2.urlopen(req)
  56.   htmlSource=fd.read()
  57. except IOError:
  58.   print "Error getting url"
  59.   sys.exit(1)
  60. except socket.timeout:
  61.   print "timeout error"
  62.   sys.exit(1)
  63.  
  64. if htmlSource.strip()!="<success>true</success>":
  65.   print "Warning: Connexion error! Check the credentials!"
  66.   print "Using default user_id (demo)"
  67. else:
  68.   print "Connexion successfull"
  69.  
  70. urid+=1
  71. # Get our user_id
  72. req=urllib2.Request("http://music.anywhere.fm/account/get_current_user?_unique_request_id_="+str(urid))
  73. try:
  74.   fd=urllib2.urlopen(req)
  75.   htmlSource=fd.read()
  76. except IOError:
  77.   print "Error getting url"
  78.   sys.exit(1)
  79. except socket.timeout:
  80.   print "timeout error"
  81.   sys.exit(1)
  82.  
  83. user_id=[q.strip() for q in htmlSource.split("\n") if q.find("user_id")>=0][0].split('>')[1].split('<')[0]
  84. print "user_id =",user_id
  85.  
  86. # ! required for AMF queries, don't remove !
  87. session_id=cj._cookies['music.anywhere.fm']['/']['auth_session_id'].value
  88.  
  89. # >> tracker_id is defined but not used, commented <<
  90. #urid+=1
  91. # Get our tracker_id
  92. #tab={'session_id':session_id,'user_id':user_id}
  93. #req = urllib2.Request("http://music.anywhere.fm/account/create_login_tracker?_unique_request_id_="+str(urid),urllib.urlencode(tab))
  94. #try:
  95. #  fd=urllib2.urlopen(req)
  96. #  htmlSource=fd.read()
  97. #except IOError:
  98. #  print "Error getting url"
  99. #  sys.exit(1)
  100. #except socket.timeout:
  101. #  print "timeout error"
  102. #  sys.exit(1)
  103. #
  104. #tracker_id=htmlSource.split(">")[1].split("<")[0]
  105. #print "tracker_id =",tracker_id
  106.  
  107. # Now let's go AMF :p
  108. # feel free to modify user_id here for study, for example to 46 (=free music)
  109. #user_id=46
  110. import pyamf
  111. from pyamf.remoting import client
  112. from pyamf.flex import messaging
  113. from pyamf import remoting
  114. from pyamf import util
  115. import uuid
  116.  
  117. urid+=1
  118. # Get a list of playlists + informations about account, friends etc
  119. # This is not necessary as we know the "Entire Playlist" is type 200 and id=user_id
  120. # but it can be useful for customisation
  121. url='http://www.anywhere.fm/amfphp/gateway.php?_unique_request_id_='+str(urid)
  122. gw = client.RemotingService(url,pyamf.AMF0,pyamf.ClientTypes.Flash9)
  123. message = messaging.RemotingMessage(operation=u'get_complete_profiles',
  124.     source=u'BlazingFast.DBQueries',
  125.     timestamp=0,
  126.     destination=u'amf',
  127.     clientId=None,
  128.     headers={u'DSEndpoint':None,u'DSId': u'nil'},
  129.     timeToLive=0,
  130.     messageId=unicode(uuid.uuid4()),
  131.     messageRefType=u'flex.messaging.messages.RemotingMessage',
  132.     body=[unicode(session_id),[user_id]])
  133. gw.addRequest('null', message)
  134.  
  135. # Encode and inject
  136. print "Getting the user profile..."
  137. x=remoting.encode(gw.getAMFRequest(gw.requests)).getvalue()
  138. xamf =  {'Content-Type' : 'application/x-amf'}
  139. req=urllib2.Request(url,data=x,headers=xamf)
  140. socket.setdefaulttimeout(6)
  141. try:
  142.   fd=urllib2.urlopen(req)
  143.   htmlSource=fd.read()
  144. except IOError:
  145.   print "Error getting url"
  146.   sys.exit(1)
  147. #doesn't work for the moment, unicode error :(
  148. #liste=remoting.decode(htmlSource)
  149.  
  150. urid+=1
  151. # Get the entire playlist of the user
  152. url='http://www.anywhere.fm/amfphp/gateway.php?_unique_request_id_='+str(urid)
  153. gw = client.RemotingService(url,pyamf.AMF0,pyamf.ClientTypes.Flash9)
  154. message = messaging.RemotingMessage(operation=u'get_songs_for_user_playlists',
  155.     source=u'BlazingFast.DBQueries',
  156.     timestamp=0,
  157.     destination=u'amf',
  158.     clientId=None,
  159.     headers={u'DSEndpoint':None,u'DSId': u'nil'},
  160.     timeToLive=0,
  161.     messageId=unicode(uuid.uuid4()),
  162.     messageRefType=u'flex.messaging.messages.RemotingMessage',
  163.     body=[unicode(session_id),user_id,True,[user_id],[200],[u'0']])
  164. gw.addRequest('null', message)
  165.  
  166. # Encode and inject
  167. print "Getting the Entire Library playlist..."
  168. x=remoting.encode(gw.getAMFRequest(gw.requests)).getvalue()
  169. xamf =  {'Content-Type' : 'application/x-amf'}
  170. req=urllib2.Request(url,data=x,headers=xamf)
  171. socket.setdefaulttimeout(6)
  172. try:
  173.   fd=urllib2.urlopen(req)
  174.   htmlSource=fd.read()
  175. except IOError:
  176.   print "Error getting url"
  177.   sys.exit(1)
  178. #doesn't work for the moment, unicode error :(
  179. #liste=remoting.decode(htmlSource)
  180.  
  181. urid+=1
  182. # Get a valid url for an mp3 file (thumbs up lol)
  183. user_id=5418
  184. url='http://www.anywhere.fm/amfphp/gateway.php?_unique_request_id_='+str(urid)
  185. gw = client.RemotingService(url,pyamf.AMF0,pyamf.ClientTypes.Flash9)
  186. message = messaging.RemotingMessage(operation=u'get_song_url',
  187.     source=u'BlazingFast.DBQueries',
  188.     timestamp=0,
  189.     destination=u'amf',
  190.     clientId=None,
  191.     headers={u'DSEndpoint':None,u'DSId': u'nil'},
  192.     timeToLive=0,
  193.     messageId=unicode(uuid.uuid4()),
  194.     messageRefType=u'flex.messaging.messages.RemotingMessage',
  195.     body=[unicode(session_id),user_id,u'4722110-184833-204-44100-452',u'8113427'])
  196. gw.addRequest('null', message)
  197.  
  198. # Encode and inject
  199. print "Getting a valid music url..."
  200. x=remoting.encode(gw.getAMFRequest(gw.requests)).getvalue()
  201. xamf =  {'Content-Type' : 'application/x-amf'}
  202. req=urllib2.Request(url,data=x,headers=xamf)
  203. socket.setdefaulttimeout(6)
  204. try:
  205.   fd=urllib2.urlopen(req)
  206.   htmlSource=fd.read()
  207. except IOError:
  208.   print "Error getting url"
  209.   sys.exit(1)
  210.  
  211. # This one works (the payload is very small, url in "standard" ascii)
  212. liste=remoting.decode(htmlSource)
  213. sesame=liste['/1'].body.body['url']
  214. print "Reading url",sesame
  215. # Let's play music hax0rz !!
  216. os.system("mplayer '"+sesame+"'")
  217. #####can be useful for debugging#####
  218. #for index, cookie in enumerate(cj):
  219. #    print index,':',cookie