-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathepisodedb.go
140 lines (124 loc) · 3.21 KB
/
episodedb.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* Episode Database
*
* Gumshoe's episode database structs and the functions that modify it.
* Add, delete and list your episodes that you keep track of with this
* component.
*/
package main
import (
"errors"
"fmt"
"net/url"
"regexp"
"strings"
"time"
)
type Episode struct {
ID int64 `json:"id"`
ShowID int64 `json:"show_id" binding:"required"`
Season int `json:"season"`
Episode int `json:"episode"`
AirDate string `json:"airdate"`
Added int64 `json:"added"`
}
func newEpisode(sid int64, s, e int) *Episode {
return &Episode{
ShowID: sid,
Season: s,
Episode: e,
Added: time.Now().UnixNano(),
}
}
func newDaily(sid int64, t, d string) *Episode {
return &Episode{
ShowID: sid,
AirDate: d,
Added: time.Now().UnixNano(),
}
}
// Start User Functions
func (e *Episode) AddEpisode() (err error) {
e.Added = time.Now().UnixNano()
checkDBLock<- 1
err = gDb.Insert(e)
<-checkDBLock
show, err := GetShow(e.ShowID)
if err == nil {
show.LastUpdate = e.Added
err = show.UpdateShow()
}
return err
}
func (e *Episode) IsNewEpisode() bool {
checkDBLock<- 1
err := gDb.SelectOne(&Episode{}, "select ID from episode where ShowID=? and Season=? and Episode=? and AirDate=?",
e.ShowID, e.Season, e.Episode, e.AirDate)
<-checkDBLock
if err == nil {
return false
}
return true
}
func (e *Episode) ValidEpisodeQuality(s string) bool {
isHDTV := episodeQualityRegexp.MatchString(s)
show, _ := GetShow(e.ShowID)
if show.Quality == "420" || show.Quality == "" {
return !isHDTV
} else {
return show.Quality == episodeQualityRegexp.FindString(s)
}
}
func GetEpisodesByShowID(id int64) (allE *[]Episode, err error) {
checkDBLock<- 1
_, err = gDb.Select(allE, "select * from episode where ShowID=?", id)
<-checkDBLock
return
}
func GetLastEpisode(sid int64) (le *Episode, err error) {
checkDBLock<- 1
err = gDb.SelectOne(le, "select * from episode where ShowID=? sort by AirDate, Season, Episode desc limit 1", sid)
<-checkDBLock
return
}
func ParseTorrentString(e string) (episode *Episode, err error) {
eMatch, err := matchEpisodeToPattern(e)
if err != nil {
return
}
sid, err := GetShowByTitle(episodeRewriter(eMatch["show"]))
if err != nil {
return nil, errors.New(fmt.Sprintf("Show %s is not being tracked.", episodeRewriter(eMatch["show"])))
}
episode.ShowID = sid.ID
episode.Season = GetInt(eMatch["season"])
episode.Episode = GetInt(eMatch["episode"])
episode.AirDate = eMatch["airdate"]
if eMatch["enum"] != "" {
episode.Season = GetInt(string(eMatch["enum"][0]))
episode.Episode = GetInt(string(eMatch["enum"][1:]))
}
return
}
// End User Functions
func episodeRewriter(ep string) string {
e := strings.Replace(ep, ".", " ", -1)
return strings.Title(e)
}
func updateEpisodeRegex() (err error) {
er, err := url.QueryUnescape(tc.IRC.EpisodeRegexp)
if err != nil {
return err
}
episodePattern, err = regexp.Compile(er)
return err
}
func matchEpisodeToPattern(e string) (named map[string]string, err error) {
match := episodePattern.FindAllStringSubmatch(e, -1)
if match == nil {
return nil, errors.New("string not matched regexp")
}
for i, n := range match[0] {
named[episodePattern.SubexpNames()[i]] = n
}
return
}