Using youtube thumbnails as image url in stories.

This commit is contained in:
Samuel Clay 2022-02-22 15:50:21 -05:00
parent 15b030a1f9
commit f403fb624e
2 changed files with 37 additions and 6 deletions

View file

@ -2496,6 +2496,15 @@ class MStory(mongo.Document):
def decoded_story_title(self):
return html.unescape(self.story_title)
@property
def story_content_str(self):
story_content = self.story_content
if not story_content and self.story_content_z:
story_content = smart_str(zlib.decompress(self.story_content_z))
return story_content
def save(self, *args, **kwargs):
story_title_max = MStory._fields['story_title'].max_length
story_content_type_max = MStory._fields['story_content_type'].max_length
@ -2794,12 +2803,10 @@ class MStory(mongo.Document):
story_content = None
if not text:
story_content = self.story_content
if not story_content and self.story_content_z:
story_content = zlib.decompress(self.story_content_z)
story_content = self.story_content_str
elif text:
if self.original_text_z:
story_content = zlib.decompress(self.original_text_z)
story_content = smart_str(zlib.decompress(self.original_text_z))
if not story_content:
return
@ -2815,6 +2822,28 @@ class MStory(mongo.Document):
return
images = soup.findAll('img')
# Add youtube thumbnail and insert appropriately before/after images.
# Give the Youtube a bit of an edge.
video_thumbnails = soup.findAll('iframe', src=lambda x: x and 'youtube.com' in x)
for video_thumbnail in video_thumbnails:
video_src = video_thumbnail.get('src')
video_id = re.search('.*?youtube.com/embed/(.*)(\?.*)?$', video_src)
if video_id:
video_img_url = f"https://img.youtube.com/vi/{video_id}/0.jpg"
else:
logging.debug(f" ***> Couldn't find youtube url in {video_thumbnail}: {video_src}")
continue
iframe_index = story_content.index('<iframe')
try:
img_index = story_content.index('<img')*3
except ValueError:
img_index = None
if not img_index or iframe_index < img_index:
images.insert(0, video_img_url)
else:
images.append(video_img_url)
if not images:
if not text:
return self.extract_image_urls(force=force, text=True)
@ -2826,7 +2855,10 @@ class MStory(mongo.Document):
image_urls = []
for image in images:
image_url = image.get('src')
if isinstance(image, str):
image_url = image
else:
image_url = image.get('src')
if not image_url:
continue
if image_url and len(image_url) >= 1024:

View file

@ -62,7 +62,6 @@ NEWSBLUR.Models.Story = Backbone.Model.extend({
if (this.get('image_urls').length >= index+1) {
var url = this.get('image_urls')[index];
if (window.location.protocol == 'https:' &&
NEWSBLUR.Globals.is_staff &&
_.str.startsWith(url, "http://")) {
var secure_url = this.get('secure_image_urls')[url];
if (secure_url) url = secure_url;