loading
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import urllib.request
import json
from bs4 import BeautifulSoup # pacman -S python-beautifulsoup4

with urllib.request.urlopen('https://forum.manjaro.org/c/announcements.json') as f_url:
    req = f_url.read()
#doc: https://docs.discourse.org/#tag/Categories    
topics = json.loads(req)['topic_list']['topics']
topics = [t for t in topics if (t['visible'] and not t['closed'] and not t['title'].startswith('About'))]

while True:
    max =0
    for i,topic in enumerate(topics):
        print(f"\33[32m{i+1}\33[0m", topic['title'])
        max = i+1

    i = input(f"\nvoir une annonce :(1..{max}) ")
    if not i.isdigit() or int(i)<1 or int(i)>max:
        exit(0)
    id = topics[int(i)-1]['id']

    # afficher le 1er post du sujet
    with urllib.request.urlopen(f"https://forum.manjaro.org/t/{id}.json") as f_url:
        req = f_url.read()    
    posts = json.loads(req)['post_stream']['posts']
    for post in posts:
        print("\n------\n\33[32m",topics[int(i)-1]['title'],"\33[0m\n------\n")
        soup = BeautifulSoup(post['cooked'], 'html.parser')
        print(soup.get_text())
        break
    print("\n")
    input()