What's new? | Help | Directory | Sign in
Google
veohdownloader
Python Veoh Downloader class with integrated wx GUI
  
  
  
  
    
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/python
"""
Copyright 2008 David Konsumer <konsumer@jetboystudio.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
"""

import httplib
import libxml2
import sys
import os
import urllib, urllib2

"""
Get info about veoh video files, and download them.
"""
class Veoh:
veoh_client_info={'clientGUID':'', 'version': '3.0.0'}
headers = {"Content-type": "application/xml", "User-agent": "veoh-3.0 service (MacOS; Safari; darwin)"}
info={}
pieces=[]
video_open=False
current_hash=''

"""
vid can be a permaLink id, veoh:// url, or http:// to a video page
"""
def __init__(self, vid, download_dir="~/Desktop", clientid='fZOXEICKQU8f16JDGGco9DmR4NmLvNZr8fSYvzUh0KpMKCxU5ZMs8D7rxk3sBhCR', log_dir="~/.veoh"):
self.veoh_client_info['clientGUID']=clientid
self.download_dir=os.path.expanduser(download_dir)
self.log_dir=os.path.expanduser(log_dir)

if not os.path.isdir(self.download_dir):
os.mkdir(self.download_dir)
if not os.path.isdir(self.log_dir):
os.mkdir(self.log_dir)
self.get_info(self.parse_url(vid))

"""
this is just a convenience function to interact with veoh services
it recursively pulls the URL until it comes through without error
"""
def get_url(self, url, data=''):
req = urllib2.Request(url)
for i in self.headers:
req.add_header(i,self.headers[i])
req.add_data(data)
try:
f = urllib2.urlopen(req)
except urllib2.HTTPError:
print "missed transfer, retrying."
return self.get_url(url, data)
return f.read()

"""
returns info about a video file
"""
def get_info(self, vid):
self.info={'vid':vid}
doc = libxml2.parseDoc(self.get_url('http://www.veoh.com/service/getMediaInfo.xml?%s' % urllib.urlencode(self.veoh_client_info), '<MediaIdList><MediaId permalinkId="%s"/></MediaIdList>' % self.info['vid']))
for i in ('FileHash', 'Size', 'Title', 'Extension', 'Duration', 'PieceHashFile','UrlRoot'):
result = doc.xpathEval('/Response/QueueEntry/Video/%s' % i)
self.info[i.lower()] = result[0].get_content().strip()
return self.info

"""
get's a list of video piece hash id's
"""
def get_piece_info(self):
xml = self.get_url(self.info['piecehashfile'],' ')
doc = libxml2.parseDoc(xml)
piecesXp = doc.xpathEval('/file/piece/@id')
self.pieces=[]
for i in piecesXp:
hash = i.getContent().strip()
self.pieces.append(hash)
# FIFO for .pop()
self.pieces.reverse()
return self.pieces


"""
start video transfer
"""
def open(self):
if not self.video_open:
self.get_piece_info()
self.__vf = open(os.path.join(self.download_dir,"%s%s" % (self.info['title'], self.info['extension'])),'wb')
self.video_open=True

"""
finish video transfer
"""
def close(self):
if self.video_open:
self.__vf.close()
self.video_open=False

"""
download and add a piece to the current video from the piece stack

returns length of data retrieved
"""
def get_piece(self):
if self.video_open:
self.current_hash = self.pieces.pop()
data = self.get_url('%s/cache/get.jsp?fileHash=%s&pieceHash=%s' % (self.info['urlroot'], self.info['filehash'], self.current_hash),' ')
self.__vf.write(data)
return len(data)
else:
return 0

"""
download a video
simple function used as reference for other front-ends
"""
def download(self):
print "saving download as %s%s" % (self.info['title'], self.info['extension'])
self.open()
total = len(self.pieces)
for count in range(0, total):
print "downloaded piece: %d/%d) %dK" % (count, total, self.get_piece()/1024)
self.close()
print "finished."

"""
wx progress dialog download
"""
def download_wx(self):
import wx
self.open()
total = len(self.pieces)
app = wx.PySimpleApp()
dlg = wx.ProgressDialog("Veoh Download",
"Downloading %s%s" % (self.info['title'], self.info['extension']),
maximum = total,
style = wx.PD_CAN_ABORT |
wx.PD_APP_MODAL |
wx.PD_ELAPSED_TIME |
wx.PD_ESTIMATED_TIME |
wx.PD_REMAINING_TIME)
for count in range(0, total):
self.get_piece()
try:
(keepGoing, skip) = dlg.Update(count+1)
except TypeError:
keepGoing = dlg.Update(count+1)

if (not keepGoing): # cancel pressed
break

self.close()
dlg.Destroy()

"""
this just returns the vid for a url, useful as a firefox URL parser
see instructions on how to setup firefox to use this script for veoh:// URLs:
http://jetboystudio.com/veoh
"""
def parse_url(self, url):
a=url.split('/')
if (a[0] == 'veoh:'):
a = a[2].split('permalinkId=')
a = a[1].split('&')
# download
if (a[1] == 'command=49347A0A-C783-4e92-ADCE-ED7C93207E58'):
return a[0]
else:
a=a[-1].split('?')
return a[0]


# end Veoh class

def main():
if (len(sys.argv) < 2):
print """\nUsage: %s URL
URL can be any veoh video page like
http://www.veoh.com/videos/v6525744grmT6Jhz?jalsdkj=234&blah=asd
or veoh://downloadVideo?permalinkId=v6525744grmT6Jhz&command=49347A0A-C783-4e92-ADCE-ED7C93207E58
it also supports permalink id's, so you can just use v6525744grmT6Jhz
""" % (sys.argv[0])
sys.exit()

v = Veoh(sys.argv[1])
v.download_wx()


if __name__ == '__main__':
main()
Show details Hide details

Change log

r5 by david.konsumer on Apr 27, 2008   Diff
After a system update, the fomat of the
dialog return value (with wx frontend) was
different.  Updated it to support both
styles.
Go to: 
Project members, sign in to write a code review

Older revisions

r4 by david.konsumer on Apr 18, 2008   Diff
Fix for  issue #1  (last packet not
downloading) and fail support for
missed packets (503 errors were
crashing the downloader)
r3 by david.konsumer on Apr 07, 2008   Diff
pieces dict was downloading out of
order. fixed but removed completion
tracking, may add it back later.
r2 by david.konsumer on Apr 07, 2008   Diff
Initial Import
All revisions of this file

File info

Size: 6222 bytes, 203 lines