Skip to content

Commit

Permalink
livedl2 first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
himananiito committed Jan 1, 2019
1 parent a2c52e1 commit 9cfe2ed
Show file tree
Hide file tree
Showing 18 changed files with 3,263 additions and 6 deletions.
26 changes: 22 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
# livedl
新配信(HTML5)に対応したニコ生録画ツール。ニコ生以外のサイトにも対応予定
# livedl2 αバージョン

次期バージョンとなる予定のlivedl2の実験的機能評価用バージョンです。

## 使い方
https://himananiito.hatenablog.jp/entry/livedl
を参照

`user-session.txt`にユーザーセッション情報(`user_session_XXXX_XXXXXX`)を書いて保存

livedl2の実行ファイルを起動し、
http://localhost:8080/
にアクセスして下さい。

### セッション情報の調べ方

**セッション情報は「絶対に」他人に流出しないようにして下さい**

例えばログイン済みのブラウザでニコ生トップページからデバッグコンソールを開き、以下のスクリプトを実行し、ブラウザ画面の文字列をコピーする。

```
var match = document.cookie.match(/user_session=(\w+)/); if(match) document.write(match[1]);
```

## ビルド方法

以下の`lived.go``livedl2.go`に読み換えて下さい。

## Linux(Ubuntu)でのビルド方法
```
Expand Down
4 changes: 2 additions & 2 deletions replacelocal.pl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use strict;
use v5.20;

for my $file("livedl.exe", "livedl.x86.exe", "livedl-logger.exe") {
for my $file("livedl2.exe") {
open my $f, "<:raw", $file or die;
undef $/;
my $s = <$f>;
Expand All @@ -16,7 +16,7 @@
while($s =~ m{(?<=\0)[^\0]{5,512}\.go(?=\0)|(?<=[[:cntrl:]])_/[A-Z]_/[^\0]{5,512}}g) {
my $s = $&;
if($s =~ m{\A(.*(?:/Users/.+?/go/src|/Go/src))(/.*)\z}s or
$s =~ m{\A(.*(?=/livedl/src/))(/.*)\z}s) {
$s =~ m{\A(.*(?=/livedl[^/]*/src/))(/.*)\z}s) {
my($all, $p, $f) = ($s, $1, $2);

my $p2 = $p;
Expand Down
4 changes: 4 additions & 0 deletions run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go build src/livedl2.go
if ($?) {
./livedl2
}
92 changes: 92 additions & 0 deletions src/httpcommon/httpcommon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package httpcommon

import (
"errors"
"net/http"
"strings"
"sync"
"time"
)

type Callback func(*http.Response, error, interface{}, interface{}, time.Time, time.Time)

type HttpWork struct {
Client *http.Client
Request *http.Request
Callback Callback
This interface{}
QueuedAt time.Time
Option interface{}
}

func GetClient() *http.Client {
var client = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) (err error) {
if req != nil && via != nil && len(via) > 0 {
if len(via) >= 10 {
return errors.New("stopped after 10 redirects")
}

// 元のRequest.URLでリダイレクト後のURLを取れるようにしたい
via[0].URL = req.URL

// ニコニコならCookieを引き継ぐ
if strings.HasSuffix(req.URL.Host, ".nicovideo.jp") {
req.Header = via[0].Header
}
}
return nil
},
}
return client
}

func Launch(num int) (q chan HttpWork) {
q = make(chan HttpWork, 10)
var m sync.Mutex

requestPerSec := 6.0 // [リクエスト数/秒] 超える場合に一定期間Sleepする
sleepTime := 500 * time.Millisecond // Sleep時間
arrSize := 5 // サンプル数

arr := make([]int64, 0, arrSize)

checkLimit := func(t time.Time) {
nano := t.UnixNano()
m.Lock()
defer m.Unlock()

if len(arr) >= arrSize {
arr = arr[1:arrSize]
arr = append(arr, nano)

diff := arr[arrSize-1] - arr[0] // total sec
delta := float64(len(arr)) / float64(diff) * 1000 * 1000 * 1000 // requests per sec

//fmt.Printf("delta is %v\n", delta)
if delta >= requestPerSec {
arr = arr[:0]
time.Sleep(sleepTime)
//return true
}
} else {
arr = append(arr, nano)
}
//return false
}

for i := 0; i < num; i++ {
go func() {
for htw := range q {

startedAt := time.Now()

checkLimit(startedAt)

res, err := htw.Client.Do(htw.Request)
htw.Callback(res, err, htw.This, htw.Option, htw.QueuedAt, startedAt)
}
}()
}
return q
}
Loading

0 comments on commit 9cfe2ed

Please sign in to comment.