forked from filebot/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-mes.groovy
114 lines (93 loc) · 3.2 KB
/
update-mes.groovy
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
#!/usr/bin/env filebot -script
def mesacc = login.split(':')
def mesadd = tryQuietly{ addshows.toBoolean() }
def mesupdate = tryQuietly { tick } ?: 'acquired'
def mesvalue = tryQuietly { value } ?: '1'
def mes = new MyEpisodesScraper(username:mesacc[0], password:mesacc[1])
def myshows = mes.getShowList()
// series name => series key (e.g. Doctor Who (2005) => doctorwho)
def collationKey = { s -> s == null ? '' : s.removeAll(/^(?i)(The|A)\b/).removeAll(/\(?\d{4}\)?$/).removeAll(/\W/).lower() }
args.getFiles().findAll{ it.isVideo() && parseEpisodeNumber(it) && detectSeriesName(it) }.groupBy{ detectSeriesName(it) }.each{ series, files ->
def show = myshows.find{ collationKey(it.name) == collationKey(series) }
if (show == null && mesadd) {
show = mes.getShows().find{ collationKey(it.name) == collationKey(series) }
if (show == null) {
println "[failure] '$series' not found"
return
}
mes.addShow(show.id)
println "[added] $show.name"
}
files.each{
if (show != null) {
def sxe = parseEpisodeNumber(it)
mes.update(show.id, sxe.season, sxe.episode, mesupdate, mesvalue)
println "[$mesupdate] $show.name $sxe [$it.name]"
} else {
println "[failure] '$series' has not been added [$it.name]"
}
}
}
/****************************************************************************
* MyEpisodes
* http://www.myepisodes.com
****************************************************************************/
import org.jsoup.Jsoup
import org.jsoup.Connection.Method
class MyEpisodesScraper {
def username
def password
def host = "https://www.myepisodes.com"
def cache = Cache.getCache('myepisodes', CacheType.Monthly)
def session = [:]
def login() {
def response = Jsoup.connect("${host}/login.php").data('username', username, 'password', password, 'action', 'Login', 'u', '').method(Method.POST).execute()
session << response.cookies()
return response.parse()
}
def get(url) {
if (!session) {
login()
}
def response = Jsoup.connect(url).cookies(session).method(Method.GET).execute()
session << response.cookies()
def html = response.parse()
if (html.select('#frmLogin')) {
session.clear()
throw new Exception('Bad Login')
}
return html
}
def getShows() {
def shows = cache.get('MyEpisodes.Shows')
if (shows == null) {
shows = ['other', 'A'..'Z'].flatten().findResults{ section ->
get("${host}/shows.php?list=${section}").select('a').findResults{ a ->
try {
// e.g. http://www.myepisodes.com/epsbyshow/491/The%20A-Team
return [id:a.absUrl('href').match(/\/(\d+)\//).toInteger(), name:a.text().trim()]
} catch(e) {
return null
}
}
}.flatten().sort{ it.name }
cache.put('MyEpisodes.Shows', shows)
}
return shows
}
def getShowList() {
get("${host}/shows.php?type=manage").select('option').findResults{ option ->
try {
return [id:option.attr('value').toInteger(), name:option.text().trim()]
} catch(e) {
return null
}
}
}
def addShow(showid) {
get("${host}/views.php?type=manageshow&mode=add&showid=${showid}")
}
def update(showid, season, episode, tick = 'acquired', value = '1') {
get("${host}/myshows.php?action=Update&showid=${showid}&season=${season}&episode=${episode}&${tick}=${value}")
}
}