From 84bef513dbe4291ca41735b9de02530ec1ccc05d Mon Sep 17 00:00:00 2001 From: nnn-revo2012 Date: Wed, 14 Jun 2023 22:37:21 +0900 Subject: [PATCH] =?UTF-8?q?Versiom=200.1.0.02=20MakeSession()=E4=BD=9C?= =?UTF-8?q?=E6=88=90=20DataJson()=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89?= =?UTF-8?q?=E3=81=AEtry...catch=E4=BD=9C=E6=88=90=20start=5Frec()=E5=86=85?= =?UTF-8?q?=E3=81=A7=E3=82=A8=E3=83=A9=E3=83=BC=E3=81=8C=E5=87=BA=E3=81=9F?= =?UTF-8?q?=E5=A0=B4=E5=90=88=E3=81=AE=E3=82=A8=E3=83=A9=E3=83=BC=E5=87=A6?= =?UTF-8?q?=E7=90=86=E5=BC=B7=E5=8C=96=20=E4=B8=AD=E6=96=AD(Abort)?= =?UTF-8?q?=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sirrene/DataJson.cs | 241 ++++++++++++++++++++++++++++++++-------- Sirrene/Form1.cs | 57 ++++++++-- Sirrene/Form1_Sub.cs | 16 ++- Sirrene/Prop/Props.cs | 5 + Sirrene/Prop/Version.cs | 4 +- 5 files changed, 265 insertions(+), 58 deletions(-) diff --git a/Sirrene/DataJson.cs b/Sirrene/DataJson.cs index 9f8ee72..d7f2605 100644 --- a/Sirrene/DataJson.cs +++ b/Sirrene/DataJson.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using System.Text; using System.Text.RegularExpressions; using System.Windows.Forms; using System.Diagnostics; @@ -31,11 +32,12 @@ public class DataJson public string Community_Id { set; get; } public string Community_Thumbnail { set; get; } public string User_Id { set; get; } - public string AccountType { set; get; } public bool IsPremium { set; get; } public bool IsPeakTime { set; get; } public bool IsEconomy { set; get; } public bool IsWatchVideo { set; get; } + public string Session_Url { set; get; } + public string Session_Data { set; get; } public DataJson(string videoid) @@ -51,42 +53,185 @@ public DataJson(string videoid) this.Title = ""; } - public bool GetData(JObject datajson) + public (bool result, string err) GetDataJson(JObject datajson) { - if (datajson["viewer"] != null) - { - if ((bool)datajson["viewer"]["isPremium"]) - this.IsPremium = true; - } + var result = false; + var err = ""; - if (datajson["system"] != null) + try { - if ((bool)datajson["system"]["isPeakTime"]) - this.IsPeakTime = true; - } + if (datajson["viewer"] != null) + { + if ((bool)datajson["viewer"]["isPremium"]) + this.IsPremium = true; + } + else + { + err = "JSON data viewer not found."; + return (result, err); + } - if (datajson["media"]["delivery"] != null) - { - if (!datajson["media"]["delivery"].HasValues) - this.IsWatchVideo = false; - } + if (datajson["system"] != null) + { + if ((bool)datajson["system"]["isPeakTime"]) + this.IsPeakTime = true; + } + else + { + err = "JSON data system not found."; + return (result, err); + } + + if (datajson["media"]["delivery"] != null) + { + if (!datajson["media"]["delivery"].HasValues) + this.IsWatchVideo = false; + } + else + { + err = "JSON data media delivery not found."; + return (result, err); + } - this.IsEconomy = this.IsPeakTime; - if (this.IsPeakTime && !this.IsPremium) - if (IsWatchVideo) - if (datajson["media"]["delivery"]["movie"] != null) + this.IsEconomy = this.IsPeakTime; + if (this.IsPeakTime) + if (this.IsPremium) { - if ((bool)(datajson["media"]["delivery"]["movie"]["audios"][0]["isAvailable"]) && - (bool)(datajson["media"]["delivery"]["movie"]["videos"][0]["isAvailable"])) - this.IsEconomy = false; + this.IsEconomy = false; } + else + { + if (IsWatchVideo) + if (datajson["media"]["delivery"]["movie"] != null) + { + if ((bool)(datajson["media"]["delivery"]["movie"]["audios"][0]["isAvailable"]) && + (bool)(datajson["media"]["delivery"]["movie"]["videos"][0]["isAvailable"])) + this.IsEconomy = false; + } + } + + if (datajson["video"] != null) + { + this.Title = (string)datajson["video"]["title"]; + } + else + { + err = "JSON data video not found."; + return (result, err); + } - if (datajson["video"] != null) + result = true; + return (result, err); + } + catch (Exception Ex) //その他のエラー { - this.Title = (string)datajson["video"]["title"]; + DebugWrite.Writeln(nameof(GetDataJson), Ex); + err = Ex.Message; + return (result, err); } + } - return true; + public (string result, string err) MakeSession(JObject datajson) + { + var result = ""; + var err = ""; + JToken session = null; + StringBuilder sb = new StringBuilder(); + + try + { + if (datajson["media"]["delivery"] != null) + { + if (datajson["media"]["delivery"].HasValues) + session = datajson["media"]["delivery"]["movie"]["session"]; + } + else + { + err = "JSON data media delivery not found."; + return (result, err); + } + + var recipe_id = (string)session["recipeId"]; + var player_id = (string)session["playerId"]; + var content_id = (string)session["contentId"]; + var token = ((string)session["token"]).Replace("\"", "\\\""); + var signature = (string)session["signature"]; + var user_id = (string)session["serviceUserId"]; + var videos = session["videos"].ToString(); + var audios = session["audios"].ToString(); + + sb.Append("{"); + sb.Append(" \"session\": {"); + sb.Append(" \"recipe_id\": \"" + recipe_id + "\","); + sb.Append(" \"content_id\": \"" + content_id + "\","); + sb.Append(" \"content_type\": \"movie\","); + sb.Append(" \"content_src_id_sets\": ["); + sb.Append(" {"); + sb.Append(" \"content_src_ids\": ["); + sb.Append(" {"); + sb.Append(" \"src_id_to_mux\": {"); + sb.Append(" \"video_src_ids\": "); + sb.Append(" " + videos); + sb.Append(" ,"); + sb.Append(" \"audio_src_ids\": "); + sb.Append(" " + audios); + sb.Append(" "); + sb.Append(" }"); + sb.Append(" }"); + sb.Append(" ]"); + sb.Append(" }"); + sb.Append(" ],"); + sb.Append(" \"timing_constraint\": \"unlimited\","); + sb.Append(" \"keep_method\": {"); + sb.Append(" \"heartbeat\": {"); + sb.Append(" \"lifetime\": 120000"); + sb.Append(" }"); + sb.Append(" },"); + sb.Append(" \"protocol\": {"); + sb.Append(" \"name\": \"http\","); + sb.Append(" \"parameters\": {"); + sb.Append(" \"http_parameters\": {"); + sb.Append(" \"parameters\": {"); + sb.Append(" \"hls_parameters\": {"); + sb.Append(" \"use_well_known_port\": \"yes\","); + sb.Append(" \"use_ssl\": \"yes\","); + sb.Append(" \"transfer_preset\": \"\","); + sb.Append(" \"segment_duration\": 6000"); + sb.Append(" }"); + sb.Append(" }"); + sb.Append(" }"); + sb.Append(" }"); + sb.Append(" },"); + sb.Append(" \"content_uri\": \"\","); + sb.Append(" \"session_operation_auth\": {"); + sb.Append(" \"session_operation_auth_by_signature\": {"); + sb.Append(" \"token\": \"" + token + "\","); + sb.Append(" \"signature\": \"" + signature + "\""); + sb.Append(" }"); + sb.Append(" },"); + sb.Append(" \"content_auth\": {"); + sb.Append(" \"auth_type\": \"ht2\","); + sb.Append(" \"content_key_timeout\": 600000,"); + sb.Append(" \"service_id\": \"nicovideo\","); + sb.Append(" \"service_user_id\": \"" + user_id + "\""); + sb.Append(" },"); + sb.Append(" \"client_info\": {"); + sb.Append(" \"player_id\": \"" + player_id + "\""); + sb.Append(" },"); + //sb.Append(" \"priority\": 0.6"); + sb.Append(" }"); + sb.Append("}"); + + result = sb.ToString(); + } + catch (Exception Ex) //その他のエラー + { + DebugWrite.Writeln(nameof(MakeSession), Ex); + err = Ex.Message; + result = sb.ToString(); + return (result, err); + } + return (result, err); } //指定フォーマットに基づいて録画サブディレクトリー名を作る @@ -109,27 +254,35 @@ public string SetRecFileFormat(string s)   %TAGn% →(n+1)番めのタグ (半角記号を全角化)   %tagn% →全角記号を削除した%TAGn% */ - String result = s; - if (result.Contains("%LOW%")) + String result = s; + try { - result = result.Replace("%LOW%", ReplaceWords("low_")); - result = result.Replace("%ID%", ReplaceWords(this.VideoId)); - result = result.Replace("%id%", "[" + ReplaceWords(this.VideoId)) + "]"; + if (result.Contains("%LOW%")) + { + result = result.Replace("%LOW%", ReplaceWords("low_")); + result = result.Replace("%ID%", ReplaceWords(this.VideoId)); + result = result.Replace("%id%", "[" + ReplaceWords(this.VideoId)) + "]"; + } + else + { + var low = ""; + if (this.IsEconomy) + low = "low_"; + result = result.Replace("%ID%", ReplaceWords(this.VideoId) + low); + result = result.Replace("%id%", "[" + ReplaceWords(this.VideoId) + "]" + low); + } + result = result.Replace("%TITLE%", ReplaceWords(this.Title)); + result = result.Replace("%title%", ReplaceWords(this.Title.Replace(" ", " "))); + //result = result.Replace("%CAT%", ReplaceWords(this.Provider_Id)); + //result = result.Replace("%cat%", ReplaceWords(this.Community_Title)); + //result = result.Replace("%TAGn%", ReplaceWords(this.Community_Id)); + //result = result.Replace("%tagn%", ReplaceWords(this.Title)); } - else + catch (Exception Ex) //その他のエラー { - var low = ""; - if (this.IsEconomy) - low = "low_"; - result = result.Replace("%ID%", ReplaceWords(this.VideoId) + low); - result = result.Replace("%id%", "[" + ReplaceWords(this.VideoId) + "]" + low); + DebugWrite.Writeln(nameof(SetRecFileFormat), Ex); + return result; } - result = result.Replace("%TITLE%", ReplaceWords(this.Title)); - result = result.Replace("%title%", ReplaceWords(this.Title.Replace(" ", " "))); - //result = result.Replace("%CAT%", ReplaceWords(this.Provider_Id)); - //result = result.Replace("%cat%", ReplaceWords(this.Community_Title)); - //result = result.Replace("%TAGn%", ReplaceWords(this.Community_Id)); - //result = result.Replace("%tagn%", ReplaceWords(this.Title)); return result; } @@ -154,6 +307,6 @@ private string ReplaceWords(string s) return result; } - } + } } diff --git a/Sirrene/Form1.cs b/Sirrene/Form1.cs index c48deb5..4d4de70 100644 --- a/Sirrene/Form1.cs +++ b/Sirrene/Form1.cs @@ -44,6 +44,7 @@ public partial class Form1 : Form private string LogFile; private string LogFile2; private string LogFile3; + private string LogFile4; public Form1(string[] args) { @@ -77,7 +78,7 @@ private void Button1_Click(object sender, EventArgs e) try { //中断処理 - if (button1.Text == "中断") + if (button1.Text == "ABORT") { if (_rHtml != null) { @@ -100,6 +101,7 @@ private void Button1_Click(object sender, EventArgs e) LogFile = null; LogFile2 = null; LogFile3 = null; + LogFile4 = null; //ニコニコに接続 ClearHosoData(); @@ -135,6 +137,7 @@ private void Button1_Click(object sender, EventArgs e) LogFile = Props.GetLogfile(save_dir, videoId); LogFile2 = Props.GetExecLogfile(save_dir, videoId); LogFile3 = Props.GetDataJsonfile(save_dir, videoId); + LogFile4 = Props.GetSessionfile(save_dir, videoId); AddLog("ダウンロード開始します。", 1); AddLog(string.Format("VideoID: {0}", videoId), 1); @@ -266,21 +269,27 @@ public async void StartRec() } AddDataJson(dataJson.ToString()); var djs = new DataJson(videoId); - djs.GetData(dataJson); + bool flg; + (flg, err) = djs.GetDataJson(dataJson); + if (!flg) + { + AddLog("Error: GetDataJson. (" + err + ")", 1); + return; + } if (djs.IsPremium) AddLog("Premium Account", 1); else AddLog("Normal Account", 1); if (djs.IsPeakTime) - AddLog("PeakTime", 1); + AddLog("PeakTime(Economy Time)", 1); else - AddLog("No PeakTime", 1); + AddLog("No PeakTime(Not Econmy Time)", 1); if (djs.IsEconomy) - AddLog("Economy Time", 1); + AddLog("エコノミー動画をダウンロードします。", 1); else - AddLog("No Economy Time", 1); + AddLog("通常動画をダウンロードします。", 1); if (!djs.IsWatchVideo) - AddLog("Can't Watch Video", 1); + AddLog("動画がダウンロードできません。", 1); //保存ファイル名作成 epi = new ExecPsInfo(); @@ -304,6 +313,7 @@ public async void StartRec() //_ndb.WriteDbKvsProps(djs.Data_Props); } AddLog("File: "+epi.SaveFile, 1); + EnableButton(false); /* //コメント情報 @@ -321,9 +331,19 @@ public async void StartRec() } */ - // Sessionを作成 - // Sessionを送る // Sessionからcontent_uriを取得 + // Sessionを作成 + String session = ""; + (session, err) = djs.MakeSession(dataJson); + if (!string.IsNullOrEmpty(session)) + { + if (err != "") + { + AddLog("Error: MakeSession. (" + err + ")", 1); + return; + } + AddSession(JObject.Parse(session).ToString()); + } var ri = new RetryInfo(); rti = ri; @@ -342,8 +362,25 @@ public async void StartRec() while (start_flg == true) { //await CheckStatus(); - await Task.Delay(1000); + await Task.Delay(5000); + start_flg = false; } + 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; } // try catch (Exception Ex) diff --git a/Sirrene/Form1_Sub.cs b/Sirrene/Form1_Sub.cs index 69529ee..e8a38fe 100644 --- a/Sirrene/Form1_Sub.cs +++ b/Sirrene/Form1_Sub.cs @@ -89,6 +89,18 @@ public void AddDataJson(string s) })); } + //sessionをファイルに書き込み + public void AddSession(string s) + { + this.Invoke(new Action(() => + { + if (props.IsLogging && LogFile4 != null) + { + System.IO.File.AppendAllText(LogFile4, s); + } + })); + } + private void ClearHosoData() { this.Invoke(new Action(() => @@ -145,14 +157,14 @@ public void EnableButton(bool flag) { this.textBox1.Enabled = true; //this.button2.Enabled = true; - this.button1.Text = "録画開始"; + this.button1.Text = "DOWNLOAD"; this.button1.Focus(); } else { this.textBox1.Enabled = false; //this.button2.Enabled = false; - this.button1.Text = "中断"; + this.button1.Text = "ABORT"; this.button1.Focus(); } })); diff --git a/Sirrene/Prop/Props.cs b/Sirrene/Prop/Props.cs index 1d8b1e5..d1f3a04 100644 --- a/Sirrene/Prop/Props.cs +++ b/Sirrene/Prop/Props.cs @@ -293,6 +293,11 @@ public static string GetDataJsonfile(string dir, string filename) var tmp = Path.GetFileNameWithoutExtension(filename) + "_dataJson_" + System.DateTime.Now.ToString("yyMMdd_HHmmss") + ".log"; return Path.Combine(dir, tmp); } + public static string GetSessionfile(string dir, string filename) + { + var tmp = Path.GetFileNameWithoutExtension(filename) + "_session_" + System.DateTime.Now.ToString("yyMMdd_HHmmss") + ".log"; + return Path.Combine(dir, tmp); + } public static string GetDirSepString() { diff --git a/Sirrene/Prop/Version.cs b/Sirrene/Prop/Version.cs index 7d5a309..ccd74f8 100644 --- a/Sirrene/Prop/Version.cs +++ b/Sirrene/Prop/Version.cs @@ -4,8 +4,8 @@ namespace Sirrene.Prop { public class Ver { - public static readonly string Version = "0.1.0.01"; - public static readonly string VerDate = "2023/05/28"; + public static readonly string Version = "0.1.0.02"; + public static readonly string VerDate = "2023/06/19"; public static string GetFullVersion() {