Created
October 26, 2013 20:46
-
-
Save GaZ3ll3/7174333 to your computer and use it in GitHub Desktop.
JIngFM CL using C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data; | |
using System.Net; | |
using System.Reflection; | |
using GLib; | |
using Gst; | |
using Gst.BasePlugins; | |
using Newtonsoft.Json; | |
using System.Diagnostics; | |
using System.Runtime.CompilerServices; | |
using System.Runtime.Remoting.Lifetime; | |
using System.Security.Policy; | |
using Atk; | |
using Gtk; | |
using Mono.Security.X509; | |
public class PlayBinPlayer { | |
private static MainLoop loop; | |
private static string[] songs; | |
private static int song_idx = 0; | |
private static PlayBin2 play; | |
public static void Main (string[] args) { | |
if (args.Length < 4) { | |
Console.WriteLine ("usage: mono QTM.exe email password key(=piano) range(=2)" ); | |
return; | |
} | |
songs = SendRequest (args[0],args[1],args[2], args[3]); | |
if (songs == null) { | |
return; | |
} | |
//songs = args; | |
Gst.Application.Init (); | |
loop = new MainLoop (); | |
play = ElementFactory.Make ("playbin2", "play") as PlayBin2; | |
if (play == null) { | |
Console.WriteLine ("error creating a playbin gstreamer object"); | |
return; | |
} | |
play.Uri = songs[song_idx++]; | |
play.Bus.AddWatch (new BusFunc (BusCb)); | |
play.SetState (State.Playing); | |
loop.Run (); | |
return; | |
} | |
private static bool BusCb (Bus bus, Message message) { | |
switch (message.Type) { | |
case Gst.MessageType.Error: | |
Enum err; | |
string msg; | |
message.ParseError (out err, out msg); | |
Console.WriteLine ("Gstreamer error: {0}", msg); | |
loop.Quit (); | |
break; | |
case Gst.MessageType.Eos: | |
if (song_idx >= songs.Length) { | |
Console.WriteLine ("Thank you, come again"); | |
loop.Quit (); | |
} else { | |
play.SetState (State.Null); | |
play.Uri = songs[song_idx++]; | |
play.SetState (State.Playing); | |
} | |
break; | |
} | |
return true; | |
} | |
public static string[] SendRequest (string sendemail, string sendpwd, string sendkey, string sendmusicrange) | |
{ | |
string create = "http://jing.fm/api/v1/sessions/create"; | |
string fetch = "http://jing.fm/api/v1/search/jing/fetch_pls"; | |
//string info = "http://jing.fm/api/v1/music/fetch_track_infos"; | |
string music = "http://jing.fm/api/v1/media/song/surl"; | |
string[] songsurl = new String[System.Convert.ToInt32 (sendmusicrange)]; | |
string[] listOfData = { | |
string.Format ("email={0}", sendemail), | |
string.Format ("pwd={0}", sendpwd) | |
}; | |
string data = String.Join ("&", listOfData); | |
string reply = null; | |
string uid = null; | |
string[] JATH = null; | |
string[] JRTH = null; | |
using (WebClient client = new WebClient ()) { | |
client.Headers.Add ("Content-Type", "application/x-www-form-urlencoded"); | |
reply = client.UploadString (create, data); | |
JATH = client.ResponseHeaders.GetValues (6); | |
JRTH = client.ResponseHeaders.GetValues (7); | |
} | |
//json format | |
Dictionary<string, dynamic> htmlid = JsonConvert.DeserializeObject<Dictionary<string, dynamic>> (reply); | |
try{ | |
uid = htmlid ["result"] ["usr"] ["id"]; | |
} | |
catch{ | |
uid = null; | |
} | |
if (uid == null) { | |
System.Console.WriteLine ("Token mistake."); | |
return null; | |
} | |
string[] listOfFetch = { | |
"mt=", | |
"ps="+sendmusicrange, | |
"q="+sendkey, | |
"ss=true", | |
"st=0", | |
"tid=0", | |
"u="+uid | |
}; | |
string Fetch = String.Join("&", listOfFetch); | |
using (WebClient client = new WebClient ()) { | |
client.Headers.Add ("Content-Type", "application/x-www-form-urlencoded"); | |
client.Headers.Add ("jing-a-token-header", JATH[0]); | |
client.Headers.Add ("jing-r-token-header", JRTH[0]); | |
reply = client.UploadString (fetch, Fetch); | |
} | |
//var jssfetch = new JavaScriptSerializer(); | |
//var dictfetch = jssfetch.Deserialize<Dictionary<string,dynamic>>(reply); | |
Dictionary<string, dynamic> htmlfetch = JsonConvert.DeserializeObject<Dictionary<string, dynamic>> (reply); | |
for (var iter = 0; iter < System.Convert.ToInt32(sendmusicrange); iter ++) | |
{ | |
string[] listOfMusic = { | |
"mid="+htmlfetch["result"]["items"][iter]["mid"], | |
"type=NO" | |
}; | |
string Music = String.Join ("&", listOfMusic); | |
System.Diagnostics.Debug.WriteLine ("{0}", Music); | |
using (WebClient client = new WebClient ()) { | |
client.Headers.Add ("Content-Type", "application/x-www-form-urlencoded"); | |
client.Headers.Add ("jing-a-token-header", JATH[0]); | |
client.Headers.Add ("jing-r-token-header", JRTH[0]); | |
reply = client.UploadString (music, Music); | |
} | |
Dictionary<string, string> htmlAttributes_Music = JsonConvert.DeserializeObject<Dictionary<string, string>>(reply); | |
string SourceString = htmlAttributes_Music ["result"]; | |
System.Diagnostics.Debug.WriteLine ("{0}",SourceString); | |
//return SourceString; | |
songsurl [iter] = SourceString; | |
} | |
return songsurl; | |
/*string[] listOfInfo = { | |
"tid="+tid, | |
"uid="+uid | |
}; | |
string Info = String.Join ("&", listOfInfo); | |
using (WebClient client = new WebClient ()) { | |
client.Headers.Add ("Content-Type", "application/x-www-form-urlencoded"); | |
client.Headers.Add ("jing-a-token-header", JATH[0]); | |
client.Headers.Add ("jing-r-token-header", JRTH[0]); | |
reply = client.UploadString (info, Info); | |
}*/ | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for linux only with mono.
For windows, need to install gstreamer-sharp(google ossbuild), python2.7/2.6, and mono.
And JATH/JRTH is not 6th and 7th slot in windows, they are 3rd and 4th instead., however, cannot use Mircosoft's .Net...Only worked with mono under windows.