Skip to content

Commit

Permalink
Ver 0.1.0.03
Browse files Browse the repository at this point in the history
Recipeをsessionから作成し、SessionAPIにPOSTしcreate(201)までのロジック作成
PostNicoSessionAsync()作成
GetContentUri()作成
MakeSession()修正
  • Loading branch information
nnn-revo2012 committed Jun 25, 2023
1 parent 84bef51 commit 261b286
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 10 deletions.
45 changes: 41 additions & 4 deletions Sirrene/DataJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public class DataJson
public bool IsPeakTime { set; get; }
public bool IsEconomy { set; get; }
public bool IsWatchVideo { set; get; }
public string Session_Url { set; get; }
public string Session_Uri { set; get; }
public string Session_Data { set; get; }
public string Content_Uri { set; get; }


public DataJson(string videoid)
Expand All @@ -51,6 +52,8 @@ public DataJson(string videoid)
this.IsEconomy = false;
this.IsWatchVideo = true;
this.Title = "";
this.Session_Uri = null;
this.Content_Uri = null;

}
public (bool result, string err) GetDataJson(JObject datajson)
Expand Down Expand Up @@ -154,11 +157,15 @@ public DataJson(string videoid)
var recipe_id = (string)session["recipeId"];
var player_id = (string)session["playerId"];
var content_id = (string)session["contentId"];
var lifetime = session["heartbeatLifetime"].ToString();
var timeout = session["contentKeyTimeout"].ToString();
var priority = session["priority"].ToString();
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();
this.Session_Uri = session["urls"][0]["url"].ToString();

sb.Append("{");
sb.Append(" \"session\": {");
Expand All @@ -184,7 +191,7 @@ public DataJson(string videoid)
sb.Append(" \"timing_constraint\": \"unlimited\",");
sb.Append(" \"keep_method\": {");
sb.Append(" \"heartbeat\": {");
sb.Append(" \"lifetime\": 120000");
sb.Append(" \"lifetime\": " + lifetime);
sb.Append(" }");
sb.Append(" },");
sb.Append(" \"protocol\": {");
Expand All @@ -211,18 +218,20 @@ public DataJson(string videoid)
sb.Append(" },");
sb.Append(" \"content_auth\": {");
sb.Append(" \"auth_type\": \"ht2\",");
sb.Append(" \"content_key_timeout\": 600000,");
sb.Append(" \"content_key_timeout\": " + timeout + ",");
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(" \"priority\": "+ priority);
sb.Append(" }");
sb.Append("}");

result = sb.ToString();
var obj = JsonConvert.DeserializeObject(result);
result = JsonConvert.SerializeObject(obj, Formatting.None);
}
catch (Exception Ex) //その他のエラー
{
Expand All @@ -234,6 +243,34 @@ public DataJson(string videoid)
return (result, err);
}

public (bool result, string err) GetContentUri(JObject session_json)
{
var result = false;
var err = "";
this.Content_Uri = null;

try
{
if (session_json["data"] != null)
{
this.Content_Uri= (string)session_json["data"]["session"]["content_uri"];
result = true;
}
else
{
err = "content_uri not found.";
}
}
catch (Exception Ex) //その他のエラー
{
DebugWrite.Writeln(nameof(GetContentUri), Ex);
err = Ex.Message;
return (result, err);
}
return (result, err);
}


//指定フォーマットに基づいて録画サブディレクトリー名を作る
public string SetRecFolderFormat(string s)
{
Expand Down
39 changes: 35 additions & 4 deletions Sirrene/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public partial class Form1 : Form
private RecHtml _rHtml = null; //RecHtml
private NicoDb _ndb = null; //NicoDb

//private DataApiDataInfo dadi = null; //動画情報
private JObject dataJson = null; //動画情報(JObject)
//private DataApiDataInfo dadi = null; //動画情報
private JObject dataJson = null; //動画情報(JObject)
private JObject sessionJson = null; //セッション情報(JObject)
private ExecPsInfo epi = null; //実行/保存ファイル情報
private RetryInfo rti = null; //リトライ情報
private CookieContainer cookiecontainer = new CookieContainer();
Expand Down Expand Up @@ -331,8 +332,7 @@ public async void StartRec()
}
*/

