diff --git a/lib/pt_upload.py b/lib/pt_upload.py index f112124..48fe896 100644 --- a/lib/pt_upload.py +++ b/lib/pt_upload.py @@ -11,6 +11,8 @@ from requests_oauthlib import OAuth2Session from oauthlib.oauth2 import LegacyApplicationClient from requests_toolbelt.multipart.encoder import MultipartEncoder +import utils + PEERTUBE_SECRETS_FILE = 'peertube_secret' @@ -53,8 +55,6 @@ def upload_video(oauth, config, options): # https://github.com/requests/toolbelt/issues/205 fields = [ ("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"), @@ -63,7 +63,6 @@ def upload_video(oauth, config, options): ("nsfw", str(options.get('--nsfw') or 0)), ("commentsEnabled", "1"), ("channelId", get_userinfo()), - # beware, see validateVideo for supported types ("videofile", get_videofile(path)) ] @@ -72,7 +71,12 @@ def upload_video(oauth, config, options): for strtags in tags: fields.append(("tags", strtags)) - # multipart_data = MultipartEncoder(fields=fields) + if options.get('--category'): + fields.append(("category", str(utils.getCategory(options.get('--category'), 'peertube')))) + else: + #if no category, set default to 2 (Films) + fields.append(("category", "2")) + multipart_data = MultipartEncoder(fields) headers = { diff --git a/lib/utils.py b/lib/utils.py new file mode 100644 index 0000000..f2e2041 --- /dev/null +++ b/lib/utils.py @@ -0,0 +1,49 @@ +#!/usr/bin/python +# coding: utf-8 + + +YOUTUBE_CATEGORY = { + "music":10, + "films":1, + "vehicles":2, + "sport":17, + "travels":19, + "gaming":20, + "people":22, + "comedy":23, + "entertainment":24, + "news":25, + "how to":26, + "education":27, + "activism":29, + "science & technology":28, + "science":28, + "technology":28, + "animals":15 +} + +PEERTUBE_CATEGORY = { + "music":1, + "films":2, + "vehicles":3, + "sport":5, + "travels":6, + "gaming":7, + "people":8, + "comedy":9, + "entertainment":10, + "news":11, + "how to":12, + "education":13, + "activism":14, + "science & technology":15, + "science":15, + "technology":15, + "animals":16 +} + +def getCategory(category, type): + if type == "youtube": + return YOUTUBE_CATEGORY[category.lower()] + else: + return PEERTUBE_CATEGORY[category.lower()] \ No newline at end of file diff --git a/lib/yt_upload.py b/lib/yt_upload.py index 5ce7f20..31ce3d2 100644 --- a/lib/yt_upload.py +++ b/lib/yt_upload.py @@ -16,6 +16,7 @@ from googleapiclient.errors import HttpError from googleapiclient.http import MediaFileUpload from google_auth_oauthlib.flow import InstalledAppFlow +import utils # Explicitly tell the underlying HTTP transport library not to retry, since # we are handling retry logic ourselves. @@ -76,12 +77,17 @@ def initialize_upload(youtube, options): if options.get('--tags'): tags = options.get('--tags').split(',') + category = None + if options.get('--category'): + category = utils.getCategory(options.get('--category'), 'youtube') + body = { "snippet": { "title": options.get('--name') or splitext(basename(path))[0], - "description": options.get('--description') or "", + "description": options.get('--description') or "default description", "tags": tags, - "categoryId": str(options.get('--category') or 1), + #if no category, set default to 1 (Films) + "categoryId": str(category or 1), }, "status": {"privacyStatus": str(options.get('--privacy') or "private")} } diff --git a/ptyt_upload.py b/ptyt_upload.py index 0e381c4..f5f6044 100755 --- a/ptyt_upload.py +++ b/ptyt_upload.py @@ -13,14 +13,25 @@ Options: --name=NAME Name of the video to upload. default to video file name -d, --description=STRING Description of the video. -t, --tags=STRING Tags for the video. comma separated + -c, --category=STRING Category for the videos, see below. Default to films -h --help Show this help. --version Show version. + +Categories: + Category is the type of video you upload. Default is films. + Here are available categories from Peertube and Youtube: + music, films, vehicles, + sports, travels, gaming, people, + comedy, entertainment, news, + how to, education, activism, science & technology, + science, technology, animals """ from os.path import dirname, realpath import sys from docopt import docopt + # Allows you to a relative import from the parent folder sys.path.insert(0, dirname(realpath(__file__)) + "/lib") @@ -40,8 +51,15 @@ 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.2-alpha" VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted') +VALID_CATEGORIES = ( + "music", "films", "vehicles", + "sports", "travels", "gaming", "people", + "comedy", "entertainment", "news", + "how to", "education", "activism", "science & technology", + "science", "technology", "animals" + ) def validateVideo(path): @@ -51,6 +69,11 @@ def validateVideo(path): else: return False +def validateCategory(category): + if category.lower() in VALID_CATEGORIES: + return True + else: + return False if __name__ == '__main__': @@ -61,6 +84,7 @@ if __name__ == '__main__': 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")), + Optional('--category'): Or(None, And(str, validateCategory, error="Category not recognized, please see --help")), '--help': bool, '--version': bool })