forked from commandercool/serviio-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewComedyCentral.groovy
138 lines (113 loc) · 4.19 KB
/
NewComedyCentral.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.net.URL;
import java.net.URLEncoder
import org.serviio.library.metadata.*
import org.serviio.library.online.*
import groovy.json.*
/**
* <h1>The New Comedy Central Serviio plugin</h1>
*
* <h2>Usage instructions</h2>
* <p>Add streams as a <strong>Web Resource</strong> with
* "<i>http://thecolbertreport.cc.com/full-episodes/...</i>" or
* "<i>http://thedailyshow.cc.com/full-episodes/...</i>" or
* "<i>http://tosh.comedycentral.com/episodes/...</i>" or
* "<i>http://beta.southparkstudios.com/full-episodes/...</i>" or
* "<i>http://www.cc.com/episodes/...</i>"
* as URL.</p>
*
* <h2>VERSION HISTORY</h2>
* <p><ul>
* <li>V1 (25.03.2014): initial release</li>
* </ul></p>
*
* @version 1
* @author <a href="https://twitter.com/bogenpirat">bog</a>
*
*/
class NewComedyCentral extends WebResourceUrlExtractor {
final Integer VERSION = 1
def VALID_FEED_URL = ~"http://www.cc.com/episodes/.*|http://beta.southparkstudios.com/full-episodes/.*|http://thedailyshow.cc.com/full-episodes/.*|http://thecolbertreport.cc.com/full-episodes/.*|http://tosh.comedycentral.com/episodes/.*"
def REGEX_MGID = ~/data-mgid="([^"]+)"/
def REGEX_ACTS = ~/<media:content[^>]*medium="video"[^>]*url="([^"]+)"[^>]*>/
def REGEX_QUALITIES = ~/<rendition[^>]*width="([^"]+)"[^>]*height="([^"]+)"[^>]*bitrate="([^"]+)"[^>]*>\s*<src>([^<]+)<\/src>\s*<\/rendition>/
def REGEX_RTMPTOHTTP = ~/^rtmpe?:\/\/.*?\/(gsp\.comedystor\/.*)/
def REGEX_TITLE = ~/<meta property="og:title" content="([^"]+)"\/>/
def mrssUrl = "http://thedailyshow.cc.com/feeds/mrss?uri=%s"
def httpUrl = "http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/"
final static Boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
int getVersion() {
return VERSION
}
String getExtractorName() {
return 'NewComedyCentral'
}
boolean extractorMatches(URL feedUrl) {
return (feedUrl =~ VALID_FEED_URL).find()
}
WebResourceContainer extractItems(URL resourceUrl, int maxItemsToRetrieve) {
// prepare list of qualities/acts
def segments = [:]
// extract mgid and title
def mgid, title
def m = resourceUrl.text =~ REGEX_MGID
if(m.find()) {
mgid = m.group(1)
}
m = resourceUrl.text =~ REGEX_TITLE
if(m.find()) {
title = m.group(1)
}
// extract act/quality urls
m = new URL(String.format(mrssUrl, URLEncoder.encode(mgid))).text =~ REGEX_ACTS
while(m.find()) {
def actUrl = new URL(m.group(1))
def qualitiesXml = actUrl.text
def m2 = qualitiesXml =~ REGEX_QUALITIES
def qualityId = "", oldQualityId = ""
while(m2.find()) {
qualityId = "${m2.group(2)}p@${m2.group(3)}K"
if(oldQualityId.equals(qualityId))
continue
if(segments[qualityId] == null) segments[qualityId] = []
def m3, segUrl
if((m3 = m2.group(4) =~ REGEX_RTMPTOHTTP).find()) {
segUrl = httpUrl + m3.group(1)
}
segments[qualityId] << segUrl
oldQualityId = qualityId
}
}
// reverse quality order so we get the best atop, and create web containers
def items = []
segments.reverseEach { quality, val ->
def concatUrls = val.join("|")
def myUrl = isWindows ? "\"concat:${concatUrls}\"" : "concat:${concatUrls}"
items << new WebResourceItem(title: "[$quality] " + title, additionalInfo: [
expiresImmediately: false,
cacheKey: title,
url: myUrl ])
}
// create and fill the container
def container = new WebResourceContainer()
container.setTitle(title)
container.setItems(items)
return container
}
ContentURLContainer extractUrl(WebResourceItem arg0, PreferredQuality arg1) {
def c = new ContentURLContainer()
if(arg0 != null) {
c.setExpiresImmediately(false)
c.setCacheKey(arg0.additionalInfo.cacheKey)
c.setContentUrl(arg0.additionalInfo.url)
c.setLive(false)
}
return c
}
static void main(args) {
NewComedyCentral cc = new NewComedyCentral()
cc.extractItems(new URL(args[0]), 123).getItems().each { it ->
ContentURLContainer result = cc.extractUrl(it, PreferredQuality.HIGH)
println result
}
}
}