Pārlūkot izejas kodu

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

master
The Dod 1 gadu atpakaļ
vecāks
revīzija
0c54bfc736
2 mainītis faili ar 6 papildinājumiem un 3 dzēšanām
  1. 5
    2
      mymastotag.py
  2. 1
    1
      templates/index.html

+ 5
- 2
mymastotag.py Parādīt failu

1
 import time
1
 import time
2
 import feedparser
2
 import feedparser
3
-from flask import Flask, render_template, redirect
3
+from flask import Flask, render_template, redirect, url_for
4
 from bs4 import BeautifulSoup
4
 from bs4 import BeautifulSoup
5
 
5
 
6
 # Source: https://lingohub.com/academy/best-practices/rtl-language-list
6
 # Source: https://lingohub.com/academy/best-practices/rtl-language-list
17
     lang = lang.lower()
17
     lang = lang.lower()
18
     is_rtl = lang in RTL_LANGS
18
     is_rtl = lang in RTL_LANGS
19
     title = f'@{user}@{instance} — #{tag}'
19
     title = f'@{user}@{instance} — #{tag}'
20
+    og_image = None
20
     feed = feedparser.parse(f'https://{instance}/@{user}/tagged/{tag}.rss')
21
     feed = feedparser.parse(f'https://{instance}/@{user}/tagged/{tag}.rss')
21
     for e in feed['entries']:
22
     for e in feed['entries']:
22
         e['date'] = time.strftime('%Y-%m-%d', e['published_parsed'])
23
         e['date'] = time.strftime('%Y-%m-%d', e['published_parsed'])
31
             mtype = m['type'].split('/')[0]
32
             mtype = m['type'].split('/')[0]
32
             if mtype=='image':
33
             if mtype=='image':
33
                 images.append(m)
34
                 images.append(m)
35
+                if not og_image:
36
+                    og_image = m.get('url')
34
             elif mtype=='video':
37
             elif mtype=='video':
35
                 videos.append(m)
38
                 videos.append(m)
36
         e['images'] = images
39
         e['images'] = images
37
         e['has_images'] = not not images
40
         e['has_images'] = not not images
38
         e['videos'] = videos
41
         e['videos'] = videos
39
         e['has_videos'] = not not videos
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 Parādīt failu

6
     <meta name="viewport" content="width=device-width, initial-scale=1">
6
     <meta name="viewport" content="width=device-width, initial-scale=1">
7
     <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
7
     <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
8
     <link rel="stylesheet" href="{{ url_for('static', filename='css') }}/bootstrap{% if is_rtl %}-rtl{% endif %}.min.css">
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
   </head>
10
   </head>
11
   <body>
11
   <body>
12
     <div class="container">
12
     <div class="container">

Notiek ielāde…
Atcelt
Saglabāt