Skip to content

Commit

Permalink
DataJson()作成
Browse files Browse the repository at this point in the history
  • Loading branch information
nnn-revo2012 committed Jun 14, 2023
1 parent f4f4a3e commit a1ea784
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Sirrene/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<value>rec</value>
</setting>
<setting name="SaveFile" serializeAs="String">
<value>?PID?-?UNAME?-?TITLE?</value>
<value>%id%%TITLE%</value>
</setting>
<setting name="QualityType" serializeAs="String">
<value>normal</value>
Expand Down
159 changes: 159 additions & 0 deletions Sirrene/DataJson.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

}
75 changes: 48 additions & 27 deletions Sirrene/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Net;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -167,6 +169,7 @@ private void Button1_Click(object sender, EventArgs e)
}
public async void StartRec()
{
cookiecontainer = null;
try
{
if (props.IsLogin == IsLogin.always)
Expand All @@ -191,6 +194,7 @@ public async void StartRec()
{
//ログインしていればOK
AddLog("Already logged in", 1);
cookiecontainer = _nvn.GetCookieContainer();
break;
}
}
Expand All @@ -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;
Expand All @@ -235,7 +241,8 @@ public async void StartRec()
AddLog("ブラウザでログインし直してください", 1);
return;
}
*/
Cookie = _nvn.GetCookieContainer();
*/
break;
} //switch
}
Expand All @@ -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)
{
Expand All @@ -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;
Expand All @@ -323,7 +344,7 @@ public async void StartRec()
//await CheckStatus();
await Task.Delay(1000);
}
*/

} // try
catch (Exception Ex)
{
Expand Down
14 changes: 12 additions & 2 deletions Sirrene/Net/NicoVideoNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,17 @@ public IList<KeyValuePair<string, string>> 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)
Expand Down Expand Up @@ -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("&quot", "\""));
}
catch (WebException Ex)
Expand Down
Loading

0 comments on commit a1ea784

Please sign in to comment.