forked from feroze/dailymotion_py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dailymotion.py
100 lines (60 loc) · 2.25 KB
/
dailymotion.py
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
#!/usr/bin/env python
BASE='https://api.dailymotion.com/json'
KEY='13fe53372252ffbcf7fc'
SECRET='1312afbc7a53a2f3c01fcf951878a05babd8bf4d'
USER='feroztest'
PASS='pitivitest'
OAUTH='https://api.dailymotion.com/oauth/token'
from httplib2 import Http
from urllib import urlencode
import json
import urllib2
#OAuth2 to fetch access_token and refresh_token
values = {'grant_type' : 'password',
'client_id' : KEY,
'client_secret' : SECRET,
'username' : USER,
'password' : PASS,
'scope':'write'
}
data = urlencode(values)
req = urllib2.Request(OAUTH, data)
response = urllib2.urlopen(req)
result=json.load(response)
access_token=result['access_token']
refresh_token=result['refresh_token']
#print access_token, refresh_token
UURL='?access_token='+access_token
#advanced api
job=json.dumps({"call":"file.upload","args":None})
data = urlencode(values)
req = urllib2.Request(BASE+UURL, job, {'content-type': 'application/json'})
response = urllib2.urlopen(req)
result=json.load(response)
temp= result['result']
upload_url= temp['upload_url']
#Post using multipart form data using module poster (sudo easy_install install poster)
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
# Register the streaming http handlers with urllib2
register_openers()
# headers contains the necessary Content-Type and Content-Length
# datagen is a generator object that yields the encoded parameters
datagen, headers = multipart_encode({"file": open("sample.3gp")})
request = urllib2.Request(upload_url, datagen, headers)
result=json.load(urllib2.urlopen(request))
v_url = result['url'] #video URL returned
job=json.dumps({"call":"video.create","args":{"url":v_url}})
data = urlencode(values)
req = urllib2.Request(BASE+UURL, job, {'content-type': 'application/json'})
response = urllib2.urlopen(req)
result=json.load(response)
temp=result['result']
id=temp['id']
print id
#publish video
job=json.dumps({"call":"video.edit","args":{"id":id,"title":"samples","tags":"pitivi","channel":"comedy"}})
data = urlencode(values)
req = urllib2.Request(BASE+UURL, job, {'content-type': 'application/json'})
response = urllib2.urlopen(req)
print "Upload successful - http://dailymotion.com/video/"+id