// Sessionからcontent_uriを取得
// Sessionを作成
// Session作成
String session = "";
(session, err) = djs.MakeSession(dataJson);
if (!string.IsNullOrEmpty(session))
Expand All @@ -344,6 +344,37 @@ public async void StartRec()
}
AddSession(JObject.Parse(session).ToString());
}
// SessionをapiにPOST
AddLog("Send PostSession", 1);
using (var _nvn = new NicoVideoNet())
{
_nvn.SetCookieContainer(cookiecontainer);
//(_, err, neterr) = await _nvn.GetNicoCrossDomainAsync(djs.Session_Url);
(sessionJson, err, neterr) = await _nvn.PostNicoSessionAsync(djs.Session_Uri, session);
}
if (err != null)
{
AddLog("Send PostSession Error: " + err + "(" + neterr + ")", 1);
}
else
{
if (sessionJson["meta"] != null)
{
var msg = (string)sessionJson["meta"]["message"] +
"(" + sessionJson["meta"]["status"].ToString() + ")";
AddLog("Send PostSession " + msg, 1);
}
}
AddSession("\r\n" + sessionJson.ToString());
(flg, err) = djs.GetContentUri(sessionJson);
if (flg)
{
AddLog("Content_Uri: " + djs.Content_Uri, 1);
}
else
{
AddLog("Content_Uri: " + err, 1);
}

var ri = new RetryInfo();
rti = ri;
Expand Down
97 changes: 97 additions & 0 deletions Sirrene/Net/NicoVideoNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ public static string GetNicoPageUrl(string videoID)
ps.Add("mail_tel", mail);
ps.Add("password", pass);

