Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.06 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.c.stdio;
  42. import std.c.string;
  43.  
  44. struct APIPath {
  45.     string host;
  46.     int port;
  47.     string path;
  48. };
  49.  
  50. class Scrobbler {
  51.  
  52. private:
  53.     Scrobble[] scrobbles;
  54.     string apptoken;
  55.     string appver;
  56.     string user;
  57.     string pass;
  58.     string sessionkey;
  59.     APIPath npPath;
  60.     APIPath submitPath;    
  61.  
  62.     void init()
  63.     {
  64.         sessionkey = getSessionKey();
  65.     }
  66.  
  67.     /* This function also sets the path for "now playing" and
  68.      * "scrobble" submits.
  69.      */
  70.     string getSessionKey()
  71.     {
  72.         writefln("comedown.scrobbler: Last.fm username: " ~
  73.             user ~ ", password: " ~ "*".repeat(pass.length));
  74.         auto curtime = to!string(getUTCtime() / ticksPerSecond);
  75.         auto authkey = MD5Str(MD5Str(pass) ~ curtime);
  76.         writefln("curtime: " ~ curtime ~ ", authkey: " ~ authkey);
  77.         auto a = APIPath("post.audioscrobbler.com", 80, "/");
  78.         auto getdata = "?hs=true&p=1.2.1&c=" ~ apptoken ~ "&v=" ~
  79.             encodeComponent(appver) ~ "&u=" ~
  80.             encodeComponent(user) ~ "&t=" ~ curtime ~ "&a=" ~
  81.             authkey;
  82.         writefln("" ~ getdata);
  83.         return "";
  84.     }
  85.  
  86.     string[] submitData(APIPath path, string request, string requestType)
  87.     {
  88.         string[] returnData;
  89.            
  90.        
  91.  
  92.         return [ "" ];
  93.     }
  94.  
  95.     string MD5Str(string str)
  96.     {  
  97.         ubyte[16u] digest;
  98.         MD5_CTX context;
  99.         context.start();
  100.         context.update(str);
  101.         context.finish(digest);
  102.         return digestToString(digest);
  103.     }
  104.  
  105. public:
  106.     this(string apptoken, string appver, string user, string pass)
  107.     {
  108.         this.apptoken = apptoken;
  109.         this.user = user;
  110.         this.pass = pass;
  111.         init();
  112.     }
  113.  
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement