From a1ea7845ff301c833d9bf3a19f95f18fee8ed0d9 Mon Sep 17 00:00:00 2001 From: nnn-revo2012 Date: Tue, 13 Jun 2023 00:03:14 +0900 Subject: [PATCH] =?UTF-8?q?DataJson()=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sirrene/App.config | 2 +- Sirrene/DataJson.cs | 159 ++++++++++++++++++++++++ Sirrene/Form1.cs | 75 +++++++---- Sirrene/Net/NicoVideoNet.cs | 14 ++- Sirrene/Process/ExecConvert.cs | 12 +- Sirrene/Properties/Settings.Designer.cs | 2 +- Sirrene/Properties/Settings.settings | 2 +- Sirrene/Record/NicoDb.cs | 2 +- Sirrene/Sirrene.csproj | 1 + 9 files changed, 230 insertions(+), 39 deletions(-) create mode 100644 Sirrene/DataJson.cs diff --git a/Sirrene/App.config b/Sirrene/App.config index 820ff37..042dde2 100644 --- a/Sirrene/App.config +++ b/Sirrene/App.config @@ -26,7 +26,7 @@ rec - ?PID?-?UNAME?-?TITLE? + %id%%TITLE% normal diff --git a/Sirrene/DataJson.cs b/Sirrene/DataJson.cs new file mode 100644 index 0000000..9f8ee72 --- /dev/null +++ b/Sirrene/DataJson.cs @@ -0,0 +1,159 @@ +using System; +using System.Threading.Tasks; +using System.Text.RegularExpressions; +using System.Windows.Forms; +using System.Diagnostics; +using System.IO; +using System.Net; + +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +using Sirrene.Prop; +using Sirrene.Net; +using Sirrene.Proc; +using Sirrene.Rec; + +namespace Sirrene +{ + public class DataJson + { + public string Status { set; get; } + public string Error { set; get; } + + public string VideoId { set; get; } + public string Title { set; get; } + public string Description { set; get; } + public string Provider_Type { set; get; } + public string Provider_Name { set; get; } + public string Provider_Id { set; get; } + public string Community_Title { set; get; } + 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 DataJson(string videoid) + { + this.VideoId = videoid; + this.Status = null; + this.Error = null; + + this.IsPremium = false; + this.IsPeakTime = false; + this.IsEconomy = false; + this.IsWatchVideo = true; + this.Title = ""; + + } + public bool GetData(JObject datajson) + { + if (datajson["viewer"] != null) + { + if ((bool)datajson["viewer"]["isPremium"]) + this.IsPremium = true; + } + + if (datajson["system"] != null) + { + if ((bool)datajson["system"]["isPeakTime"]) + this.IsPeakTime = true; + } + + if (datajson["media"]["delivery"] != null) + { + if (!datajson["media"]["delivery"].HasValues) + this.IsWatchVideo = false; + } + + this.IsEconomy = this.IsPeakTime; + if (this.IsPeakTime && !this.IsPremium) + 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"]; + } + + return true; + } + + //指定フォーマットに基づいて録画サブディレクトリー名を作る + public string SetRecFolderFormat(string s) + { + return SetRecFileFormat(s); + } + + //指定フォーマットに基づいて録画ファイル名を作る + public string SetRecFileFormat(string s) + { + /* +   %LOW% →economy時 low_ +   %ID% →動画ID %LOW%がなくeconomy時 動画IDlow_ +   %id% →[動画ID] %LOW%がなくeconomy時 [動画ID]low_ +   %TITLE% →動画タイトル +   %title% →全角空白を半角空白に変えた動画タイトル +   %CAT% →(もしあれば)カテゴリータグ (属性 category="1" のタグ)(半角記号を全角化) +   %cat% →全角記号を削除した%CAT% +   %TAGn% →(n+1)番めのタグ (半角記号を全角化) +   %tagn% →全角記号を削除した%TAGn% + */ + String result = s; + 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)); + + return result; + } + + private string ReplaceWords(string s) + { + var result = s.Replace("\\", "¥"); + result = result.Replace("/", "?"); + result = result.Replace(":", ":"); + result = result.Replace("*", "*"); + result = result.Replace("?", "?"); + result = result.Replace("\"", "”"); + result = result.Replace("<", "<"); + result = result.Replace(">", ">"); + result = result.Replace("|", "|"); + + result = result.Replace(")", ")"); + result = result.Replace("(", "("); + + //result = result.Replace(" ", " "); + //result = result.Replace("\u3000", " "); + + return result; + } + } + +} diff --git a/Sirrene/Form1.cs b/Sirrene/Form1.cs index 1f312e2..c48deb5 100644 --- a/Sirrene/Form1.cs +++ b/Sirrene/Form1.cs @@ -4,6 +4,7 @@ using System.Windows.Forms; using System.Diagnostics; using System.IO; +using System.Net; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -33,6 +34,7 @@ public partial class Form1 : Form private JObject dataJson = null; //動画情報(JObject) private ExecPsInfo epi = null; //実行/保存ファイル情報 private RetryInfo rti = null; //リトライ情報 + private CookieContainer cookiecontainer = new CookieContainer(); private string videoId = null; @@ -167,6 +169,7 @@ private void Button1_Click(object sender, EventArgs e) } public async void StartRec() { + cookiecontainer = null; try { if (props.IsLogin == IsLogin.always) @@ -191,6 +194,7 @@ public async void StartRec() { //ログインしていればOK AddLog("Already logged in", 1); + cookiecontainer = _nvn.GetCookieContainer(); break; } } @@ -211,11 +215,13 @@ public async void StartRec() { AddLog("Login OK", 1); db.SetSession(alias, _nvn.GetCookieContainer()); + cookiecontainer = _nvn.GetCookieContainer(); } } else { AddLog("Already logged in", 1); + cookiecontainer = _nvn.GetCookieContainer(); } } break; @@ -235,7 +241,8 @@ public async void StartRec() AddLog("ブラウザでログインし直してください", 1); return; } -*/ + Cookie = _nvn.GetCookieContainer(); + */ break; } //switch } @@ -248,40 +255,57 @@ public async void StartRec() string err; int neterr; using (var _nvn = new NicoVideoNet()) + { + _nvn.SetCookieContainer(cookiecontainer); (dataJson, err, neterr) = await _nvn.GetNicoPageAsync(videoId); + } if (err != null) { AddLog("この動画は存在しないか、削除された可能性があります。 (" + err + ")", 1); return; } - //AddLog("Account: " + bci.AccountType, 1); AddDataJson(dataJson.ToString()); + var djs = new DataJson(videoId); + djs.GetData(dataJson); + if (djs.IsPremium) + AddLog("Premium Account", 1); + else + AddLog("Normal Account", 1); + if (djs.IsPeakTime) + AddLog("PeakTime", 1); + else + AddLog("No PeakTime", 1); + if (djs.IsEconomy) + AddLog("Economy Time", 1); + else + AddLog("No Economy Time", 1); + if (!djs.IsWatchVideo) + AddLog("Can't Watch Video", 1); + //保存ファイル名作成 epi = new ExecPsInfo(); epi.Sdir = string.IsNullOrEmpty(props.SaveDir) ? System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) : props.SaveDir; epi.Exec = GetExecFile(props.ExecFile[0]); epi.Arg = props.ExecCommand[0]; - //epi.Sfile = bci.SetRecFileFormat(props.SaveFile); - //epi.Sfolder = bci.SetRecFolderFormat(props.SaveFolder); - epi.Sfile = props.SaveFile; - epi.Sfolder = props.SaveFolder; + epi.Sfile = djs.SetRecFileFormat(props.SaveFile); + epi.Sfolder = djs.SetRecFolderFormat(props.SaveFolder); epi.Protocol = "hls"; epi.Seq = 0; - ExecPsInfo.MakeRecDir(epi); + //ExecPsInfo.MakeRecDir(epi); -/* - if (props.Protocol == Protocol.hls && props.UseExternal == UseExternal.native) + if (props.UseExternal == UseExternal.native) { var file = ExecPsInfo.GetSaveFileSqlite3(epi); - if (bci.IsTimeShift()) file += Props.TIMESHIFT; file += ".sqlite3"; epi.SaveFile = file; _ndb = new NicoDb(this, epi.SaveFile); - _ndb.CreateDbAll(); + //_ndb.CreateDbAll(); - _ndb.WriteDbKvsProps(bci.Data_Props); + //_ndb.WriteDbKvsProps(djs.Data_Props); } + AddLog("File: "+epi.SaveFile, 1); +/* //コメント情報 if (props.IsComment) { @@ -295,26 +319,23 @@ public async void StartRec() cctl = null; _nNetComment = new NicoNetComment(this, bci, cmi, _nvn, _ndb, cctl); } +*/ + + // Sessionを作成 + // Sessionを送る + // Sessionからcontent_uriを取得 var ri = new RetryInfo(); rti = ri; - rti.Count = props.Retry; - - if (props.Protocol == Protocol.hls && props.UseExternal == UseExternal.native) - _rHtml = new RecHtml(this, bci, _nNetComment, _nvn.GetCookieContainer(), _ndb, rti); - else - _eProcess = new ExecProcess(this, bci, _nNetComment, rti); - _nNetStream = new NicoNetStream(this, bci, cmi, epi, _nNetComment, _eProcess, _rHtml, rti); + rti.Count = 3; - AddLog("webSocketUrl: " + bci.WsUrl, 9); - AddLog("frontendId: " + bci.FrontEndId, 9); - //bci.FrontEndId = "90"; + //if (props.UseExternal == UseExternal.native) + // _rHtml = new RecHtml(this, djs, cookiecontainer, _ndb, rti); + //else + // _eProcess = new ExecProcess(this, djs, rti); //放送情報を表示 - DispHosoData(bci); - - //WebSocket接続開始 - _nNetStream.Connect(); + //DispHosoData(bci); //1秒おきに状態を調べて処理する start_flg = true; @@ -323,7 +344,7 @@ public async void StartRec() //await CheckStatus(); await Task.Delay(1000); } -*/ + } // try catch (Exception Ex) { diff --git a/Sirrene/Net/NicoVideoNet.cs b/Sirrene/Net/NicoVideoNet.cs index 0bea02f..ed4bfd8 100644 --- a/Sirrene/Net/NicoVideoNet.cs +++ b/Sirrene/Net/NicoVideoNet.cs @@ -99,12 +99,17 @@ public IList> GetCookieList() return result.ToList(); } - public CookieContainer GetCookieContainer() { return _wc.cookieContainer; } + public void SetCookieContainer(CookieContainer cookie) + { + if (cookie != null) + _wc.cookieContainer = cookie; + return; + } //*************** URL系 ******************* //動画URLから動画IDをゲット(sm|nm|so00000000000) @@ -229,7 +234,12 @@ public static string GetNicoPageUrl(string videoID) if (string.IsNullOrEmpty(hs)) return (data, "null", neterr); var ttt = WebUtility.HtmlDecode(Regex.Match(hs, "data-api-data=\"([^\"]*)\"", RegexOptions.Compiled).Groups[1].Value); if (string.IsNullOrEmpty(ttt)) - return (data, "Not found data-api-data", 0); + { + if (hs.IndexOf("window.NicoGoogleTagManagerDataLayer = [];") > 0) + return (data, "Not login and not found data-api-data. Need login.", 0); + else + return (data, "Not found data-api-data", 0); + } data = JObject.Parse(ttt.Replace(""", "\"")); } catch (WebException Ex) diff --git a/Sirrene/Process/ExecConvert.cs b/Sirrene/Process/ExecConvert.cs index d797306..b06b918 100644 --- a/Sirrene/Process/ExecConvert.cs +++ b/Sirrene/Process/ExecConvert.cs @@ -51,8 +51,8 @@ public override void ExecPs(string exefile, string argument) // 標準出力を受信する _ps.StartInfo.RedirectStandardOutput = true; _ps.StartInfo.RedirectStandardError = true; - _ps.OutputDataReceived += receivedPs; - _ps.ErrorDataReceived += receivedErrorPs; + _ps.OutputDataReceived += ReceivedPs; + _ps.ErrorDataReceived += ReceivedErrorPs; // 標準入力 _ps.StartInfo.RedirectStandardInput = true; @@ -86,7 +86,7 @@ public override void ExecPs(string exefile, string argument) } - private void receivedPs(object sender, DataReceivedEventArgs e) + private void ReceivedPs(object sender, DataReceivedEventArgs e) { try { @@ -98,11 +98,11 @@ private void receivedPs(object sender, DataReceivedEventArgs e) } catch (Exception Ex) { - DebugWrite.Writeln(nameof(receivedPs), Ex); + DebugWrite.Writeln(nameof(ReceivedPs), Ex); } } - private void receivedErrorPs(object sender, DataReceivedEventArgs e) + private void ReceivedErrorPs(object sender, DataReceivedEventArgs e) { try { @@ -114,7 +114,7 @@ private void receivedErrorPs(object sender, DataReceivedEventArgs e) } catch (Exception Ex) { - DebugWrite.Writeln(nameof(receivedErrorPs), Ex); + DebugWrite.Writeln(nameof(ReceivedErrorPs), Ex); } } diff --git a/Sirrene/Properties/Settings.Designer.cs b/Sirrene/Properties/Settings.Designer.cs index f104e28..2ec9bd4 100644 --- a/Sirrene/Properties/Settings.Designer.cs +++ b/Sirrene/Properties/Settings.Designer.cs @@ -85,7 +85,7 @@ public string SaveFolder { [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("?PID?-?UNAME?-?TITLE?")] + [global::System.Configuration.DefaultSettingValueAttribute("%id%%TITLE%")] public string SaveFile { get { return ((string)(this["SaveFile"])); diff --git a/Sirrene/Properties/Settings.settings b/Sirrene/Properties/Settings.settings index 99ee23c..1cfd189 100644 --- a/Sirrene/Properties/Settings.settings +++ b/Sirrene/Properties/Settings.settings @@ -18,7 +18,7 @@ rec - ?PID?-?UNAME?-?TITLE? + %id%%TITLE% normal diff --git a/Sirrene/Record/NicoDb.cs b/Sirrene/Record/NicoDb.cs index 74990a9..ec122be 100644 --- a/Sirrene/Record/NicoDb.cs +++ b/Sirrene/Record/NicoDb.cs @@ -67,7 +67,7 @@ public void CreateDbAll() { CreateDbMedia(); //CreateDbComment(); - CreateDbKvs(); + //CreateDbKvs(); } public void CreateDbMedia() diff --git a/Sirrene/Sirrene.csproj b/Sirrene/Sirrene.csproj index 5a09e46..24cbe9b 100644 --- a/Sirrene/Sirrene.csproj +++ b/Sirrene/Sirrene.csproj @@ -82,6 +82,7 @@ + Form