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

This should add nyaasi support.

Is not tested though.
Cette révision appartient à :
akuma06 2017-12-24 18:09:38 +01:00
Parent 96fbf786d8
révision 3604deed9e
4 fichiers modifiés avec 82 ajouts et 7 suppressions

Voir le fichier

@ -152,7 +152,8 @@ type TorrentsConfig struct {
// UploadConfig : Config struct for uploading torrents
type UploadConfig struct {
DefaultAnidexToken string `yaml:"anidex_api_token,omitempty"`
DefaultNyaasiToken string `yaml:"nyaasi_api_token,omitempty"`
DefaultNyaasiUsername string `yaml:"nyaasi_api_username,omitempty"`
DefaultNyaasiPassword string `yaml:"nyaasi_api_password,omitempty"`
DefaultTokyoTToken string `yaml:"tokyot_api_token,omitempty"`
UploadsDisabled bool `yaml:"uploads_disabled,omitempty"`
AdminsAreStillAllowedTo bool `yaml:"admins_are_still_allowed_to,omitempty"`

Voir le fichier

@ -89,7 +89,7 @@ func UploadPostHandler(c *gin.Context) {
log.CheckErrorWithMessage(err, "ERROR_TORRENT_CREATED: Error while creating entry in db")
if AnidexUpload || NyaaSiUpload || TokyoToshoUpload {
go func(anidexApiKey string, anidexFormCategory string, anidexFormLang string, nyaasiApiKey string, toshoApiKey string) {
go func(anidexApiKey string, anidexFormCategory string, anidexFormLang string, nyaasiUsername string, nyaasiPassword string, toshoApiKey string) {
err := upload.GotFile(torrent)
if err != nil {
log.CheckError(err)
@ -101,13 +101,13 @@ func UploadPostHandler(c *gin.Context) {
}
if NyaaSiUpload {
go upload.ToNyaasi(nyaasiApiKey, torrent)
go upload.ToNyaasi(nyaasiUsername, nyaasiPassword, torrent)
}
if TokyoToshoUpload {
go upload.ToTTosho(toshoApiKey, torrent)
}
}(c.PostForm("anidex_api"), c.PostForm("anidex_form_category"), c.PostForm("anidex_form_lang"), c.PostForm("nyaasi_api"), c.PostForm("tokyot_api"))
}(c.PostForm("anidex_api"), c.PostForm("anidex_form_category"), c.PostForm("anidex_form_lang"), c.PostForm("nyaasi_username"), c.PostForm("nyaasi_password"), c.PostForm("tokyot_api"))
// After that, we redirect to the page for upload status
url := fmt.Sprintf("/upload/status/%d", torrent.ID)
c.Redirect(302, url)

Voir le fichier

@ -134,8 +134,10 @@
<p>{{ T("upload_to") }} <b>Nyaa.si</b>:</p>
<div>
<div class="checkbox-container"><input type="checkbox" value="true" name="nyaasi_upload" id="nyaasi_upload" {{if User.NyaasiAPIToken != ""}}checked{{end}} class="form-torrent"/></div>
<input name="nyaasi_api" id="nyaasi_api" placeholder="Nyaa.si API token" class="form-input up-input" type="text" value="{{User.NyaasiAPIToken}}" {{if User.AnidexAPIToken != ""}}disabled{{end}}/>
<div class="checkbox-container"><input type="checkbox" value="true" name="nyaasi_upload" id="nyaasi_upload" class="form-torrent"/></div>
<input name="nyaasi_username" id="nyaasi_username" placeholder="Nyaa.si Username" class="form-input up-input" type="text" value="" />
<br/>
<input name="nyaasi_password" id="nyaasi_password" placeholder="Nyaa.si Password" class="form-input up-input" type="password" value="" />
</div>
<p>{{ T("upload_to") }} <b>TokyoTosho</b>:</p>

Voir le fichier

@ -2,6 +2,7 @@ package upload
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
@ -121,12 +122,83 @@ func ToAnidex(torrent *models.Torrent, apiKey string, subCat string, lang string
}
// ToNyaasi : function to upload a torrent to anidex
func ToNyaasi(apiKey string, torrent *models.Torrent) {
func ToNyaasi(username string, password 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")
// If the torrent is posted as anonymous or apikey is not set, we set it with default value
if username == "" || (torrent.Hidden && username != "") {
username = config.Get().Upload.DefaultNyaasiUsername
password = config.Get().Upload.DefaultNyaasiPassword
}
if username == "" || password == "" { // You need to check that username AND password are not empty even after config. Since they are left empty in config by default and are required
log.Errorf("Username or Password is empty, we can't upload to Nyaa.si for torrent %d", torrent.ID)
uploadMultiple.updateAndSave(nyaasi, errorState, "No valid account providen (required)")
return
}
params := map[string]interface{}{
"name": torrent.Name,
"category": Category(nyaasi, torrent),
"information": "",
"description": torrent.Description,
"anonymous": torrent.IsAnon(),
"hidden": false,
"remake": torrent.IsRemake(),
"trusted": torrent.IsTrusted(),
}
torrentData, _ := json.Marshal(params)
extraParams := map[string]string{
"torrent_data": string(torrentData),
}
request, err := newfileUploadRequest("https://nyaa.si/api/upload", extraParams, "torrent", torrent.GetPath())
if err != nil {
log.CheckError(err)
return
}
request.SetBasicAuth(username, password)
client := &http.Client{}
rsp, err := client.Do(request)
if err != nil {
log.CheckError(err)
return
}
log.Info("Launch Nyaa.Si http request")
if err != nil {
uploadMultiple.updateAndSave(nyaasi, 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(nyaasi, errorState, "Unknown error")
log.CheckErrorWithMessage(err, "Error in parsing request: %s")
return
}
if uploadMultiple.Nyaasi.Status == pendingState {
var data map[string]interface{}
if err = json.Unmarshal(bodyByte, &data); err != nil {
log.CheckErrorWithMessage(err, "Cannot unmarshal json Response after upload request to Nyaa.Si")
uploadMultiple.Nyaasi.Status = errorState
uploadMultiple.Nyaasi.Message = err.Error()
}
if _, ok := data["errors"]; ok {
uploadMultiple.Nyaasi.Status = errorState
uploadMultiple.Nyaasi.Message = string(bodyByte)
} else {
uploadMultiple.Nyaasi.Status = doneState
uploadMultiple.Nyaasi.Message = fmt.Sprintf("%s", data["url"].(string))
}
uploadMultiple.save(nyaasi)
log.Info("Nyaa.Si request done")
fmt.Println(uploadMultiple)
}
}
// ToTTosho : function to upload a torrent to TokyoTosho