Created
May 1, 2017 14:35
-
-
Save weituotian/83f6a914ae3d221e8b302ca60f8c3009 to your computer and use it in GitHub Desktop.
bilibili secret key download
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.Linq; | |
using System.Text; | |
using System.Security.Cryptography; | |
using System.Text.RegularExpressions; | |
using System.Net; | |
using System.IO; | |
using System.IO.Compression; | |
namespace console | |
{ | |
internal class Program | |
{ | |
private const string UserAgent = "Mozilla / 5.0(Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"; | |
private const string AppKey = "1d8b6e7d45233436"; // "84956560bc028eb7"; //"f3bb208b3d081dc8"; // | |
private const string BiliKey = "560c52ccd288fed045859ed18bffd973"; | |
// "94aba54af9065f71de72f5508f1cd42e"; //"1c15888dc316e05a15fdd0a02ed6584f"; // | |
private static void Main(string[] args) | |
{ | |
/*testSign(); | |
return;*/ | |
Console.WriteLine("Bilibili Stream URL generation test by gdkchan"); | |
Console.WriteLine("Enter a video URL to extract the Stream:"); | |
const string url = "http://www.bilibili.com/video/av8885415/"; // Console.ReadLine(); | |
Console.WriteLine(string.Empty); | |
Console.WriteLine("Trying to download the webpage..."); | |
var html = HttpGetGZip(url); | |
var cId = Regex.Match(html, "cid=([0-9]*)").Groups[1].Value; | |
var Params = new List<string>(); | |
Params.Add("appkey=" + AppKey); | |
Params.Add("cid=" + 16776021); | |
Params.Add("otype=json"); | |
Params.Add("quality=3"); | |
Params.Add("type=mp4"); | |
Console.WriteLine("payload: " + string.Join("&", Params.ToArray())); | |
var baseSign = string.Join("&", Params.ToArray()) + BiliKey; | |
var signature = GetMd5(baseSign); | |
Console.WriteLine("sign: " + signature); | |
Params.Add("sign=" + signature); | |
var jsonurl = "http://interface.bilibili.com/playurl?" + string.Join("&", Params.ToArray()); | |
Console.WriteLine("Trying to download JSON from \"" + jsonurl + "\"..."); | |
Console.WriteLine(string.Empty); | |
var json = HttpGet(jsonurl); | |
//Extract all URLs from the JSON | |
var urLs = Regex.Matches(json, "https?://[^\"]*"); | |
if (urLs.Count > 0) | |
{ | |
Console.ForegroundColor = ConsoleColor.White; | |
Console.WriteLine("URLs:"); | |
Console.ResetColor(); | |
foreach (Match streamUrl in urLs) | |
{ | |
Console.WriteLine(streamUrl.Value); | |
} | |
} | |
else | |
{ | |
//If we couldn't extract URLs then something is broken, show the returned message | |
Console.WriteLine("Can't extract, see JSON below:" + Environment.NewLine + json); | |
} | |
Console.WriteLine(string.Empty); | |
Console.WriteLine("Press any key to exit"); | |
Console.ReadKey(); | |
} | |
private static string GenSign(string contentId) | |
{ | |
var Params = new List<string> {"cid=" + contentId, "from=miniplay", "player=1"}; | |
var baseSign = string.Join("&", Params.ToArray()) + BiliKey; | |
var signature = GetMd5(baseSign); | |
return signature; | |
} | |
#region "Hash Utils" | |
private static string GetMd5(string input) | |
{ | |
//Calculates the MD5 of a Input string | |
using (var hash = MD5.Create()) | |
{ | |
//Compute the MD5 Hash of the String on a Byte Array | |
var hashed = hash.ComputeHash(Encoding.ASCII.GetBytes(input)); | |
//Convert Byte Array to a Hex String and returns it | |
var output = new StringBuilder(); | |
foreach (var hb in hashed) output.Append(hb.ToString("X2")); | |
return output.ToString().ToLowerInvariant(); | |
} | |
} | |
#endregion | |
#region "HTTP Utils" | |
private static string HttpGetGZip(string url) | |
{ | |
//This just downloads a Webpage using HTTP Get and the Firefox User Agent | |
var request = WebRequest.Create(url); | |
((HttpWebRequest) request).UserAgent = UserAgent; | |
//Note: The page is GZip compressed, so we need to decompress it first | |
var response = request.GetResponse(); | |
using (var reader = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress)) | |
{ | |
using (var output = new MemoryStream()) | |
{ | |
var buffer = new byte[0x1000]; //4KiB Buffer | |
var length = 0; | |
while ((length = reader.Read(buffer, 0, buffer.Length)) > 0) | |
{ | |
output.Write(buffer, 0, length); | |
} | |
return Encoding.UTF8.GetString(output.ToArray()); | |
} | |
} | |
} | |
private static string HttpGet(string url) | |
{ | |
//Same thing of the function above, but without the GZip compression | |
var request = WebRequest.Create(url); | |
((HttpWebRequest) request).UserAgent = UserAgent; | |
var response = request.GetResponse(); | |
var reader = new StreamReader(response.GetResponseStream()); | |
return reader.ReadToEnd(); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment