diff --git a/README.md b/README.md index 51ddc69..f740bfd 100644 --- a/README.md +++ b/README.md @@ -56,16 +56,24 @@ Simply upload a video: ./prismedia_upload.py --file="yourvideo.mp4" ``` + Specify description and tags: ``` ./prismedia_upload.py --file="yourvideo.mp4" -d "My supa description" -t "tag1,tag2,foo" ``` + +Use a NFO file to specify your video options: + +``` +./prismedia_upload.py --file="yourvideo.mp4" --nfo /path/to/your/nfo.txt +``` + + Use --help to get all available options: ``` -./prismedia_upload.py --help prismedia_upload - tool to upload videos to Peertube and Youtube Usage: @@ -74,14 +82,17 @@ Usage: prismedia_upload.py --version Options: - --name=NAME Name of the video to upload. [default: video filename] - -d, --description=STRING Description of the video. [default: default description] + --name=NAME Name of the video to upload. (default to video filename) + -d, --description=STRING Description of the video. (default: default description) -t, --tags=STRING Tags for the video. comma separated - -c, --category=STRING Category for the videos, see below. [default: Films] + -c, --category=STRING Category for the videos, see below. (default: Films) --cca License should be CreativeCommon Attribution (affects Youtube upload only) - -p, --privacy=STRING Choose between public, unlisted or private. [default: private] - --disable-comments Disable comments (Peertube only) [default: comments are enabled] - --nsfw Set the video as No Safe For Work (Peertube only as YT API does not support) [default: video is safe] + -p, --privacy=STRING Choose between public, unlisted or private. (default: private) + --disable-comments Disable comments (Peertube only as YT API does not support) (default: comments are enabled) + --nsfw Set the video as No Safe For Work (Peertube only as YT API does not support) (default: video is safe) + --nfo=STRING Configure a specific nfo file to set options for the video. + By default Prismedia search a .txt based on video name + See nfo_example.txt for more details -h --help Show this help. --version Show version. diff --git a/lib/pt_upload.py b/lib/pt_upload.py index 96088d1..f5f067f 100644 --- a/lib/pt_upload.py +++ b/lib/pt_upload.py @@ -21,25 +21,25 @@ PEERTUBE_PRIVACY = { } -def get_authenticated_service(config): - peertube_url = str(config.get('peertube', 'peertube_url')) +def get_authenticated_service(secret): + peertube_url = str(secret.get('peertube', 'peertube_url')) oauth_client = LegacyApplicationClient( - client_id=str(config.get('peertube', 'client_id')) + client_id=str(secret.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')) + username=str(secret.get('peertube', 'username').lower()), + password=str(secret.get('peertube', 'password')), + client_id=str(secret.get('peertube', 'client_id')), + client_secret=str(secret.get('peertube', 'client_secret')) ) return oauth -def upload_video(oauth, config, options): +def upload_video(oauth, secret, options): def get_userinfo(): user_info = json.loads(oauth.get(url + "/api/v1/users/me").content) @@ -51,7 +51,7 @@ def upload_video(oauth, config, options): mimetypes.types_map[splitext(path)[1]]) path = options.get('--file') - url = config.get('peertube', 'peertube_url') + url = secret.get('peertube', 'peertube_url') # We need to transform fields into tuple to deal with tags as # MultipartEncoder does not support list refer @@ -59,10 +59,8 @@ 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/licences ("licence", "1"), - ("description", options.get('--description') or "default description"), - # look at the list numbers at /videos/privacies + ("description", options.get('--description') or "default description"), ("nsfw", str(int(options.get('--nsfw')) or "0")), ("channelId", get_userinfo()), ("videofile", get_videofile(path)) @@ -107,14 +105,17 @@ def upload_video(oauth, config, options): def run(options): - config = RawConfigParser() - config.read(PEERTUBE_SECRETS_FILE) - insecure_transport = config.get('peertube', 'OAUTHLIB_INSECURE_TRANSPORT') + secret = RawConfigParser() + try: + secret.read(PEERTUBE_SECRETS_FILE) + except Exception as e: + exit("Error loading " + str(PEERTUBE_SECRETS_FILE) + ": " + str(e)) + insecure_transport = secret.get('peertube', 'OAUTHLIB_INSECURE_TRANSPORT') os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = insecure_transport - oauth = get_authenticated_service(config) + oauth = get_authenticated_service(secret) try: print('Peertube : Uploading file...') - upload_video(oauth, config, options) + upload_video(oauth, secret, options) except Exception as e: if hasattr(e, 'message'): print("Error: " + e.message) diff --git a/lib/utils.py b/lib/utils.py index df3fe3e..0062b22 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -1,8 +1,10 @@ #!/usr/bin/python # coding: utf-8 +from ConfigParser import RawConfigParser, NoOptionError, NoSectionError +from os.path import dirname, splitext, basename, isfile -### FOR CATEGORIE ### +### CATEGORIES ### YOUTUBE_CATEGORY = { "music": 10, "films": 1, @@ -43,6 +45,7 @@ PEERTUBE_CATEGORY = { "animals": 16 } + ###################### @@ -50,4 +53,66 @@ def getCategory(category, type): if type == "youtube": return YOUTUBE_CATEGORY[category.lower()] else: - return PEERTUBE_CATEGORY[category.lower()] \ No newline at end of file + return PEERTUBE_CATEGORY[category.lower()] + + +# return the nfo as a RawConfigParser object +def loadNFO(options): + video_directory = dirname(options.get('--file')) +"/" + if options.get('--nfo'): + try: + print "Using " + options.get('--nfo') + " as NFO, loading..." + if isfile(options.get('--nfo')): + nfo = RawConfigParser() + nfo.read(options.get('--nfo')) + return nfo + else: + exit("Given NFO file does not exist, please check your path.") + except Exception as e: + exit("Problem with NFO file: " + str(e)) + else: + if options.get('--name'): + nfo_file = video_directory + options.get('--name') + ".txt" + print nfo_file + if isfile(nfo_file): + try: + print "Using " + nfo_file + " as NFO, loading..." + nfo = RawConfigParser() + nfo.read(nfo_file) + return nfo + except Exception as e: + exit("Problem with NFO file: " + str(e)) + + # if --nfo and --name does not exist, use --file as default + video_file = splitext(basename(options.get('--file')))[0] + nfo_file = video_directory + video_file + ".txt" + if isfile(nfo_file): + try: + print "Using " + nfo_file + " as NFO, loading..." + nfo = RawConfigParser() + nfo.read(nfo_file) + return nfo + except Exception as e: + exit("Problem with nfo file: " + str(e)) + print "No suitable NFO found, skipping." + return False + + +def parseNFO(options): + nfo = loadNFO(options) + if nfo: + # We need to check all options and replace it with the nfo value if not defined (None or False) + for key, value in options.iteritems(): + key = key.replace("-", "") + try: + # get string options + if value is None and nfo.get('video', key): + options['--' + key] = nfo.get('video', key) + # get boolean options + elif value is False and nfo.getboolean('video', key): + options['--' + key] = nfo.getboolean('video', key) + except NoOptionError: + continue + except NoSectionError: + exit("Given NFO file miss section [video], please check syntax of your NFO.") + return options diff --git a/lib/yt_upload.py b/lib/yt_upload.py index d53a47c..495c17b 100644 --- a/lib/yt_upload.py +++ b/lib/yt_upload.py @@ -88,7 +88,7 @@ def initialize_upload(youtube, options): body = { "snippet": { "title": options.get('--name') or splitext(basename(path))[0], - "description": options.get('--description'), + "description": options.get('--description') or "default description", "tags": tags, # if no category, set default to 1 (Films) "categoryId": str(category or 1), diff --git a/prismedia_upload.py b/prismedia_upload.py index 0316cea..2c47312 100755 --- a/prismedia_upload.py +++ b/prismedia_upload.py @@ -11,13 +11,16 @@ Usage: Options: --name=NAME Name of the video to upload. (default to video filename) - -d, --description=STRING Description of the video. [default: default description] + -d, --description=STRING Description of the video. (default: default description) -t, --tags=STRING Tags for the video. comma separated - -c, --category=STRING Category for the videos, see below. [default: Films] + -c, --category=STRING Category for the videos, see below. (default: Films) --cca License should be CreativeCommon Attribution (affects Youtube upload only) - -p, --privacy=STRING Choose between public, unlisted or private. [default: private] - --disable-comments Disable comments (Peertube only as YT API does not support) [default: comments are enabled] - --nsfw Set the video as No Safe For Work (Peertube only as YT API does not support) [default: video is safe] + -p, --privacy=STRING Choose between public, unlisted or private. (default: private) + --disable-comments Disable comments (Peertube only as YT API does not support) (default: comments are enabled) + --nsfw Set the video as No Safe For Work (Peertube only as YT API does not support) (default: video is safe) + --nfo=STRING Configure a specific nfo file to set options for the video. + By default Prismedia search a .txt based on video name + See nfo_example.txt for more details -h --help Show this help. --version Show version. @@ -41,6 +44,7 @@ sys.path.insert(0, dirname(realpath(__file__)) + "/lib") import yt_upload import pt_upload +import utils try: from schema import Schema, And, Or, Optional, SchemaError @@ -55,7 +59,7 @@ except ImportError: ' is installed, NOT the Python bindings to libmagic API \n' 'see https://github.com/ahupp/python-magic\n') -VERSION = "prismedia 0.2-alpha" +VERSION = "prismedia 0.3" VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted') VALID_CATEGORIES = ( "music", "films", "vehicles", @@ -119,6 +123,7 @@ if __name__ == '__main__': validatePrivacy, error="Please use recognized privacy between public, unlisted or private") ), + Optional('--nfo'): Or(None, str), Optional('--cca'): bool, Optional('--disable-comments'): bool, Optional('--nsfw'): bool, @@ -131,5 +136,7 @@ if __name__ == '__main__': except SchemaError as e: exit(e) + options = utils.parseNFO(options) + yt_upload.run(options) pt_upload.run(options)