Skip to content

Commit

Permalink
Merge pull request #6 from yudeunagi/develop
Browse files Browse the repository at this point in the history
5ch系のis-modified-since対応+バグ修正
  • Loading branch information
yudeunagi authored Nov 4, 2019
2 parents 319c68b + e532adb commit e5539b9
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "unacast",
"version": "0.1.0",
"version": "0.1.2",
"description": "掲示板のレスをYoutubeコメント風に表示したいという思想の元に開発されるツール",
"main": "src/main/main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "electron .",
"buildwin": "electron-packager ./ unacast --platform=win32 --arch=x64 --icon=icon.ico",
"buildmac": "electron-packager ./ unacast --platform=darwin --arch=x64",
"macbuildmemo": "echo macのビルドで --icon=icon.ico を指定するとなぜか The path argument must be one of type string, Buffer, or URL. Received type undefined って言われるので外しておく"
"macbuildmemo": "echo macのビルドで --icon=icon.ico を指定するとなぜか The path argument must be one of type string, Buffer, or URL. Received type undefined って言われるので外しておく、あと管理者権限のコマンドプロンプトで実行してね(シンボリックリンク作れないので)"
},
"repository": {
"type": "git",
Expand Down
15 changes: 7 additions & 8 deletions public/js/readThread.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ window.onload = function(){
var url = $('#threadUrl').val();
//レス番号の設定
var resNum = $('#resNumber').val();

if(resNum.length < 1) {
//レス番号がない場合は最新の1件を取得する
getLastNumber(url);
Expand All @@ -19,13 +20,13 @@ window.onload = function(){
setInterval(readThread, interval, url);
}



// 最終レス番取得API
function getLastNumber(url){
// ポート番号の取得
var port = $('#port').val();
//リクエストボディの作成
const data = makeRequestBody(url);
const requestUrl = 'http://localhost:3000/getRes';
const requestUrl = 'http://localhost:' + port + '/getRes';
//fetchでレスを取得する
fetch(requestUrl, {
method: 'POST',
Expand Down Expand Up @@ -62,14 +63,12 @@ function getLastNumber(url){
*resNum:最終レス番号
*/
var readThread = function(url){
//最終レス番号以降のすべてのレスを取得する

// ポート番号の取得
var port = $('#port').val();
//リクエストボディの作成
const data = makeRequestBody(url);
// console.log(data);

// 内部で作成したレス取得APIを呼び出す
const requestUrl = 'http://localhost:3000/getRes';
const requestUrl = 'http://localhost:' + port + '/getRes';
//fetchでレスを取得する
fetch(requestUrl, {
method: 'POST',
Expand Down
61 changes: 54 additions & 7 deletions src/main/readBBS/Read5ch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
*/
const request = require('request-promise'); //httpリクエスト
const iconv = require('iconv-lite'); // 文字コード変換用パッケージ
//ステータスコード304 _NotModified
const NOT_MODIFIED = '304';

// 最終取得スレッド
var lastThreadUrl = '';
// 最終レス番号
var lastResNumber = 0;
// 最終バイト数
var lastByte = 0;
//最終更新日時
var lastModified = null;
// 最終バイト数
var lastByte = 0;

/**
* コンストラクタ
Expand All @@ -29,6 +33,17 @@ var ReadSitaraba = function()
*/
ReadSitaraba.prototype.read = async function(threadUrl, resNum, res, next){


//板や最終日レス番号がかわったら最初からとり直す(lastmodifiと rangeのリセット)
if(threadUrl != lastThreadUrl
|| resNum < lastResNumber ){
lastThreadUrl = threadUrl;
lastModified = null;
lastByte = 0;
}else {
console.log('noresete');
}

//リクエストURL作成 下記みたいな感じで変換する
//https://bbs.jpnkn.com/test/read.cgi/yudeunagi/1572734724/
//https://bbs.jpnkn.com/yudeunagi/dat/1572734724.dat
Expand All @@ -52,23 +67,55 @@ ReadSitaraba.prototype.read = async function(threadUrl, resNum, res, next){
url: requestUrl,
method: 'GET',
encoding: null, // ここでnull指定しないとなんかうまくいかない
"If-Modified-Since": lastModified,
"Range": range
"resolveWithFullResponse": true,
headers: {
"if-modified-since": lastModified,
}
}
console.log(options);

var responseJson;
//掲示板へのリクエスト実行
console.log('[Read5ch.js]5ch系BBSレス取得API呼び出し開始');
await request(options)
.then(body =>{
console.log('[Read5ch.js]5ch系BBSレス取得API呼び出し成功');
.then(response =>{
var statusCode = response.statusCode;
console.log('[Read5ch.js]5ch系BBSレス取得API呼び出し完了、statusCode=' + statusCode);

// 取得バイト数表示
console.log('[Read5ch.read]レスポンスヘッダ=');
var headers = response.headers;
console.log(headers);
//LastModifiedとRange更新処理
if(headers['last-modified'] != null){
lastModified = headers['last-modified'];
console.log(lastModified);
}
// if(headers['content-length'] != null){
// console.log(headers['content-length']);
// lastByte = lastByte + parseInt(headers['content-length']);
// console.log('range=' + lastByte);
// }

//gzipで取得出来たら解凍処理も入れる

//したらばAPIの文字コードはEUC-JPなのでUTF-8に変換する
var str = iconv.decode(Buffer.from(body), 'Shift_JIS');
var str = iconv.decode(Buffer.from(response.body), 'Shift_JIS');
// レスポンスオブジェクト作成
responseJson = purseNewResponse(str, resNum);

// ヘッダからLast-Modified と content-length数をとる

})
.catch(error =>{
console.log('[Read5ch.js]5ch系BBSレス取得APIリクエストエラー、message=' + error.message);
var rsArray = new Array();
console.log(rsArray.length);
responseJson = rsArray;
});
return responseJson;
}

/**
*取得したレスポンス(複数)のパース
*戻りとしてパースしたjsonオブジェクトの配列を返す
Expand Down
1 change: 1 addition & 0 deletions views/server.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<form id="requestForm">
<input type="hidden" id="threadUrl" name="threadUrl" value="<%=url%>" >
<input type="hidden" id="resNumber" name="resNumber" value="<%=resNumber%>" >
<input type="hidden" id="port" name="port" value="<%=port%>" >
<input type="hidden" id="noname" name="noname" value="<%=noname%>" >
<input type="hidden" id="dispNumber" name="dispNumber" value="<%=dipsNumber%>" >
<input type="hidden" id="interval" name="interval" value="<%=interval%>" >
Expand Down

0 comments on commit e5539b9

Please sign in to comment.