11from django .template .defaultfilters import slugify
22from django .urls import reverse
33from markdownify import markdownify
4+ from django .utils .translation import get_language
45
56from pod .activitypub .constants import AP_LICENSE_MAPPING
67from pod .activitypub .models import ExternalVideo
78from pod .activitypub .utils import ap_url , make_magnet_url , stable_uuid
9+ from pod .video .models import LANG_CHOICES
810
11+ import logging
12+ logger = logging .getLogger (__name__ )
913
10- def ap_video_to_external_video (payload ):
14+
15+ def ap_video_to_external_video (payload , source_instance ):
1116 """Create an ExternalVideo object from an AP Video payload."""
12- return ExternalVideo .objects .create ()
17+
18+ video_source_links = [link for link in payload ["url" ] if "mediaType" in link and link ["mediaType" ] == "video/mp4" ]
19+ if not video_source_links :
20+ tags = []
21+ for link in payload ["url" ]:
22+ if "tag" in link :
23+ tags .extend (link ["tag" ])
24+ video_source_links = [link for link in tags if "mediaType" in link and link ["mediaType" ] == "video/mp4" ]
25+
26+ external_video_attributes = {
27+ "ap_id" : payload ["id" ],
28+ "video" : video_source_links [0 ]["href" ],
29+ "title" : payload ["name" ],
30+ "date_added" : payload ["published" ],
31+ "thumbnail" : [icon for icon in payload ["icon" ] if "thumbnails" in icon ["url" ]][0 ]["url" ],
32+ "duration" : int (payload ["duration" ].lstrip ("PT" ).rstrip ("S" )),
33+ "viewcount" : payload ["views" ],
34+ "source_instance" : source_instance ,
35+ }
36+
37+ if (
38+ "language" in payload
39+ and "identifier" in payload ["language" ]
40+ and (identifier := payload ["language" ]["identifier" ])
41+ and identifier in LANG_CHOICES
42+ ):
43+ external_video_attributes ["main_lang" ] = identifier
44+
45+ if "content" in payload and (content := payload ["content" ]):
46+ external_video_attributes ["description" ] = content
47+
48+ external_video , created = ExternalVideo .objects .update_or_create (
49+ ap_id = external_video_attributes ["ap_id" ],
50+ defaults = external_video_attributes ,
51+ )
52+
53+ if created :
54+ logger .info ("ActivityPub external video %s created from %s instance" , external_video , source_instance )
55+ else :
56+ logger .info ("ActivityPub external video %s updated from %s instance" , external_video , source_instance )
57+
58+ return external_video
1359
1460
1561def video_to_ap_video (video ):
@@ -125,8 +171,16 @@ def video_urls(video):
125171 magnets may become fully optional someday
126172 https://framacolibri.org/t/comments-and-suggestions-on-the-peertube-activitypub-implementation/21215/2
127173 """
128-
129- return {
174+ truc = video .get_video_mp4 ()
175+ # print(video)
176+ # print(dir(truc[0]))
177+ # print(truc[0].id)
178+ # print(truc[0].pk)
179+ # print(truc[0].source_file.url)
180+ # print(truc[0].source_file.file)
181+ # print(type(truc[0].source_file))
182+ # print(dir(truc[0].source_file))
183+ machin = {
130184 "url" : (
131185 [
132186 # Webpage
@@ -141,7 +195,11 @@ def video_urls(video):
141195 {
142196 "type" : "Link" ,
143197 "mediaType" : mp4 .encoding_format ,
144- "href" : ap_url (mp4 .source_file .url ),
198+ # "href": ap_url(mp4.source_file.url),
199+ "href" : ap_url (reverse (
200+ "video:video_mp4" ,
201+ kwargs = {"id" : video .id , "mp4_id" : mp4 .id },
202+ )),
145203 "height" : mp4 .height ,
146204 "width" : mp4 .width ,
147205 "size" : mp4 .source_file .size ,
@@ -165,6 +223,8 @@ def video_urls(video):
165223 ]
166224 )
167225 }
226+ print (machin )
227+ return machin
168228
169229
170230def video_attributions (video ):
@@ -370,16 +430,16 @@ def video_icon(video):
370430 # only image/jpeg is supported on peertube
371431 # https://github.com/Chocobozzz/PeerTube/blob/b824480af7054a5a49ddb1788c26c769c89ccc8a/server/core/helpers/custom-validators/activitypub/videos.ts#L192
372432 """
373- if not video .thumbnail :
374- return {}
433+ # if not video.thumbnail:
434+ # return {}
375435
376436 return {
377437 "icon" : [
378438 {
379439 "type" : "Image" ,
380- "url" : video .get_thumbnail_url (scheme = True ),
381- "width" : video .thumbnail .file .width ,
382- "height" : video .thumbnail .file .height ,
440+ "url" : video .get_thumbnail_url (scheme = True , is_activity_pub = True ),
441+ "width" : video .thumbnail .file .width if video . thumbnail else 640 ,
442+ "height" : video .thumbnail .file .height if video . thumbnail else 360 ,
383443 # TODO: use the real media type when peertub supports JPEG
384444 # "mediaType": video.thumbnail.file_type,
385445 "mediaType" : "image/jpeg" ,
0 commit comments