mirror of
https://git.lecygnenoir.info/LecygneNoir/prismedia.git
synced 2025-10-03 17:39:16 +02:00
add category options with conversion between peertube/youtube
This commit is contained in:
parent
c79309e091
commit
4c58f14cdf
4 changed files with 90 additions and 7 deletions
|
@ -11,6 +11,8 @@ from requests_oauthlib import OAuth2Session
|
||||||
from oauthlib.oauth2 import LegacyApplicationClient
|
from oauthlib.oauth2 import LegacyApplicationClient
|
||||||
from requests_toolbelt.multipart.encoder import MultipartEncoder
|
from requests_toolbelt.multipart.encoder import MultipartEncoder
|
||||||
|
|
||||||
|
import utils
|
||||||
|
|
||||||
PEERTUBE_SECRETS_FILE = 'peertube_secret'
|
PEERTUBE_SECRETS_FILE = 'peertube_secret'
|
||||||
|
|
||||||
|
|
||||||
|
@ -53,8 +55,6 @@ def upload_video(oauth, config, options):
|
||||||
# https://github.com/requests/toolbelt/issues/205
|
# https://github.com/requests/toolbelt/issues/205
|
||||||
fields = [
|
fields = [
|
||||||
("name", options.get('--name') or splitext(basename(path))[0]),
|
("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
|
# look at the list numbers at /videos/licences
|
||||||
("licence", str(options.get('--licence') or 1)),
|
("licence", str(options.get('--licence') or 1)),
|
||||||
("description", options.get('--description') or "default description"),
|
("description", options.get('--description') or "default description"),
|
||||||
|
@ -63,7 +63,6 @@ def upload_video(oauth, config, options):
|
||||||
("nsfw", str(options.get('--nsfw') or 0)),
|
("nsfw", str(options.get('--nsfw') or 0)),
|
||||||
("commentsEnabled", "1"),
|
("commentsEnabled", "1"),
|
||||||
("channelId", get_userinfo()),
|
("channelId", get_userinfo()),
|
||||||
# beware, see validateVideo for supported types
|
|
||||||
("videofile", get_videofile(path))
|
("videofile", get_videofile(path))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -72,7 +71,12 @@ def upload_video(oauth, config, options):
|
||||||
for strtags in tags:
|
for strtags in tags:
|
||||||
fields.append(("tags", strtags))
|
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)
|
multipart_data = MultipartEncoder(fields)
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
|
|
49
lib/utils.py
Normal file
49
lib/utils.py
Normal file
|
@ -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()]
|
|
@ -16,6 +16,7 @@ from googleapiclient.errors import HttpError
|
||||||
from googleapiclient.http import MediaFileUpload
|
from googleapiclient.http import MediaFileUpload
|
||||||
from google_auth_oauthlib.flow import InstalledAppFlow
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
||||||
|
|
||||||
|
import utils
|
||||||
|
|
||||||
# Explicitly tell the underlying HTTP transport library not to retry, since
|
# Explicitly tell the underlying HTTP transport library not to retry, since
|
||||||
# we are handling retry logic ourselves.
|
# we are handling retry logic ourselves.
|
||||||
|
@ -76,12 +77,17 @@ def initialize_upload(youtube, options):
|
||||||
if options.get('--tags'):
|
if options.get('--tags'):
|
||||||
tags = options.get('--tags').split(',')
|
tags = options.get('--tags').split(',')
|
||||||
|
|
||||||
|
category = None
|
||||||
|
if options.get('--category'):
|
||||||
|
category = utils.getCategory(options.get('--category'), 'youtube')
|
||||||
|
|
||||||
body = {
|
body = {
|
||||||
"snippet": {
|
"snippet": {
|
||||||
"title": options.get('--name') or splitext(basename(path))[0],
|
"title": options.get('--name') or splitext(basename(path))[0],
|
||||||
"description": options.get('--description') or "",
|
"description": options.get('--description') or "default description",
|
||||||
"tags": tags,
|
"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")}
|
"status": {"privacyStatus": str(options.get('--privacy') or "private")}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,14 +13,25 @@ Options:
|
||||||
--name=NAME Name of the video to upload. default to video file name
|
--name=NAME Name of the video to upload. default to video file name
|
||||||
-d, --description=STRING Description of the video.
|
-d, --description=STRING Description of the video.
|
||||||
-t, --tags=STRING Tags for the video. comma separated
|
-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.
|
-h --help Show this help.
|
||||||
--version Show version.
|
--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
|
from os.path import dirname, realpath
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from docopt import docopt
|
from docopt import docopt
|
||||||
|
|
||||||
|
|
||||||
# Allows you to a relative import from the parent folder
|
# Allows you to a relative import from the parent folder
|
||||||
sys.path.insert(0, dirname(realpath(__file__)) + "/lib")
|
sys.path.insert(0, dirname(realpath(__file__)) + "/lib")
|
||||||
|
|
||||||
|
@ -40,8 +51,15 @@ except ImportError:
|
||||||
' is installed, NOT the Python bindings to libmagic API \n'
|
' is installed, NOT the Python bindings to libmagic API \n'
|
||||||
'see https://github.com/ahupp/python-magic\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_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):
|
def validateVideo(path):
|
||||||
|
@ -51,6 +69,11 @@ def validateVideo(path):
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def validateCategory(category):
|
||||||
|
if category.lower() in VALID_CATEGORIES:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
if __name__ == '__main__':
|
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('--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('--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('--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,
|
'--help': bool,
|
||||||
'--version': bool
|
'--version': bool
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue