Skip to content

Commit

Permalink
DLできない動画はsession送らず終了
Browse files Browse the repository at this point in the history
暗号化動画メッセージ表示
Heartbeat送信ロジック作成
  • Loading branch information
nnn-revo2012 committed Jun 26, 2023
1 parent 261b286 commit 5e41bd0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
32 changes: 26 additions & 6 deletions Sirrene/DataJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ public class DataJson
public bool IsPremium { set; get; }
public bool IsPeakTime { set; get; }
public bool IsEconomy { set; get; }
public bool IsEncrypt { set; get; }
public bool IsWatchVideo { set; get; }
public string Session_Uri { set; get; }
public string Session_Data { set; get; }
public string Content_Uri { set; get; }

public string Heartbeat_Uri { set; get; }
public string Heartbeat_Data { set; get; }

public DataJson(string videoid)
{
Expand All @@ -50,16 +51,19 @@ public DataJson(string videoid)
this.IsPremium = false;
this.IsPeakTime = false;
this.IsEconomy = false;
this.IsEncrypt = false;
this.IsWatchVideo = true;
this.Title = "";
this.Session_Uri = null;
this.Content_Uri = null;
this.Heartbeat_Uri = null;

}
public (bool result, string err) GetDataJson(JObject datajson)
{
var result = false;
var err = "";
JToken delivery = null;

try
{
Expand Down Expand Up @@ -87,7 +91,9 @@ public DataJson(string videoid)

if (datajson["media"]["delivery"] != null)
{
if (!datajson["media"]["delivery"].HasValues)
if (datajson["media"]["delivery"].HasValues)
delivery = datajson["media"]["delivery"];
else
this.IsWatchVideo = false;
}
else
Expand All @@ -105,14 +111,22 @@ public DataJson(string videoid)
else
{
if (IsWatchVideo)
if (datajson["media"]["delivery"]["movie"] != null)
if (delivery["movie"] != null)
{
if ((bool)(datajson["media"]["delivery"]["movie"]["audios"][0]["isAvailable"]) &&
(bool)(datajson["media"]["delivery"]["movie"]["videos"][0]["isAvailable"]))
if ((bool)(delivery["movie"]["audios"][0]["isAvailable"]) &&
(bool)(delivery["movie"]["videos"][0]["isAvailable"]))
this.IsEconomy = false;
}
}

if (IsWatchVideo)
if (delivery["encryption"] != null &&
delivery["encryption"].HasValues)
{
this.IsEncrypt = true;
this.IsWatchVideo = false;
}

if (datajson["video"] != null)
{
this.Title = (string)datajson["video"]["title"];
Expand Down Expand Up @@ -254,6 +268,12 @@ public DataJson(string videoid)
if (session_json["data"] != null)
{
this.Content_Uri= (string)session_json["data"]["session"]["content_uri"];
this.Heartbeat_Uri = this.Session_Uri + "/" +
(string)session_json["data"]["session"]["id"] +
"?_format=json&_method=PUT";
var obj = JsonConvert.DeserializeObject(session_json["data"].ToString());
this.Heartbeat_Data = JsonConvert.SerializeObject(obj, Formatting.None);

result = true;
}
else
Expand Down
51 changes: 49 additions & 2 deletions Sirrene/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ public async void StartRec()
AddLog("エコノミー動画をダウンロードします。", 1);
else
AddLog("通常動画をダウンロードします。", 1);
if (djs.IsEncrypt)
AddLog("暗号化された動画です。", 1);
if (!djs.IsWatchVideo)
AddLog("動画がダウンロードできません。", 1);

Expand Down Expand Up @@ -332,6 +334,26 @@ public async void StartRec()
}
*/

if (!djs.IsWatchVideo)
{
if (_rHtml != null)
{
_rHtml.BreakProcess("");
}
if (_eProcess != null)
{
_eProcess.BreakProcess(epi.BreakKey);
}
if (_ndb != null)
{
_ndb.Dispose();
}
AddLog("ダウンロード終了しました。", 1);
EnableButton(true);
start_flg = false;
return;
}

// Session作成
String session = "";
(session, err) = djs.MakeSession(dataJson);
Expand All @@ -350,7 +372,7 @@ public async void StartRec()
{
_nvn.SetCookieContainer(cookiecontainer);
//(_, err, neterr) = await _nvn.GetNicoCrossDomainAsync(djs.Session_Url);
(sessionJson, err, neterr) = await _nvn.PostNicoSessionAsync(djs.Session_Uri, session);
(sessionJson, err, neterr) = await _nvn.PostNicoSessionAsync(djs.Session_Uri + "?_format=json", session);
}
if (err != null)
{
Expand All @@ -365,11 +387,13 @@ public async void StartRec()
AddLog("Send PostSession " + msg, 1);
}
}
AddSession("\r\n" + sessionJson.ToString());
AddSession("\r\nResponse:\r\n" + sessionJson.ToString());
(flg, err) = djs.GetContentUri(sessionJson);
if (flg)
{
AddLog("Content_Uri: " + djs.Content_Uri, 1);
AddLog("Heartbeat_Uri: " + djs.Heartbeat_Uri, 1);
//AddSession("\r\nHeartbeat:\r\n" + djs.Heartbeat_Data);
}
else
{
Expand All @@ -380,6 +404,29 @@ public async void StartRec()
rti = ri;
rti.Count = 3;

//ハートビートテスト
JObject dummy = null;
await Task.Delay(10000);
using (var _nvn = new NicoVideoNet())
{
_nvn.SetCookieContainer(cookiecontainer);
(dummy, err, neterr) = await _nvn.PostNicoSessionAsync(djs.Heartbeat_Uri, djs.Heartbeat_Data);
}
if (err != null)
{
AddLog("Send Heartbeat Error: " + err + "(" + neterr + ")", 1);
}
else
{
if (dummy["meta"] != null)
{
var msg = (string)dummy["meta"]["message"] +
"(" + dummy["meta"]["status"].ToString() + ")";
AddLog("Send Heartbeat " + msg, 1);
}
}


//if (props.UseExternal == UseExternal.native)
// _rHtml = new RecHtml(this, djs, cookiecontainer, _ndb, rti);
//else
Expand Down
3 changes: 1 addition & 2 deletions Sirrene/Net/NicoVideoNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,7 @@ public static string GetNicoPageUrl(string videoID)
_wc.Headers.Add("Origin", "https://www.nicovideo.jp");
_wc.Headers.Add(HttpRequestHeader.Referer, "https://www.nicovideo.jp/");

var session_url = url + "?_format=json";
var result = await _wc.UploadStringTaskAsync(session_url, "POST", senddata).Timeout(_wc.timeout);
var result = await _wc.UploadStringTaskAsync(url, "POST", senddata).Timeout(_wc.timeout);
if (string.IsNullOrEmpty(result)) return (data, "result is null", neterr);
data = JObject.Parse(result);
}
Expand Down

0 comments on commit 5e41bd0

Please sign in to comment.