-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
108 lines (85 loc) · 2.5 KB
/
history.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package api
import (
"fmt"
"github.com/anacrolix/missinggo/perf"
"github.com/asdine/storm"
"github.com/gin-gonic/gin"
"github.com/projectx13/projectx/database"
"github.com/projectx13/projectx/xbmc"
)
// History ...
func History(ctx *gin.Context) {
defer perf.ScopeTimer()()
ctx.Writer.Header().Set("Access-Control-Allow-Origin", "*")
infohash := ctx.Query("infohash")
if torrent := InTorrentsHistory(infohash); torrent != nil {
ctx.Redirect(302, URLQuery(
URLForXBMC("/play"), "uri", torrent.URI,
))
return
}
items := []*xbmc.ListItem{}
var ths []database.TorrentHistory
if err := database.GetStormDB().AllByIndex("Dt", &ths, storm.Reverse()); err != nil {
log.Infof("Could not get list of history items: %s", err)
}
for _, th := range ths {
items = append(items, &xbmc.ListItem{
Label: th.Name,
Path: torrentHistoryGetXbmcURL(th.InfoHash),
ContextMenu: [][]string{
{"LOCALIZE[30406]", fmt.Sprintf("XBMC.RunPlugin(%s)",
URLQuery(URLForXBMC("/history/remove"),
"infohash", th.InfoHash,
))},
},
Info: &xbmc.ListItemInfo{
Mediatype: "video",
},
IsPlayable: true,
})
}
ctx.JSON(200, xbmc.NewView("", items))
}
func torrentHistoryEmpty() bool {
count, err := database.GetStormDB().Count(&database.TorrentHistory{})
if err != nil {
log.Infof("Could not get count for torrent history: %s", err)
}
return err != nil || count == 0
}
// HistoryRemove ...
func HistoryRemove(ctx *gin.Context) {
defer perf.ScopeTimer()()
infohash := ctx.DefaultQuery("infohash", "")
if len(infohash) == 0 {
return
}
log.Debugf("Removing infohash '%s' with torrent history", infohash)
var th database.TorrentHistory
if err := database.GetStormDB().One("InfoHash", infohash, &th); err == nil {
database.GetStormDB().DeleteStruct(&th)
database.GetStormDB().ReIndex(&database.TorrentHistory{})
}
xbmc.Refresh()
ctx.String(200, "")
return
}
// HistoryClear ...
func HistoryClear(ctx *gin.Context) {
defer perf.ScopeTimer()()
log.Debugf("Cleaning queries with torrent history")
if err := database.GetStormDB().Drop(&database.TorrentHistory{}); err != nil {
log.Infof("Could not clean torrent history: %s", err)
}
database.GetStormDB().ReIndex(&database.TorrentHistory{})
xbmc.Refresh()
ctx.String(200, "")
return
}
func torrentHistoryGetXbmcURL(infohash string) string {
return URLQuery(URLForXBMC("/history"), "infohash", infohash)
}
func torrentHistoryGetHTTPUrl(infohash string) string {
return URLQuery(URLForHTTP("/history"), "infohash", infohash)
}