Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.49 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 apptoken;
  57.     string appver;
  58.     string user;
  59.     string pass;
  60.     string sessionkey;
  61.     APIPath npPath;
  62.     APIPath submitPath;    
  63.  
  64.     void init()
  65.     {
  66.         sessionkey = getSessionKey();
  67.     }
  68.  
  69.     /* This function also sets the path for "now playing" and
  70.      * "scrobble" submits.
  71.      */
  72.     string getSessionKey()
  73.     {
  74.         writefln("comedown.scrobbler: Last.fm username: " ~
  75.             user ~ ", password: " ~ "*".repeat(pass.length));
  76.         auto curtime = to!string(getUTCtime() / ticksPerSecond);
  77.         auto authkey = MD5Str(MD5Str(pass) ~ curtime);
  78.         writefln("curtime: " ~ curtime ~ ", authkey: " ~ authkey);
  79.         auto a = APIPath("post.audioscrobbler.com", 80, "/");
  80.         auto getdata = "?hs=true&p=1.2.1&c=" ~ apptoken ~ "&v=" ~
  81.             encodeComponent(appver) ~ "&u=" ~
  82.             encodeComponent(user) ~ "&t=" ~ curtime ~ "&a=" ~
  83.             authkey;
  84.         writefln("" ~ getdata);
  85.         auto response = submitData(a, getdata, "GET");
  86.         if(response.length == 0) {
  87.             return "";
  88.         } else {
  89.             ;
  90.         }
  91.         return "";
  92.     }
  93.  
  94.     string[] submitData(APIPath path, string request, string requestType)
  95.     {
  96.         string[] returnData;
  97.         try {
  98.             auto Socket sock = new TcpSocket(new InternetAddress(domain, port));
  99.     Stream ss        = new SocketStream(sock); 
  100.         }
  101.  
  102.         catch (Exception e) {
  103.                
  104.         }
  105.        
  106.         return [];
  107.     }
  108.  
  109.     string MD5Str(string str)
  110.     {  
  111.         ubyte[16u] digest;
  112.         MD5_CTX context;
  113.         context.start();
  114.         context.update(str);
  115.         context.finish(digest);
  116.         return digestToString(digest);
  117.     }
  118.  
  119. public:
  120.     this(string apptoken, string appver, string user, string pass)
  121.     {
  122.         this.apptoken = apptoken;
  123.         this.user = user;
  124.         this.pass = pass;
  125.         init();
  126.     }
  127.  
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement