Skip to content

Commit

Permalink
Merge 10c404e into 56952d2
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoferron committed Sep 29, 2020
2 parents 56952d2 + 10c404e commit 62dceb1
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ language: python

python:
- '2.7'
- '3.4'
- '3.5'

install:
- pip install -r requirements.txt
- pip install pytest > /dev/null
- pip install pytest-cov > /dev/null
- pip install coveralls > /dev/null

script: py.test --cov dailymotion.py --cov-report term-missing TestDailymotion.py
script: py.test --cov dailymotion --cov-report term-missing TestDailymotion.py

after_success:
- coveralls
Expand Down
41 changes: 28 additions & 13 deletions TestDailymotion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import time
import os
import pytest
import sys

class TestA(unittest.TestCase):

Expand All @@ -17,6 +18,7 @@ def setUpClass(self):
self.password = config.PASSWORD
self.scope = ['manage_videos', 'manage_playlists', 'userinfo']
self.redirect_uri = config.REDIRECT_URI
self.file_path = config.VIDEO_PATH or './examples/video.mp4'
self.oauth_authorize_endpoint_url = config.OAUTH_AUTHORIZE_URL or 'https://api.dailymotion.com/oauth/authorize'
self.oauth_token_endpoint_url = config.OAUTH_TOKEN_URL or 'https://api.dailymotion.com/oauth/token'
self.session_file_directory = './data'
Expand Down Expand Up @@ -44,23 +46,20 @@ def test_get(self):
self.assertEqual(videos['has_more'], True)
self.assertEqual('list' in videos, True)
self.assertEqual(len(videos['list']) > 0, True)

@pytest.mark.skip

def test_set_grant_type(self):
d = dailymotion.Dailymotion()
self.assertRaises(dailymotion.DailymotionClientError, d.set_grant_type, 'password', api_secret=self.api_secret, scope=self.scope,
info={'username': self.username, 'password': self.password})
self.assertRaises(dailymotion.DailymotionClientError, d.set_grant_type, 'password', api_secret=self.api_secret, scope=self.scope)
self.assertRaises(dailymotion.DailymotionClientError, d.set_grant_type, 'password', api_secret=self.api_secret, scope=None)

@pytest.mark.skip

def test_get_authorization_url(self):
d = dailymotion.Dailymotion(api_base_url=self.api_base_url, oauth_authorize_endpoint_url=self.oauth_authorize_endpoint_url)
d.set_grant_type('authorization', api_key=self.api_key, api_secret=self.api_secret, scope=self.scope, info={'redirect_uri' : self.redirect_uri})
authorization_url = d.get_authorization_url(redirect_uri=self.redirect_uri, scope=self.scope)
self.assertEqual(re.match('https?://(?:www)?(?:[\w-]{2,255}(?:\.\w{2,6}){1,2})(?:/[\w&%?#-]{1,300})?',authorization_url) == None, False)

@pytest.mark.skip

