2017-05-05 14:20:51 +02:00
package model
import (
"github.com/ewhal/nyaa/config"
2017-05-06 04:18:24 +02:00
"github.com/ewhal/nyaa/util"
2017-05-05 14:20:51 +02:00
2017-05-06 04:18:24 +02:00
"encoding/json"
2017-05-05 14:20:51 +02:00
"html"
"html/template"
"strconv"
"strings"
"time"
)
type Feed struct {
Id int
Name string
Hash string
Magnet string
Timestamp string
}
type Torrents struct {
2017-05-07 06:26:09 +02:00
Id int ` gorm:"column:torrent_id;primary_key" `
2017-05-05 14:20:51 +02:00
Name string ` gorm:"column:torrent_name" `
2017-05-06 23:46:53 +02:00
Category int ` gorm:"column:category_id" `
Sub_Category int ` gorm:"column:sub_category_id" `
2017-05-05 14:20:51 +02:00
Status int ` gorm:"column:status_id" `
Hash string ` gorm:"column:torrent_hash" `
Date int64 ` gorm:"column:date" `
Downloads int ` gorm:"column:downloads" `
2017-05-06 14:43:15 +02:00
Filesize int64 ` gorm:"column:filesize" `
2017-05-07 07:18:41 +02:00
Description string ` gorm:"column:description" `
2017-05-06 04:18:24 +02:00
Comments [ ] byte ` gorm:"column:comments" `
2017-05-05 14:20:51 +02:00
}
/ * We need JSON Object instead because of Magnet URL that is not in the database but generated dynamically
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
JSON Models Oject
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
* /
2017-05-06 23:46:53 +02:00
type ApiResultJson struct {
Torrents [ ] TorrentsJson ` json:"torrents" `
QueryRecordCount int ` json:"queryRecordCount" `
TotalRecordCount int ` json:"totalRecordCount" `
2017-05-05 14:20:51 +02:00
}
2017-05-06 04:18:24 +02:00
type CommentsJson struct {
C template . HTML ` json:"c" `
Us string ` json:"us" `
Un string ` json:"un" `
UI int ` json:"ui" `
T int ` json:"t" `
Av string ` json:"av" `
ID string ` json:"id" `
}
2017-05-05 14:20:51 +02:00
type TorrentsJson struct {
2017-05-06 23:46:53 +02:00
Id string ` json:"id" `
Name string ` json:"name" `
Status int ` json:"status" `
Hash string ` json:"hash" `
Date string ` json:"date" `
Filesize string ` json:"filesize" `
Description template . HTML ` json:"description" `
Comments [ ] CommentsJson ` json:"comments" `
Sub_Category string ` json:"sub_category" `
Category string ` json:"category" `
Magnet template . URL ` json:"magnet" `
2017-05-05 14:20:51 +02:00
}
/* Model Conversion to Json */
func ( t * Torrents ) ToJson ( ) TorrentsJson {
2017-05-07 03:10:35 +02:00
magnet := util . InfoHashToMagnet ( strings . TrimSpace ( t . Hash ) , t . Name , config . Trackers ... )
2017-05-06 04:18:24 +02:00
b := [ ] CommentsJson { }
2017-05-06 18:58:05 +02:00
_ = json . Unmarshal ( [ ] byte ( t . Comments ) , & b )
2017-05-05 14:20:51 +02:00
res := TorrentsJson {
Id : strconv . Itoa ( t . Id ) ,
Name : html . UnescapeString ( t . Name ) ,
Status : t . Status ,
Hash : t . Hash ,
Date : time . Unix ( t . Date , 0 ) . Format ( time . RFC3339 ) ,
2017-05-06 14:43:15 +02:00
Filesize : util . FormatFilesize ( t . Filesize ) ,
2017-05-06 18:41:28 +02:00
Description : template . HTML ( t . Description ) ,
2017-05-06 04:18:24 +02:00
Comments : b ,
2017-05-06 23:46:53 +02:00
Sub_Category : strconv . Itoa ( t . Sub_Category ) ,
Category : strconv . Itoa ( t . Category ) ,
2017-05-05 14:20:51 +02:00
Magnet : util . Safe ( magnet ) }
return res
}
/* Complete the functions when necessary... */