Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.77 KB | None | 0 0
  1. /* scrobbler.d: Main scrobbler class of Comedown
  2.  *
  3.  * Copyright (c) 2010 Kirn Gill <segin2005@gmail.com>
  4.  *  
  5.  * Permission is hereby granted, free of charge, to any person or organization
  6.  * obtaining a copy of the software and accompanying documentation covered by
  7.  * this license (the "Software") to use, reproduce, display, distribute,
  8.  * execute, and transmit the Software, and to prepare derivative works of the
  9.  * Software, and to permit third-parties to whom the Software is furnished to
  10.  * do so, all subject to the following:
  11.  *
  12.  * The copyright notices in the Software and this entire statement, including
  13.  * the above license grant, this restriction and the following disclaimer,
  14.  * must be included in all copies of the Software, in whole or in part, and
  15.  * all derivative works of the Software, unless such copies or derivative
  16.  * works are solely in the form of machine-executable object code generated by
  17.  * a source language processor.
  18.  *
  19.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21.  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  22.  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  23.  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  24.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25.  * DEALINGS IN THE SOFTWARE.
  26.  *
  27.  */
  28.  
  29. module comedown.scrobbler;
  30. import comedown.types.scrobble;
  31.  
  32. import std.array;
  33. import std.string;
  34. import std.xml;
  35. import std.md5;
  36. import std.uri;
  37. import std.conv;
  38. import std.date;
  39. import std.stdio;
  40. import std.string;
  41. import std.socket;
  42. import std.socketstream;
  43. import std.c.stdio;
  44. import std.c.string;
  45.  
  46. struct APIPath {
  47.     string host;
  48.     int port;
  49.     string path;
  50. };
  51.  
  52. class Scrobbler {
  53.  
  54. private:
  55.     Scrobble[] scrobbles;
  56.     string appid;
  57.     string appver;
  58.     string user;
  59.     string pass;
  60.     string status;
  61.     string sessionkey;
  62.     APIPath npPath;
  63.     APIPath submitPath;    
  64.  
  65.     void init()
  66.     {
  67.         sessionkey = getSessionKey();
  68.     }
  69.  
  70.     /* This function also sets the path for "now playing" and
  71.      * "scrobble" submits.
  72.      */
  73.     string getSessionKey()
  74.     {
  75.         writefln("comedown.scrobbler: Last.fm username: " ~
  76.             user ~ ", password: " ~ "*".repeat(pass.length));
  77.         auto curtime = to!string(getUTCtime() / ticksPerSecond);
  78.         auto authkey = MD5Str(MD5Str(pass) ~ curtime);
  79.         writefln("curtime: " ~ curtime ~ ", authkey: " ~ authkey);
  80.         auto a = APIPath("post.audioscrobbler.com", 80, "/");
  81.         auto getdata = [
  82.                 "hs" : "true",
  83.                 "p"  : "1.2.1",
  84.                 "c"  : appid,
  85.                 "v"  : appver,
  86.                 "u"  : user,
  87.                 "t"  : curtime,
  88.                 "a"  : authkey
  89.                 ];
  90.         auto response = submitData(a, getdata, "GET");
  91.         if(response.length == 0) {
  92.             return "";
  93.         } else {
  94.             ;
  95.         }
  96.         return "";
  97.     }
  98.  
  99.     string[] submitData(APIPath path, string[string] request, string requestType)
  100.     {
  101.         string[] returnData;
  102.         auto requestSerial = requestType == "GET" ? "?" : "";
  103.         foreach(requestKey, requestValue; request) {
  104.             requestSerial ~= encodeComponent(requestKey) ~ "=" ~
  105.                 encodeComponent(requestValue) ~ "&";
  106.         };
  107.  
  108.         requestSerial.popBack;
  109.  
  110.         writefln(requestSerial);
  111.        
  112.         try {
  113.             auto sock = new TcpSocket(new InternetAddress
  114.                 (path.host, cast(ushort) path.port));
  115.             auto ss = new SocketStream(sock);
  116.         }
  117.  
  118.         catch (Exception e) {
  119.             std.stdio.stderr.writefln("Exception: " ~ e.toString());
  120.         }
  121.        
  122.         return [];
  123.     }
  124.  
  125.     string MD5Str(string str)
  126.     {  
  127.         ubyte[16u] digest;
  128.         MD5_CTX context;
  129.         context.start();
  130.         context.update(str);
  131.         context.finish(digest);
  132.         return digestToString(digest);
  133.     }
  134.  
  135. public:
  136.     this(string appid, string appver, string user, string pass)
  137.     {
  138.         this.appid = appid;
  139.         this.user = user;
  140.         this.pass = pass;
  141.         init();
  142.     }
  143.  
  144.     void setUser(string user, string pass)
  145.     {
  146.         sessionkey = "";
  147.         this.user = user;
  148.         this.pass = pass;
  149.     }
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement