Skip to content

Commit

Permalink
Fix comment
Browse files Browse the repository at this point in the history
  • Loading branch information
kurema committed May 17, 2024
1 parent 5cc8246 commit ca62be8
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.pl text eol=lf
*.cgi text eol=lf
*.html text eol=lf
74 changes: 28 additions & 46 deletions nicoch/commentproxy.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use File::Basename;
require File::Spec->catfile(dirname(__FILE__),"common.pl");

print <<"HEAD";
Content-type: application/xml
Content-type: application/json
Access-Control-Allow-Origin: *
HEAD
Expand All @@ -31,61 +31,43 @@ my $login_info = {

my $q=new CGI;
my $movie_id=$q->param('id');
#my $movie_id="DEBUG";

my $ua = LWP::UserAgent->new(cookie_jar => {});
$ua->post("https://secure.nicovideo.jp/secure/login?site=niconico", $login_info);


my $info_res = $ua->get("https://www.nicovideo.jp/watch/".$movie_id);
my $info_json = scraper {
process 'div#js-initial-watch-data', 'json' => '@data-api-data'
}->scrape($info_res->content)->{json};
$info_json = unescape($info_json);
$info = decode_json( $info_json );

#my $ms= $info->{thread}->{serverUrl};
my $ms= $info->{comment}->{server}->{url};#"https://nvcomment.nicovideo.jp/legacy/api/"
#my $user_id= $info->{video}->{dmcInfo}->{user}->{user_id};
my $user_id = $info->{viewer}->{id};
my $user_key = $info->{comment}->{keys}->{userKey};
my $length = $info->{video}->{duration};
my $threads = $info->{comment}->{threads};

my $min=int($length/60)+1;
my $post_msg="<packet>\n";

foreach my $thread (@$threads){
if($thread->{isActive} == 1){
my $thread_id = $thread->{id};
if($thread->{isThreadkeyRequired} == 1){
#my $thread_key_res=$ua->get("http://flapi.nicovideo.jp/api/getthreadkey?thread=".$thread->{id});

#my $thread_key = ParseUrl($thread_key_res->content,"threadkey");
#my $force_184 = ParseUrl($thread_key_res->content,"force_184");
my $thread_key = $thread->{threadkey};
my $force_184 = $thread->{is184Forced};

$post_msg.=<<"PACKET"
<thread thread="$thread_id" version="20090904" threadkey="$thread_key" force_184="$force_184" user_id="$user_id" />
<thread_leaves scores="1" thread="$thread_id" threadkey="$thread_key" force_184="$force_184" user_id="$user_id">0-$min:100,1000</thread_leaves>
PACKET
}else{
$post_msg.=" <thread thread=\"$thread_id\" version=\"20061206\" res_from=\"-1000\" user_id=\"$user_id\" userkey=\"$user_key\" />\n";
if($thread->{isLeafRequired} == 1){
$post_msg.=" <thread_leaves scores=\"1\" thread=\"$thread_id\" user_id=\"$user_id\" userkey=\"$user_key\">0-$min:100,1000</thread_leaves>\n";
}
}
}
}

$post_msg.="</packet>\n";

my $req=HTTP::Request->new(POST => $ms);
$req->content($post_msg);
my $threadsUrl = ($info->{comment}->{nvComment}->{server})."/v1/threads";

my $post_msg=$info->{comment}->{nvComment};
delete $post_msg->{server};
my %emptyHash=();
$post_msg->{additionals}=\%emptyHash;

#DEBUG!
#print $threadsUrl;
#print "\n";
#print encode_json($post_msg);
#print "\n";

my $request_option=HTTP::Request->new( "OPTIONS" , $threadsUrl );
$request_option->header( "Origin" => "https://www.nicovideo.jp" );
$request_option->header( "Referer" => "https://www.nicovideo.jp/" );
$ua->request($request_option);

my $req=HTTP::Request->new(POST => $threadsUrl);
$req->header( "Origin" => "https://www.nicovideo.jp" );
$req->header( "Referer" => "https://www.nicovideo.jp/" );
$req->header( "x-client-os-type" => "others" );
$req->header( "x-frontend-id" => "6" );
$req->header( "x-frontend-version" => "0" );
$req->content(encode_json($post_msg));
print $ua->request($req)->content;

sub ParseUrl{
my $text=$_[0];
my $key=$_[1];
my ($res) = $text=~ m/$key\=([^\&]+)/;
return uri_unescape($res);
}
92 changes: 92 additions & 0 deletions nicoch/commentproxyold.cgi
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/perl
#use Net::Netrc;
use LWP::UserAgent;
use URI::Escape;
use CGI;
use Web::Scraper;
use Unicode::Escape qw(escape unescape);
use JSON;
use File::Spec;
use File::Basename;

require File::Spec->catfile(dirname(__FILE__),"common.pl");

print <<"HEAD";
Content-type: application/xml
Access-Control-Allow-Origin: *
HEAD

#my $mach = Net::Netrc->lookup('nicovideo');
#my ($nicologin, $nicopassword, $nicoaccount) = $mach->lpa;

my %conf=GetConf("/etc/nicochcgi/nicoch.conf");
$nicologin = $conf{"niconico_id"};
$nicopassword=$conf{"niconico_password"};

my $login_info = {
mail_tel => $nicologin,
password => $nicopassword,
};

my $q=new CGI;
#my $movie_id=$q->param('id');
my $movie_id="so43707962";

my $ua = LWP::UserAgent->new(cookie_jar => {});
$ua->post("https://secure.nicovideo.jp/secure/login?site=niconico", $login_info);

my $info_res = $ua->get("https://www.nicovideo.jp/watch/".$movie_id);
my $info_json = scraper {
process 'div#js-initial-watch-data', 'json' => '@data-api-data'
}->scrape($info_res->content)->{json};
$info_json = unescape($info_json);
$info = decode_json( $info_json );

#my $ms= $info->{thread}->{serverUrl};
my $ms= $info->{comment}->{server}->{url};#"https://nvcomment.nicovideo.jp/legacy/api/"
#my $user_id= $info->{video}->{dmcInfo}->{user}->{user_id};
my $user_id = $info->{viewer}->{id};
my $user_key = $info->{comment}->{keys}->{userKey};
my $length = $info->{video}->{duration};
my $threads = $info->{comment}->{threads};

my $min=int($length/60)+1;
my $post_msg="<packet>\n";

foreach my $thread (@$threads){
if($thread->{isActive} == 1){
my $thread_id = $thread->{id};
if($thread->{isThreadkeyRequired} == 1){
#my $thread_key_res=$ua->get("http://flapi.nicovideo.jp/api/getthreadkey?thread=".$thread->{id});

#my $thread_key = ParseUrl($thread_key_res->content,"threadkey");
#my $force_184 = ParseUrl($thread_key_res->content,"force_184");
my $thread_key = $thread->{threadkey};
my $force_184 = $thread->{is184Forced};

$post_msg.=<<"PACKET"
<thread thread="$thread_id" version="20090904" threadkey="$thread_key" force_184="$force_184" user_id="$user_id" />
<thread_leaves scores="1" thread="$thread_id" threadkey="$thread_key" force_184="$force_184" user_id="$user_id">0-$min:100,1000</thread_leaves>
PACKET
}else{
$post_msg.=" <thread thread=\"$thread_id\" version=\"20061206\" res_from=\"-1000\" user_id=\"$user_id\" userkey=\"$user_key\" />\n";
if($thread->{isLeafRequired} == 1){
$post_msg.=" <thread_leaves scores=\"1\" thread=\"$thread_id\" user_id=\"$user_id\" userkey=\"$user_key\">0-$min:100,1000</thread_leaves>\n";
}
}
}
}

$post_msg.="</packet>\n";

my $req=HTTP::Request->new(POST => $ms);
$req->content($post_msg);
print $ua->request($req)->content;

sub ParseUrl{
my $text=$_[0];
my $key=$_[1];
my ($res) = $text=~ m/$key\=([^\&]+)/;
return uri_unescape($res);
}
41 changes: 40 additions & 1 deletion nicoch/play.html
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@

if(CommentSource!=comment_org){
GetChatXML(function(xml) {
Comments=ParseComment(xml);
Comments=ParseCommentJson(xml);
NotifyCommentUpdate(Comments);
if(CommentTimer){clearInterval(CommentTimer);}
CommentTimer=setInterval('DrawComment()', 1000.0/Fps);
Expand Down Expand Up @@ -944,7 +944,46 @@
function CheckY(fontHeight,y1,y2,lineCnt1,lineCnt2){
return y1-fontHeight*(lineCnt2-0.5) < y2 && y2-fontHeight*(lineCnt1-0.5) < y1;
}
function ParseCommentJson(json){
let result=[];
const commentObj=JSON.parse(json);
let chat=function(text,vpos,mail,cnt,unixtime){
this.text=text;
this.vpos=vpos;
if(mail!=null){
this.mail=mail.toLowerCase();
}else{
this.mail="";
}
this.count=cnt;
this.lastY=-1;
this.width=-1;
this.lastCnt=-1;
this.fontScale=-1;
//for contextmenu
this.lastYReal=-1;
this.lastX=-1;
this.lastWidth=-1;
this.lastDisplayed=0;
this.lastHeight=-1;
//for info
this.date=new Date(unixtime);
}
for(let i=0; i<commentObj.data.threads.length; i++){
const thread = commentObj.data.threads[i];
for(let j=0; j<thread.comments.length; j++){
const comment = thread.comments[j];
result.push(new chat(comment.body,comment.vposMs,comment.commands.join(' '),comment.no,comment.postedAt));
}
}
result.sort(function(a,b){
if(a.vpos==b.vpos){return a.count>b.count?1:-1;}
return a.vpos-b.vpos;
});
return result;
}
function ParseComment(xml){
//This function is obsolete. It's not used anymore.
let result=[];
let parser = new DOMParser();
let dom=parser.parseFromString(xml, 'text/xml');
Expand Down
4 changes: 3 additions & 1 deletion nicoch/play_ch.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<a href=".">Home</a>
<a href="tv.html">TV Home</a>
<a href="play.html" onclick="OpenVideoPage();return false;">単体ビュワー</a>
<a href="https://www.nicovideo.jp/watch/" onclick="OpenVideoPageNicovideo();return false;">公式</a>
<a id="linkNicovideo" href="https://www.nicovideo.jp/watch/" onclick="OpenVideoPageNicovideo();return false;">公式</a>
</div>
<div id="comments"></div>
</div>
Expand Down Expand Up @@ -117,6 +117,7 @@
GetData(function(){SetId();PlayDefault();});

var iframe=document.getElementById("player");
var iframed = (iframe.contentWindow || iframe.contentDocument);
iframe.onload = function() {
FixKeyDown();
}
Expand Down Expand Up @@ -403,6 +404,7 @@
if(targetCount==-1) return;
VideoCount=targetCount;
let video=currentChannel.videos[targetCount];
document.getElementById("linkNicovideo").href="https://www.nicovideo.jp/watch/"+video.id;
PlayUrl(video.player_url+repeatFooter);
SetHash(currentChannel.channel_id,video.id);
SetInfo(video);
Expand Down

0 comments on commit ca62be8

Please sign in to comment.