def test_get_access_token(self):
d = dailymotion.Dailymotion(api_base_url=self.api_base_url,
oauth_authorize_endpoint_url=self.oauth_authorize_endpoint_url,
Expand All @@ -70,7 +69,6 @@ def test_get_access_token(self):
self.assertEqual(isinstance (access_token, str) or isinstance(access_token, unicode), True)
d.logout()

@pytest.mark.skip
def test_set_access_token(self):
d = dailymotion.Dailymotion()
d.set_grant_type('password', api_key=self.api_key, api_secret=self.api_secret, scope=self.scope, info={'username': self.username, 'password': self.password})
Expand All @@ -79,7 +77,6 @@ def test_set_access_token(self):
self.assertEqual(isinstance (response.get('fullname'), str) or isinstance(response.get('fullname'), unicode), True)
d.logout()

@pytest.mark.skip
def test_auth_call(self):
d = dailymotion.Dailymotion(api_base_url=self.api_base_url,
oauth_authorize_endpoint_url=self.oauth_authorize_endpoint_url,
Expand All @@ -91,21 +88,41 @@ def test_auth_call(self):
self.assertEqual(isinstance (response.get('fullname'), str) or isinstance(response.get('fullname'), unicode), True)
d.logout()

@pytest.mark.skip
def test_upload(self):
d = dailymotion.Dailymotion(api_base_url=self.api_base_url,
oauth_authorize_endpoint_url=self.oauth_authorize_endpoint_url,
oauth_token_endpoint_url=self.oauth_token_endpoint_url,
session_store_enabled=True)

d.set_grant_type('password', api_key=self.api_key, api_secret=self.api_secret, scope=self.scope, info={'username': self.username, 'password': self.password})
url = d.upload('./examples/video.mp4')
url = d.upload(self.file_path)
self.assertEqual(re.match('https?://(?:www)?(?:[\w-]{2,255}(?:\.\w{2,6}){1,2})(?:/[\w&%?#-]{1,300})?',url) == None, False)
video = d.post('/videos', {'url' : url,
'title' : 'my_test_upload_%s' % time.strftime("%c"),
'published' : 'true',
'channel' : 'news'
})
self.assertEqual('id' in video, True)
d.delete('/video/%s' % video['id'])
d.logout()

@pytest.mark.skipif(sys.version_info < (3, 5), reason="requires python3.5 or higher")
def test_xupload(self):
d = dailymotion.Dailymotion(api_base_url=self.api_base_url,
oauth_authorize_endpoint_url=self.oauth_authorize_endpoint_url,
oauth_token_endpoint_url=self.oauth_token_endpoint_url,
session_store_enabled=True)

d.set_grant_type('password', api_key=self.api_key, api_secret=self.api_secret, scope=self.scope, info={'username': self.username, 'password': self.password})
url = d.upload(self.file_path, workers=5)
self.assertEqual(re.match('https?://(?:www)?(?:[\w-]{2,255}(?:\.\w{2,6}){1,2})(?:/[\w&%?#-]{1,300})?',url) == None, False)
d.post('/videos', {'url' : url,
video = d.post('/videos', {'url' : url,
'title' : 'my_test_upload_%s' % time.strftime("%c"),
'published' : 'true',
'channel' : 'news'
})
self.assertEqual('id' in video, True)
d.delete('/video/%s' % video['id'])
d.logout()


Expand All @@ -119,7 +136,6 @@ def test_session_store_option(self):
d = dailymotion.Dailymotion(session_store_enabled=None)
self.assertEqual(d.DEFAULT_SESSION_STORE, d._session_store_enabled)

@pytest.mark.skip
def test_in_memory_session(self):
d = dailymotion.Dailymotion(api_base_url=self.api_base_url,
oauth_authorize_endpoint_url=self.oauth_authorize_endpoint_url,
Expand All @@ -133,7 +149,6 @@ def test_in_memory_session(self):
self.assertEqual(second_access_token, access_token)
d.logout()

@pytest.mark.skip
def test_file_storage_session(self):
fs = dailymotion.FileSessionStore(self.session_file_directory)
d = dailymotion.Dailymotion(api_base_url=self.api_base_url,
Expand Down
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
USERNAME = os.getenv('DM_USERNAME', '[YOUR USERNAME]')
PASSWORD = os.getenv('DM_PASSWORD', '[YOUR PASSWORD]')
REDIRECT_URI = os.getenv('DM_REDIRECT_URI', '[YOUR REDIRECT URI]')
VIDEO_PATH = os.getenv('DM_VIDEO_PATH')
BASE_URL = 'https://api.dailymotion.com'
OAUTH_AUTHORIZE_URL = 'https://www.dailymotion.com/oauth/authorize'
OAUTH_TOKEN_URL = 'https://api.dailymotion.com/oauth/token'
38 changes: 26 additions & 12 deletions dailymotion.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from collections import defaultdict

__author__ = 'Samir AMZANI <samir.amzani@gmail.com>'
__version__ = '0.2.4'
__version__ = '0.2.5'
__python_version__ = '.'.join([str(i) for i in sys.version_info[:3]])

try:
Expand All @@ -25,6 +25,10 @@
except ImportError: # Python < 2.6
from cgi import parse_qsl

if sys.version_info > (3, 5):
import xupload
else: # Python < 3.5
xupload = None

class DailymotionClientError(Exception):
def __init__(self, message, error_type=None):
Expand Down Expand Up @@ -334,7 +338,7 @@ def call(self, endpoint, method='GET', params=None, files=None):

return self.request(endpoint, method, params, files)

def upload(self, file_path, progress=None):
def upload(self, file_path, progress=None, workers=0):
if not os.path.exists(file_path):
raise IOError("[Errno 2] No such file or directory: '%s'" % file_path)

Expand All @@ -344,20 +348,30 @@ def upload(self, file_path, progress=None):
file_path = os.path.abspath(os.path.expanduser(file_path))

result = self.get('/file/upload')

m = MultipartEncoder(fields={'file': (os.path.basename(file_path), open(file_path, 'rb'))})

headers = {
'User-Agent': 'Dailymotion-Python/%s (Python %s)' % (__version__, __python_version__),
'Content-Type': m.content_type
'User-Agent': 'Dailymotion-Python/%s (Python %s)' % (__version__, __python_version__)
}

r = requests.post(result['upload_url'], data=m, headers=headers, timeout=self.timeout)
if workers > 0 and xupload:
x = xupload.Xupload(
result['upload_url'],
file_path,
workers=workers,
headers=headers,
progress=progress
)
response = x.start()
else:
m = MultipartEncoder(fields={'file': (os.path.basename(file_path), open(file_path, 'rb'))})
headers['Content-Type'] = m.content_type

r = requests.post(result['upload_url'], data=m, headers=headers, timeout=self.timeout)

try:
response = json.loads(r.text)
except ValueError as e:
raise DailymotionUploadInvalidResponse('Invalid API server response.\n%s' % str(e))

try:
response = json.loads(r.text)
except ValueError as e:
raise DailymotionUploadInvalidResponse('Invalid API server response.\n%s' % response)
if 'error' in response:
raise DailymotionUploadError(response['error'])

Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
requests
requests_toolbelt
pytest
aiohttp!=4.0.0a1;python_version>"3.4"
aiofiles;python_version>"3.4"
asyncio;python_version>"3.4"
10 changes: 7 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os, sys

setup(name='dailymotion',
version='0.2.4',
version='0.2.5',
description='Dailymotion API SDK',
long_description='Dailymotion API SDK',
classifiers=[
Expand All @@ -22,9 +22,13 @@
license='Apache License, Version 2.0',
include_package_data=True,
zip_safe=False,
py_modules = ['dailymotion',],
py_modules = ['dailymotion','xupload'],
setup_requires=["wheel"],
install_requires=[
'requests',
'requests_toolbelt'
'requests_toolbelt',
'aiohttp;python_version>"3.4"',
'aiofiles;python_version>"3.4"',
'asyncio;python_version>"3.4"'
],
)
Loading

0 comments on commit 62dceb1

Please sign in to comment.