Page 1 sur 1

annonces du forum inter en console

Publié : 01 mai 2019, 22:28
par papajoke
:bjr:
petit script python qui permet de lister les annonces et éventuellement lire le premier message.

Utilise l'api de discourse qui permet un export au format json
utilise le paquet python-beautifulsoup4 qui permet de transformer de l'html en texte

le fichier manja-annonces

#!/usr/bin/python
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()
la sortie:

1 [Testing] Manjaro Gnome 18.1.0 Juhraya pre2
2 [Unstable Update] 2019-04-30 - Systemd, Lightdm, Falkon
3 [Testing Update] 2019-04-29 - mostly kernels
4 [Stable Update] 2019-04-28 - Gcc, Systemd, Kernels, Virtualbox, Qt5, Deepin
5 [Stable Update i686] 2019-04-28 - MESA 19.0.3, GCC 8.3.0, OpenSSH, Kernels, systemd 242.16
...
voir une annonce :(1..17) 
Saisir un numéro invalide pour sortir :wink:

Le tester online : https://interannonces.papajoker1.repl.run

Re: annonces du forum inter en console

Publié : 02 mai 2019, 00:45
par Cyberdr3am
Vraiment sympas ce petit script.

merci