Browse Source

Add new feature to combine channel and playlist on peertube 🎉

LecygneNoir 2 months ago
parent
commit
44875b3567
2 changed files with 67 additions and 7 deletions
  1. 58
    5
      lib/pt_upload.py
  2. 9
    2
      prismedia_upload.py

+ 58
- 5
lib/pt_upload.py View File

@@ -55,6 +55,50 @@ def get_default_channel(user_info):
55 55
     return user_info['videoChannels'][0]['id']
56 56
 
57 57
 
58
+def get_channel_by_name(user_info, options):
59
+    for channel in user_info["videoChannels"]:
60
+        if channel['displayName'].encode('utf8') == str(options.get('--channel')):
61
+            return channel['id']
62
+
63
+
64
+def create_channel(oauth, url, options):
65
+    template = ('Peertube: Channel %s does not exist, creating it.')
66
+    logging.info(template % (str(options.get('--channel'))))
67
+    channel_name = utils.cleanString(str(options.get('--channel')))
68
+    # Peertube allows 20 chars max for channel name
69
+    channel_name = channel_name[:19]
70
+    data = '{"name":"' + channel_name +'", \
71
+            "displayName":"' + str(options.get('--channel')) +'", \
72
+            "description":null}'
73
+
74
+    headers = {
75
+        'Content-Type': "application/json"
76
+    }
77
+    try:
78
+        response = oauth.post(url + "/api/v1/video-channels/",
79
+                              data=data,
80
+                              headers=headers)
81
+    except Exception as e:
82
+        if hasattr(e, 'message'):
83
+            logging.error("Error: " + str(e.message))
84
+        else:
85
+            logging.error("Error: " + str(e))
86
+    if response is not None:
87
+        if response.status_code == 200:
88
+            jresponse = response.json()
89
+            jresponse = jresponse['videoChannel']
90
+            return jresponse['id']
91
+        if response.status_code == 409:
92
+            logging.error('Peertube: Error: It seems there is a conflict with an existing channel, please beware '
93
+                          'Peertube internal name is compiled from 20 firsts characters of channel name.'
94
+                          ' Please check your channel name and retry.')
95
+            exit(1)
96
+        else:
97
+            logging.error(('Peertube: Creating channel failed with an unexpected response: '
98
+                           '%s') % response)
99
+            exit(1)
100
+
101
+
58 102
 def get_default_playlist(user_info):
59 103
     return user_info['videoChannels'][0]['id']
60 104
 
@@ -65,7 +109,7 @@ def get_playlist_by_name(user_playlists, options):
65 109
             return playlist['id']
66 110
 
67 111
 
68
-def create_playlist(oauth, url, options, default_channel):
112
+def create_playlist(oauth, url, options, channel):
69 113
     template = ('Peertube: Playlist %s does not exist, creating it.')
70 114
     logging.info(template % (str(options.get('--playlist'))))
71 115
     # We use files for form-data Content
@@ -74,7 +118,7 @@ def create_playlist(oauth, url, options, default_channel):
74 118
     files = {'displayName': (None, str(options.get('--playlist'))),
75 119
              'privacy': (None, "1"),
76 120
              'description': (None, "null"),
77
-             'videoChannelId': (None, str(default_channel)),
121
+             'videoChannelId': (None, str(channel)),
78 122
              'thumbnailfile': (None, "null")}
79 123
     try:
80 124
         response = oauth.post(url + "/api/v1/video-playlists/",
@@ -199,13 +243,22 @@ def upload_video(oauth, secret, options):
199 243
         fields.append(("thumbnailfile", get_file(options.get('--thumbnail'))))
200 244
         fields.append(("previewfile", get_file(options.get('--thumbnail'))))
201 245
 
202
-    default_channel = get_default_channel(user_info)
203
-    fields.append(("channelId", str(default_channel)))
246
+    if options.get('--channel'):
247
+        channel_id = get_channel_by_name(user_info, options)
248
+        if not channel_id and options.get('--channelCreate'):
249
+            channel_id = create_channel(oauth, url, options)
250
+        elif not channel_id:
251
+            logging.warning("Channel `" + options.get('--channel') + "` is unknown, using default channel.")
252
+            channel_id = get_default_channel(user_info)
253
+    else:
254
+        channel_id = get_default_channel(user_info)
255
+
256
+    fields.append(("channelId", str(channel_id)))
204 257
 
205 258
     if options.get('--playlist'):
206 259
         playlist_id = get_playlist_by_name(user_playlists, options)
207 260
         if not playlist_id and options.get('--playlistCreate'):
208
-            playlist_id = create_playlist(oauth, url, options, default_channel)
261
+            playlist_id = create_playlist(oauth, url, options, channel_id)
209 262
         elif not playlist_id:
210 263
             logging.warning("Playlist `" + options.get('--playlist') + "` does not exist, please set --playlistCreate"
211 264
                             " if you want to create it")

+ 9
- 2
prismedia_upload.py View File

@@ -24,7 +24,8 @@ Options:
24 24
   --disable-comments  Disable comments (Peertube only as YT API does not support) (default: comments are enabled)
25 25
   --nsfw  Set the video as No Safe For Work (Peertube only as YT API does not support) (default: video is safe)
26 26
   --nfo=STRING  Configure a specific nfo file to set options for the video.
27
-                By default Prismedia search a .txt based on video name
27
+                By default Prismedia search a .txt based on the video name and will
28
+                decode the file as UTF-8 (so make sure your nfo file is UTF-8 encoded)
28 29
                 See nfo_example.txt for more details
29 30
   --platform=STRING  List of platform(s) to upload to, comma separated.
30 31
                      Supported platforms are youtube and peertube (default is both)
@@ -36,8 +37,12 @@ Options:
36 37
   --thumbnail=STRING    Path to a file to use as a thumbnail for the video.
37 38
                         Supported types are jpg and jpeg.
38 39
                         By default, prismedia search for an image based on video name followed by .jpg or .jpeg
40
+  --channel=STRING Set the channel to use for the video (Peertube only)
41
+                    If the channel is not found, spawn an error except if --channelCreate is set.
42
+  --channelCreate  Create the channel if not exists. (Peertube only, default do not create)
43
+                   Only relevant if --channel is set.
39 44
   --playlist=STRING Set the playlist to use for the video. Also known as Channel for Peertube.
40
-                    If the playlist is not found, spawn an error except if --playlist-create is set.
45
+                    If the playlist is not found, spawn an error except if --playlistCreate is set.
41 46
   --playlistCreate  Create the playlist if not exists. (default do not create)
42 47
                     Only relevant if --playlist is set.
43 48
   -h --help  Show this help.
@@ -207,6 +212,8 @@ if __name__ == '__main__':
207 212
         Optional('--thumbnail'): Or(None, And(
208 213
                                     str, validateThumbnail, error='thumbnail is not supported, please use jpg/jpeg'),
209 214
                                     ),
215
+        Optional('--channel'): Or(None, str),
216
+        Optional('--channelCreate'): bool,
210 217
         Optional('--playlist'): Or(None, str),
211 218
         Optional('--playlistCreate'): bool,
212 219
         '--help': bool,

Loading…
Cancel
Save