_wc.Headers.Add(HttpRequestHeader.ContentType, "text/html; charset=UTF-8");
_wc.Headers.Add(HttpRequestHeader.Accept, "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
_wc.Headers.Add(HttpRequestHeader.AcceptLanguage, "ja,en-US;q=0.9,en;q=0.8");

byte[] resArray = await _wc.UploadValuesTaskAsync(Props.NicoLoginUrl, ps).Timeout(_wc.timeout);
var data = System.Text.Encoding.UTF8.GetString(resArray);
flag = Regex.IsMatch(data, "user\\.login_status += +\\'login\\'", RegexOptions.Compiled) ? true : false;
Expand Down Expand Up @@ -194,6 +198,10 @@ public static string GetNicoPageUrl(string videoID)

try
{
_wc.Headers.Add(HttpRequestHeader.ContentType, "text/html; charset=UTF-8");
_wc.Headers.Add(HttpRequestHeader.Accept, "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
_wc.Headers.Add(HttpRequestHeader.AcceptLanguage, "ja,en-US;q=0.9,en;q=0.8");

var hs = await _wc.DownloadStringTaskAsync(Props.NicoDomain).Timeout(_wc.timeout);
flag = Regex.IsMatch(hs, "user\\.login_status += +\\'login\\'", RegexOptions.Compiled) ? true : false;
}
Expand Down Expand Up @@ -230,6 +238,10 @@ public static string GetNicoPageUrl(string videoID)
var nicoid = GetVideoID(nicoUrl);
if (string.IsNullOrEmpty(nicoid)) return (data, "null", neterr);

_wc.Headers.Add(HttpRequestHeader.ContentType, "text/html; charset=UTF-8");
_wc.Headers.Add(HttpRequestHeader.Accept, "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
_wc.Headers.Add(HttpRequestHeader.AcceptLanguage, "ja,en-US;q=0.9,en;q=0.8");

var hs = await _wc.DownloadStringTaskAsync(Props.NicoVideoUrl + nicoid).Timeout(_wc.timeout);
if (string.IsNullOrEmpty(hs)) return (data, "null", neterr);
var ttt = WebUtility.HtmlDecode(Regex.Match(hs, "data-api-data=\"([^\"]*)\"", RegexOptions.Compiled).Groups[1].Value);
Expand Down Expand Up @@ -264,6 +276,91 @@ public static string GetNicoPageUrl(string videoID)
return (data, err, neterr);
}

public async Task<(string data, string err, int neterr)> GetNicoCrossDomainAsync(string url)
{
string data = null;
string err = null;
int neterr = 0;
try
{
if (string.IsNullOrEmpty(url)) return (data, "null", neterr);

_wc.Headers.Add(HttpRequestHeader.ContentType, "text/html; charset=UTF-8");
_wc.Headers.Add(HttpRequestHeader.Accept, "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
_wc.Headers.Add(HttpRequestHeader.AcceptLanguage, "ja,en-US;q=0.9,en;q=0.8");

int index = url.IndexOf("/", "https://".Length);
var host_url = url.Substring(0, index);
data = await _wc.DownloadStringTaskAsync(host_url).Timeout(_wc.timeout);
if (string.IsNullOrEmpty(data)) return (data, "null", neterr);
}
catch (WebException Ex)
{
DebugWrite.WriteWebln(nameof(GetNicoCrossDomainAsync), Ex);
if (Ex.Status == WebExceptionStatus.ProtocolError)
{
HttpWebResponse errres = (HttpWebResponse)Ex.Response;
neterr = (int)errres.StatusCode;
err = neterr.ToString() + " " + errres.StatusDescription;
}
else
err = Ex.Message;
return (data, err, neterr);
}
catch (Exception Ex) //その他のエラー
{
DebugWrite.Writeln(nameof(GetNicoPageAsync), Ex);
err = Ex.Message;
return (data, err, neterr);
}

return (data, err, neterr);
}

public async Task<(JObject data, string err, int neterr)> PostNicoSessionAsync(string url, string senddata)
{
JObject data = null;
string err = null;
int neterr = 0;
try
{
if (string.IsNullOrEmpty(url)) return (data, "url is null", neterr);
if (string.IsNullOrEmpty(senddata)) return (data, "senddata is null", neterr);

_wc.Headers.Add(HttpRequestHeader.ContentType, "application/json");
_wc.Headers.Add(HttpRequestHeader.Accept, "application/json");
_wc.Headers.Add(HttpRequestHeader.AcceptLanguage, "ja,en-US;q=0.9,en;q=0.8");
_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);
if (string.IsNullOrEmpty(result)) return (data, "result is null", neterr);
data = JObject.Parse(result);
}
catch (WebException Ex)
{
DebugWrite.WriteWebln(nameof(PostNicoSessionAsync), Ex);
if (Ex.Status == WebExceptionStatus.ProtocolError)
{
HttpWebResponse errres = (HttpWebResponse)Ex.Response;
neterr = (int)errres.StatusCode;
err = neterr.ToString() + " " + errres.StatusDescription;
}
else
err = Ex.Message;
return (data, err, neterr);
}
catch (Exception Ex) //その他のエラー
{
DebugWrite.Writeln(nameof(GetNicoPageAsync), Ex);
err = Ex.Message;
return (data, err, neterr);
}

return (data, err, neterr);
}

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
Expand Down
4 changes: 2 additions & 2 deletions Sirrene/Prop/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Sirrene.Prop
{
public class Ver
{
public static readonly string Version = "0.1.0.02";
public static readonly string VerDate = "2023/06/19";
public static readonly string Version = "0.1.0.03";
public static readonly string VerDate = "2023/06/26";

public static string GetFullVersion()
{
Expand Down

0 comments on commit 261b286

Please sign in to comment.