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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| #!/usr/bin/env python
import sys
from urllib.request import Request, urlopen
from pathlib import Path
from dataclasses import dataclass
from bs4 import BeautifulSoup
version = '2023-03-13'
ME = "monlogin" # mettre son speudo en évidence à l'affichage
FORUMS = {
'linux': 17,
'python': 96,
#'divers': 'x',
}
FICHIER = Path(__file__).parent / "developpez.cache.html"
arg_help = [a for a in sys.argv if a.startswith('-h') or a.startswith('--h')]
if arg_help:
print(f" --{'cache':12} : pour dev ce script")
for forum, numero in FORUMS.items():
print(f" --{forum:12} : forum {forum:12} (défaut: python)")
print("\nNote: pour passer d'un forum à un autre, il faut attendre 10 secondes 😢")
print("Fichier cache:", FICHIER)
exit(0)
def get_url(jours=2) -> str:
id_forum = 0
for forum, numero in FORUMS.items():
if f"--{forum}" in sys.argv[1:]:
id_forum = numero
break
if not id_forum:
id_forum = FORUMS["python"]
return f"http://www.developpez.net/forums/search.php?do=getdaily&days={jours}&f={id_forum}"
def _hi(txt: str, color: int = 34, use: bool = True) -> str:
""" un arc-en-ciel dans le terminal """
if not use:
return txt
if txt == ME:
color = 43
return f"\33[{color}m{txt}\33[0m"
def http_get(url) -> str:
""" on télécharge et on ne se cache pas avec une fausse identité """
accept_language = 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7'
user = "developpez user (Linux)"
with urlopen(Request(url, headers={
'User-Agent': user,
'Accept-Language': accept_language,
})) as ret:
return ret.read().decode('ISO-8859-1')
return ''
@dataclass
class Sujet():
""" datas récupérées sur la page web """
title: str
auteur: str
creation: str
count: int
helpeur: str
modif: str
url: str
closed: bool
# on utilise le cache en phase de developpement !!!
if "--cache" not in sys.argv or not FICHIER.exists():
url = get_url(2)
html = http_get(url)
print("##", url, "...")
if not html:
exit(2)
FICHIER.write_text(html)
print(f"## Fichier sauvegardé dans:", FICHIER)
else:
print("## Utilisation du cache", FICHIER)
print()
html = FICHIER.read_text()
soup = BeautifulSoup(html, "html.parser")
items = []
for li in reversed(soup.select('ol.searchbits li.imodselector')):
creation = li.select_one('div.threadmeta div.author span').get_text().split(',')[-1].strip()
try:
nb = int(li.select_one('ul.threadstats li a').get_text().replace('*',''))
except ValueError:
# pas des espaces utilisées mais `*` = '1\xa0326'
nb = -1
last_msg = li.select_one('dl.threadlastpost span.time').get_text()
url = li.select_one('dl.threadlastpost dd a[href*="#post"]').get("href", '')
closed = bool(li.select_one('h3 img[src*="resolu"]'))
items.append(Sujet(
title=li.select_one('h3 a').get_text(),
auteur=li.select_one('div.threadmeta div.author a').get_text(),
creation=creation,
count=nb,
helpeur=li.select_one('dl.threadlastpost dd a').get_text(),
modif=last_msg,
url=url,
closed=closed,
))
# Affichage du résultat
item: Sujet
for item in items:
puce = "ⵔ" if item.closed else "⏺"
print(f"{puce} {_hi(item.title)} [{item.count}]\t({item.creation})\t par: {_hi(item.auteur)}")
print(f" {_hi(item.helpeur, 33)} à : {item.modif}")
# url cachée mais cliquable
print(f"{_hi(item.url,30)}")
#print() |
Partager