- def __init__(self, title, parent, url):
- Node.__init__(self, title, parent)
- self.url = url
- self.can_download = True
-
- def get_vid(self):
- doc = grab_html(self.url, 3600)
- for script in doc.xpath("//script"):
- if not script.text:
- continue
- for line in script.text.split("\n"):
- if line.find("vid : ") <= 0:
- continue
- vid = line[line.find("'")+1 : line.rfind("'")]
- vid = int(vid)
- return vid
- raise Exception("Could not find vid on page " + self.url)
-
- def download(self):
- vid = self.get_vid()
- doc = grab_html(METADATA % vid, 0)
- content = doc.xpath("//content")[0]
- vbase = content.attrib["url"]
- vpath = content.attrib["path"]
- filename = self.title + ".flv"
- return download_rtmp(filename, vbase, vpath, HASH_URL)
+ def __init__(self, title, parent, url):
+ Node.__init__(self, title, parent)
+ self.url = url
+ self.can_download = True
+
+ def get_video_id(self):
+ doc = grab_html(self.url, 3600)
+ for script in doc.xpath("//script"):
+ if not script.text:
+ continue
+ for line in script.text.split(";"):
+ line = line.strip()
+ if line.find("new Y.VideoPlatform.VideoPlayer") <= 0:
+ continue
+
+### vidparams = line[line.find("(")+1 : line.rfind(")")]
+### vidparams = json.loads(vidparams)
+### return vidparams["playlist"]["mediaItems"][0]["id"]
+
+ # Cannot parse it as JSON :(
+ pos1 = line.find('"mediaItems":')
+ if pos1 < 0:
+ continue
+ pos2 = line.find('"id":', pos1)
+ if pos2 < 0:
+ continue
+ pos3 = line.find('"', pos2+5)
+ pos4 = line.find('"', pos2+6)
+ if pos3 < 0 or pos4 < 0:
+ continue
+ return line[pos3+1:pos4]
+
+ raise Exception("Could not find video id on page " + self.url)
+
+ def generate_session(self):
+ return "".join([random.choice(string.ascii_letters) for x in xrange(22)])
+
+ def download(self):
+ vid_id = self.get_video_id()
+ qs = dict(METADATA_QUERY.items()) # copy..
+ qs["q"] = qs["q"] % (vid_id, self.generate_session())
+ url = METADATA_BASE + urllib.urlencode(qs)
+ doc = grab_json(url, 0, skip_function=True)
+ stream_data = doc["query"]["results"]["mediaObj"][0]["streams"][0]
+ vbase = stream_data["host"]
+ vpath = stream_data["path"]
+ filename = self.title + ".flv"
+ return download_rtmp(filename, vbase, vpath, HASH_URL)