Albirew/nyaa-pantsu
Albirew
/
nyaa-pantsu
Archivé
1
0
Bifurcation 0

This adds TokyoTosho support. Since TokyoTosho API only support upload by URL (and not torrent file upload). We need to make the download url available without ddos protection.

Also TokyoTosho doesn't need a category select. Since it does have limited option, we can automatically convert our categories to the tokyotosho one.
Cette révision appartient à :
akuma06 2017-12-23 21:10:49 +01:00
Parent d9701e3ab4
révision b0d32ff8a3
3 fichiers modifiés avec 151 ajouts et 12 suppressions

Voir le fichier

@ -360,16 +360,7 @@ func (t *Torrent) ToJSON() TorrentJSON {
} else if t.OldUploader != "" {
uploader = t.OldUploader
}
torrentlink := ""
if len(config.Get().Torrents.CacheLink) > 0 { // Only use torrent cache if set, don't check id since better to have all .torrent
if config.IsSukebei() {
torrentlink = "" // torrent cache doesn't have sukebei torrents
} else {
torrentlink = fmt.Sprintf(config.Get().Torrents.CacheLink, t.Hash)
}
} else if len(config.Get().Torrents.StorageLink) > 0 { // Only use own .torrent if storage set
torrentlink = fmt.Sprintf(config.Get().Torrents.StorageLink, t.Hash)
}
scrape := Scrape{}
if t.Scrape != nil {
scrape = *t.Scrape
@ -405,7 +396,7 @@ func (t *Torrent) ToJSON() TorrentJSON {
WebsiteLink: sanitize.Safe(t.WebsiteLink),
Languages: t.Languages,
Magnet: template.URL(magnet),
TorrentLink: sanitize.Safe(torrentlink),
TorrentLink: sanitize.Safe(t.Download()),
Leechers: scrape.Leechers,
Seeders: scrape.Seeders,
Completed: scrape.Completed,
@ -532,6 +523,20 @@ func (t *Torrent) DeleteTags() {
}
}
// Download generate a download link for a torrent
func (t *Torrent) Download() (torrentlink string) {
if len(config.Get().Torrents.CacheLink) > 0 { // Only use torrent cache if set, don't check id since better to have all .torrent
if !config.IsSukebei() { // torrent cache doesn't have sukebei torrents
torrentlink = fmt.Sprintf(config.Get().Torrents.CacheLink, t.Hash)
}
return
}
if len(config.Get().Torrents.StorageLink) > 0 { // Only use own .torrent if storage set
torrentlink = fmt.Sprintf(config.Get().Torrents.StorageLink, t.Hash)
}
return
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {

Voir le fichier

@ -7,6 +7,7 @@ import (
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"strings"
"time"
@ -15,6 +16,7 @@ import (
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/utils/cache"
"github.com/NyaaPantsu/nyaa/utils/log"
"github.com/NyaaPantsu/nyaa/utils/upload/ttosho"
)
const (
@ -124,12 +126,76 @@ func ToNyaasi(apiKey string, torrent *models.Torrent) {
uploadMultiple := MultipleForm{PantsuID: torrent.ID, Nyaasi: service{Status: pendingState}}
uploadMultiple.Nyaasi.Message = "Sorry u are not allowed"
uploadMultiple.save(nyaasi)
log.Info("Create NyaaSi instance")
}
// ToTTosho : function to upload a torrent to anidex
// ToTTosho : function to upload a torrent to TokyoTosho
func ToTTosho(apiKey string, torrent *models.Torrent) {
uploadMultiple := MultipleForm{PantsuID: torrent.ID, TTosho: service{Status: pendingState}}
uploadMultiple.save(ttosho)
log.Info("Create TokyoTosho instance")
// If the torrent is posted as anonymous or apikey is not set, we set it with default value
if apiKey == "" || (torrent.Hidden && apiKey != "") {
apiKey = config.Get().Upload.DefaultTokyoTToken
}
if apiKey == "" { // You need to check that apikey is not empty even after config. Since it is left empty in config by default and is required
log.Errorf("ApiKey is empty, we can't upload to TokyoTosho for torrent %d", torrent.ID)
uploadMultiple.updateAndSave(ttosho, errorState, "No ApiKey providen (required)")
return
}
extraParams := map[string]string{
//Required
"apikey": apiKey,
"url": torrent.Download(),
"type": ttoshoConfig.Category(torrent),
"send": "true",
//Optional
"website": torrent.WebsiteLink,
"comment": torrent.Description,
}
request, err := newUploadRequest("https://www.tokyotosho.info/new.php", extraParams)
if err != nil {
log.CheckError(err)
return
}
client := &http.Client{}
rsp, err := client.Do(request)
if err != nil {
log.CheckError(err)
return
}
log.Info("Launch TokyoTosho http request")
if err != nil {
uploadMultiple.updateAndSave(ttosho, errorState, "Error during the HTTP POST request")
log.CheckErrorWithMessage(err, "Error in request: %s")
return
}
defer rsp.Body.Close()
bodyByte, err := ioutil.ReadAll(rsp.Body)
if err != nil {
uploadMultiple.updateAndSave(ttosho, errorState, "Unknown error")
log.CheckErrorWithMessage(err, "Error in parsing request: %s")
return
}
if uploadMultiple.TTosho.Status == pendingState {
if strings.Contains(string(bodyByte), "OK,") {
uploadMultiple.TTosho.Status = doneState
idnumber := strings.Split(string(bodyByte), ",")
uploadMultiple.TTosho.Message = fmt.Sprintf("Upload done! https://www.tokyotosho.info/details.php?id=%s", idnumber[1])
} else {
uploadMultiple.TTosho.Status = errorState
uploadMultiple.TTosho.Message = string(bodyByte)
}
uploadMultiple.save(ttosho)
log.Info("TokyoTosho request done")
fmt.Println(uploadMultiple)
}
}
// Saves the multipleform in each go routines and share the state of each upload for 5 minutes
@ -213,3 +279,18 @@ func newfileUploadRequest(uri string, params map[string]string, paramName, path
request.Header.Add("Content-Type", writer.FormDataContentType())
return request, nil
}
// Creates a new upload http request with optional extra params
func newUploadRequest(uri string, params map[string]string) (*http.Request, error) {
var form url.Values
for key, val := range params {
form[key] = append(form[key], val)
}
body := bytes.NewBufferString(form.Encode())
request, err := http.NewRequest("POST", uri, body)
if err != nil {
return nil, err
}
return request, nil
}

Voir le fichier

@ -0,0 +1,53 @@
package ttoshoConfig
import (
"fmt"
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/models"
)
// Convert automatically our sukebei cats to anidex Hentai cats
var sukebeiCategories = map[string]string{
"1_1": "12",
"1_2": "12",
"1_3": "14",
"1_4": "13",
"1_5": "4",
"2_1": "4",
"2_2": "15",
}
var normalCategories = map[string]string{
"3_12": "1",
"3_5": "1",
"3_13": "10",
"3_6": "7",
"2_3": "2",
"2_4": "2",
"4_7": "3",
"4_8": "7",
"4_14": "10",
"5_9": "8",
"5_10": "8",
"5_18": "10",
"5_11": "7",
"6_15": "5",
"6_16": "5",
"1_1": "5",
"1_2": "5",
}
// Category returns the category converted from nyaa one to tosho one
func Category(t *models.Torrent) string {
cat := fmt.Sprintf("%d_%d", t.Category, t.SubCategory)
if config.IsSukebei() {
if val, ok := sukebeiCategories[cat]; ok {
return val
}
}
if val, ok := normalCategories[cat]; ok {
return val
}
return ""
}