mirror of
https://git.lecygnenoir.info/LecygneNoir/prismedia.git
synced 2025-10-03 17:39:16 +02:00
pep8
This commit is contained in:
parent
117e58a807
commit
010931d8b7
3 changed files with 152 additions and 125 deletions
|
@ -3,10 +3,8 @@
|
|||
|
||||
import os
|
||||
import mimetypes
|
||||
import httplib
|
||||
import httplib2
|
||||
import json
|
||||
import array
|
||||
from os.path import splitext, basename, abspath
|
||||
|
||||
from ConfigParser import RawConfigParser
|
||||
from requests_oauthlib import OAuth2Session
|
||||
|
@ -15,44 +13,58 @@ from requests_toolbelt.multipart.encoder import MultipartEncoder
|
|||
|
||||
PEERTUBE_SECRETS_FILE = 'peertube_secret'
|
||||
|
||||
|
||||
def get_authenticated_service(config):
|
||||
oauth = OAuth2Session(client=LegacyApplicationClient(client_id=str(config.get('peertube', 'client_id'))))
|
||||
oauth.fetch_token(token_url=str(config.get('peertube', 'peertube_url')) + '/api/v1/users/token',
|
||||
username=str(config.get('peertube', 'username').lower()), #lower as peertube does not store uppecase for pseudo
|
||||
peertube_url = str(config.get('peertube', 'peertube_url'))
|
||||
|
||||
oauth_client = LegacyApplicationClient(
|
||||
client_id=str(config.get('peertube', 'client_id'))
|
||||
)
|
||||
oauth = OAuth2Session(client=oauth_client)
|
||||
oauth.fetch_token(
|
||||
token_url=peertube_url + '/api/v1/users/token',
|
||||
# lower as peertube does not store uppecase for pseudo
|
||||
username=str(config.get('peertube', 'username').lower()),
|
||||
password=str(config.get('peertube', 'password')),
|
||||
client_id=str(config.get('peertube', 'client_id')),
|
||||
client_secret=str(config.get('peertube', 'client_secret'))
|
||||
)
|
||||
return oauth
|
||||
|
||||
|
||||
def upload_video(oauth, config, options):
|
||||
|
||||
def get_userinfo():
|
||||
user_info = json.loads(oauth.get(url+"/api/v1/users/me").content)
|
||||
user_info = json.loads(oauth.get(url + "/api/v1/users/me").content)
|
||||
return str(user_info["id"])
|
||||
|
||||
def get_videofile(path):
|
||||
mimetypes.init()
|
||||
return (os.path.basename(path), open(os.path.abspath(path), 'rb'),
|
||||
mimetypes.types_map[os.path.splitext(path)[1]])
|
||||
return (basename(path), open(abspath(path), 'rb'),
|
||||
mimetypes.types_map[splitext(path)[1]])
|
||||
|
||||
path = options.get('--file')
|
||||
url = config.get('peertube', 'peertube_url')
|
||||
tags = None
|
||||
tags_tuple=[]
|
||||
|
||||
# We need to transform fields into tuple to deal with tags as MultipartEncoder does not support list
|
||||
# refer https://github.com/requests/toolbelt/issues/190 and https://github.com/requests/toolbelt/issues/205
|
||||
# We need to transform fields into tuple to deal with tags as
|
||||
# MultipartEncoder does not support list refer
|
||||
# https://github.com/requests/toolbelt/issues/190 and
|
||||
# https://github.com/requests/toolbelt/issues/205
|
||||
fields = [
|
||||
("name", options.get('--name') or os.path.splitext(os.path.basename(path))[0]),
|
||||
("category", str(options.get('--category') or 1)), # look at the list numbers at /videos/categories
|
||||
("licence", str(options.get('--licence') or 1)), # look at the list numbers at /videos/licences
|
||||
("name", options.get('--name') or splitext(basename(path))[0]),
|
||||
# look at the list numbers at /videos/categories
|
||||
("category", str(options.get('--category') or 1)),
|
||||
# look at the list numbers at /videos/licences
|
||||
("licence", str(options.get('--licence') or 1)),
|
||||
("description", options.get('--description') or "default description"),
|
||||
("privacy", str(options.get('--privacy') or 3)), # look at the list numbers at /videos/privacies
|
||||
# look at the list numbers at /videos/privacies
|
||||
("privacy", str(options.get('--privacy') or 3)),
|
||||
("nsfw", str(options.get('--nsfw') or 0)),
|
||||
("commentsEnabled", "1"),
|
||||
("channelId", get_userinfo()),
|
||||
("videofile", get_videofile(path)) # beware, see validateVideo for supported types
|
||||
# beware, see validateVideo for supported types
|
||||
("videofile", get_videofile(path))
|
||||
]
|
||||
|
||||
if options.get('--tags'):
|
||||
|
@ -67,21 +79,25 @@ def upload_video(oauth, config, options):
|
|||
'Content-Type': multipart_data.content_type
|
||||
}
|
||||
|
||||
response = oauth.post(config.get('peertube', 'peertube_url')+"/api/v1/videos/upload", data=multipart_data, headers=headers)
|
||||
response = oauth.post(url + "/api/v1/videos/upload",
|
||||
data=multipart_data,
|
||||
headers=headers)
|
||||
if response is not None:
|
||||
if response.status_code == 200:
|
||||
print 'Peertube : Video was successfully uploaded.'
|
||||
print('Peertube : Video was successfully uploaded.')
|
||||
else:
|
||||
exit('Peertube : The upload failed with an unexpected response: %s' % response)
|
||||
exit(('Peertube : The upload failed with an unexpected response: '
|
||||
'%s') % response)
|
||||
|
||||
|
||||
def run(options):
|
||||
config = RawConfigParser()
|
||||
config.read(PEERTUBE_SECRETS_FILE)
|
||||
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = config.get('peertube', 'OAUTHLIB_INSECURE_TRANSPORT')
|
||||
insecure_transport = config.get('peertube', 'OAUTHLIB_INSECURE_TRANSPORT')
|
||||
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = insecure_transport
|
||||
oauth = get_authenticated_service(config)
|
||||
try:
|
||||
print 'Peertube : Uploading file...'
|
||||
print('Peertube : Uploading file...')
|
||||
upload_video(oauth, config, options)
|
||||
except Exception as e:
|
||||
if hasattr(e, 'message'):
|
||||
|
|
|
@ -1,18 +1,15 @@
|
|||
#!/usr/bin/python
|
||||
# coding: utf-8
|
||||
# From Youtube samples : https://raw.githubusercontent.com/youtube/api-samples/master/python/upload_video.py
|
||||
# From Youtube samples : https://raw.githubusercontent.com/youtube/api-samples/master/python/upload_video.py # noqa
|
||||
|
||||
import argparse
|
||||
import httplib
|
||||
import httplib2
|
||||
import os
|
||||
import random
|
||||
import time
|
||||
import copy
|
||||
import json
|
||||
|
||||
from os.path import splitext, basename, exists
|
||||
import google.oauth2.credentials
|
||||
import google_auth_oauthlib.flow
|
||||
|
||||
from googleapiclient.discovery import build
|
||||
from googleapiclient.errors import HttpError
|
||||
|
@ -28,10 +25,17 @@ httplib2.RETRIES = 1
|
|||
MAX_RETRIES = 10
|
||||
|
||||
# Youtube retriables cases
|
||||
RETRIABLE_EXCEPTIONS = (httplib2.HttpLib2Error, IOError, httplib.NotConnected,
|
||||
httplib.IncompleteRead, httplib.ImproperConnectionState,
|
||||
httplib.CannotSendRequest, httplib.CannotSendHeader,
|
||||
httplib.ResponseNotReady, httplib.BadStatusLine)
|
||||
RETRIABLE_EXCEPTIONS = (
|
||||
IOError,
|
||||
httplib2.HttpLib2Error,
|
||||
httplib.NotConnected,
|
||||
httplib.IncompleteRead,
|
||||
httplib.ImproperConnectionState,
|
||||
httplib.CannotSendRequest,
|
||||
httplib.CannotSendHeader,
|
||||
httplib.ResponseNotReady,
|
||||
httplib.BadStatusLine,
|
||||
)
|
||||
|
||||
RETRIABLE_STATUS_CODES = [500, 502, 503, 504]
|
||||
|
||||
|
@ -45,8 +49,9 @@ API_VERSION = 'v3'
|
|||
|
||||
# Authorize the request and store authorization credentials.
|
||||
def get_authenticated_service():
|
||||
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
|
||||
if os.path.exists(CREDENTIALS_PATH):
|
||||
flow = InstalledAppFlow.from_client_secrets_file(
|
||||
CLIENT_SECRETS_FILE, SCOPES)
|
||||
if exists(CREDENTIALS_PATH):
|
||||
with open(CREDENTIALS_PATH, 'r') as f:
|
||||
credential_params = json.load(f)
|
||||
credentials = google.oauth2.credentials.Credentials(
|
||||
|
@ -62,7 +67,8 @@ def get_authenticated_service():
|
|||
p = copy.deepcopy(vars(credentials))
|
||||
del p["expiry"]
|
||||
json.dump(p, f)
|
||||
return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)
|
||||
return build(API_SERVICE_NAME, API_VERSION, credentials=credentials)
|
||||
|
||||
|
||||
def initialize_upload(youtube, options):
|
||||
path = options.get('--file')
|
||||
|
@ -70,17 +76,15 @@ def initialize_upload(youtube, options):
|
|||
if options.get('--tags'):
|
||||
tags = options.get('--tags').split(',')
|
||||
|
||||
body=dict(
|
||||
snippet=dict(
|
||||
title=options.get('--name') or os.path.splitext(os.path.basename(path))[0],
|
||||
description=options.get('--description') or "",
|
||||
tags=tags,
|
||||
categoryId=str(options.get('--category') or 1),
|
||||
),
|
||||
status=dict(
|
||||
privacyStatus=str(options.get('--privacy') or "private"),
|
||||
)
|
||||
)
|
||||
body = {
|
||||
"snippet": {
|
||||
"title": options.get('--name') or splitext(basename(path))[0],
|
||||
"description": options.get('--description') or "",
|
||||
"tags": tags,
|
||||
"categoryId": str(options.get('--category') or 1),
|
||||
},
|
||||
"status": {"privacyStatus": str(options.get('--privacy') or "private")}
|
||||
}
|
||||
|
||||
# Call the API's videos.insert method to create and upload the video.
|
||||
insert_request = youtube.videos().insert(
|
||||
|
@ -91,6 +95,7 @@ def initialize_upload(youtube, options):
|
|||
|
||||
resumable_upload(insert_request)
|
||||
|
||||
|
||||
# This method implements an exponential backoff strategy to resume a
|
||||
# failed upload.
|
||||
def resumable_upload(request):
|
||||
|
@ -99,31 +104,36 @@ def resumable_upload(request):
|
|||
retry = 0
|
||||
while response is None:
|
||||
try:
|
||||
print 'Youtube : Uploading file...'
|
||||
print('Youtube : Uploading file...')
|
||||
status, response = request.next_chunk()
|
||||
if response is not None:
|
||||
if 'id' in response:
|
||||
print 'Youtube : Video id "%s" was successfully uploaded.' % response['id']
|
||||
template = ('Youtube : Video id "%s" was successfully '
|
||||
'uploaded.')
|
||||
print(template % response['id'])
|
||||
else:
|
||||
exit('Youtube : The upload failed with an unexpected response: %s' % response)
|
||||
except HttpError, e:
|
||||
template = ('Youtube : The upload failed with an '
|
||||
'unexpected response: %s')
|
||||
exit(template % response)
|
||||
except HttpError as e:
|
||||
if e.resp.status in RETRIABLE_STATUS_CODES:
|
||||
error = 'Youtube : A retriable HTTP error %d occurred:\n%s' % (e.resp.status,
|
||||
e.content)
|
||||
template = 'Youtube : A retriable HTTP error %d occurred:\n%s'
|
||||
error = template % (e.resp.status, e.content)
|
||||
else:
|
||||
raise
|
||||
except RETRIABLE_EXCEPTIONS, e:
|
||||
except RETRIABLE_EXCEPTIONS as e:
|
||||
error = 'Youtube : A retriable error occurred: %s' % e
|
||||
|
||||
if error is not None:
|
||||
print error
|
||||
print(error)
|
||||
retry += 1
|
||||
if retry > MAX_RETRIES:
|
||||
exit('Youtube : No longer attempting to retry.')
|
||||
|
||||
max_sleep = 2 ** retry
|
||||
sleep_seconds = random.random() * max_sleep
|
||||
print 'Youtube : Sleeping %f seconds and then retrying...' % sleep_seconds
|
||||
print('Youtube : Sleeping %f seconds and then retrying...'
|
||||
% sleep_seconds)
|
||||
time.sleep(sleep_seconds)
|
||||
|
||||
|
||||
|
@ -131,5 +141,6 @@ def run(options):
|
|||
youtube = get_authenticated_service()
|
||||
try:
|
||||
initialize_upload(youtube, options)
|
||||
except HttpError, e:
|
||||
print 'Youtube : An HTTP error %d occurred:\n%s' % (e.resp.status, e.content)
|
||||
except HttpError as e:
|
||||
print('Youtube : An HTTP error %d occurred:\n%s' % (e.resp.status,
|
||||
e.content))
|
||||
|
|
|
@ -15,14 +15,17 @@ Options:
|
|||
-t, --tags=STRING Tags for the video. comma separated
|
||||
-h --help Show this help.
|
||||
--version Show version.
|
||||
|
||||
|
||||
|
||||
"""
|
||||
import argparse
|
||||
from os.path import dirname, realpath
|
||||
from sys.path import insert
|
||||
|
||||
from docopt import docopt
|
||||
|
||||
import yt_upload
|
||||
import pt_upload
|
||||
|
||||
try:
|
||||
from schema import Schema, And, Or, Use, Optional, SchemaError
|
||||
from schema import Schema, And, Or, Optional, SchemaError
|
||||
except ImportError:
|
||||
exit('This program requires that the `schema` data-validation library'
|
||||
' is installed: \n'
|
||||
|
@ -34,9 +37,10 @@ except ImportError:
|
|||
' is installed, NOT the Python bindings to libmagic API \n'
|
||||
'see https://github.com/ahupp/python-magic\n')
|
||||
|
||||
VERSION="ptyt 0.1-alpha"
|
||||
VERSION = "ptyt 0.1-alpha"
|
||||
VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted')
|
||||
|
||||
|
||||
def validateVideo(path):
|
||||
supported_types = ['video/mp4']
|
||||
if magic.from_file(path, mime=True) in supported_types:
|
||||
|
@ -44,19 +48,15 @@ def validateVideo(path):
|
|||
else:
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
#Allows you to a relative import from the parent folder
|
||||
import os.path, sys
|
||||
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))+"/lib")
|
||||
|
||||
import yt_upload
|
||||
import pt_upload
|
||||
# Allows you to a relative import from the parent folder
|
||||
insert(0, dirname(realpath(__file__)) + "/lib")
|
||||
|
||||
options = docopt(__doc__, version=VERSION)
|
||||
|
||||
schema = Schema({
|
||||
'--file': And(str, lambda s: validateVideo(s), error='file is not supported, please use mp4'),
|
||||
'--file': And(str, validateVideo, error='file is not supported, please use mp4'),
|
||||
Optional('--name'): Or(None, And(str, lambda x: not x.isdigit(), error="The video name should be a string")),
|
||||
Optional('--description'): Or(None, And(str, lambda x: not x.isdigit(), error="The video name should be a string")),
|
||||
Optional('--tags'): Or(None, And(str, lambda x: not x.isdigit(), error="Tags should be a string")),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue