Explorar el Código

Feed's 1st image (if any) is og:image

master
The Dod hace 1 año
padre
commit
0c54bfc736
Se han modificado 2 ficheros con 6 adiciones y 3 borrados
  1. 5
    2
      mymastotag.py
  2. 1
    1
      templates/index.html

+ 5
- 2
mymastotag.py Ver fichero

@@ -1,6 +1,6 @@
1 1
 import time
2 2
 import feedparser
3
-from flask import Flask, render_template, redirect
3
+from flask import Flask, render_template, redirect, url_for
4 4
 from bs4 import BeautifulSoup
5 5
 
6 6
 # Source: https://lingohub.com/academy/best-practices/rtl-language-list
@@ -17,6 +17,7 @@ def my_masto_tag(lang, instance, user, tag):
17 17
     lang = lang.lower()
18 18
     is_rtl = lang in RTL_LANGS
19 19
     title = f'@{user}@{instance} — #{tag}'
20
+    og_image = None
20 21
     feed = feedparser.parse(f'https://{instance}/@{user}/tagged/{tag}.rss')
21 22
     for e in feed['entries']:
22 23
         e['date'] = time.strftime('%Y-%m-%d', e['published_parsed'])
@@ -31,10 +32,12 @@ def my_masto_tag(lang, instance, user, tag):
31 32
             mtype = m['type'].split('/')[0]
32 33
             if mtype=='image':
33 34
                 images.append(m)
35
+                if not og_image:
36
+                    og_image = m.get('url')
34 37
             elif mtype=='video':
35 38
                 videos.append(m)
36 39
         e['images'] = images
37 40
         e['has_images'] = not not images
38 41
         e['videos'] = videos
39 42
         e['has_videos'] = not not videos
40
-    return render_template('index.html', lang=lang, is_rtl=is_rtl, title=title, feed=feed)
43
+    return render_template('index.html', lang=lang, is_rtl=is_rtl, title=title, og_image=og_image or url_for('static', filename='hashtag.png', _external=True), feed=feed)

+ 1
- 1
templates/index.html Ver fichero

@@ -6,7 +6,7 @@
6 6
     <meta name="viewport" content="width=device-width, initial-scale=1">
7 7
     <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
8 8
     <link rel="stylesheet" href="{{ url_for('static', filename='css') }}/bootstrap{% if is_rtl %}-rtl{% endif %}.min.css">
9
-    <meta property="og:image" content="{{ url_for('static', filename='hashtag.png', _external=True) }}">
9
+    <meta property="og:image" content="{{og_image|default(url_for('static', filename='hashtag.png', _external=True))}}">
10 10
   </head>
11 11
   <body>
12 12
     <div class="container">

Loading…
Cancelar
Guardar