start big refactoring to iterate over sections, then section’s articles

This commit is contained in:
Guilhem Fauré 2023-05-17 12:17:54 +02:00
parent 8021bd395e
commit d15ad5fd8e
2 changed files with 54 additions and 20 deletions

View File

@ -137,14 +137,17 @@ class Section:
self.depth: int = section.profondeur
self.agenda: int = section.agenda
def get_articles(self, limit: int):
return Articles(limit)
class Articles:
exported: int = 0
def __init__(self, maxexport: int) -> None:
def __init__(self, limit: int) -> None:
# Query the DB to retrieve all articles sorted by publication date
self.articles = (
SpipArticles.select().order_by(SpipArticles.date.desc()).limit(maxexport)
SpipArticles.select().order_by(SpipArticles.date.desc()).limit(limit)
)
self.toExport: int = len(self.articles)
@ -163,3 +166,33 @@ class Articles:
{"exported": self.exported, "remaining": self.remaining()},
article,
)
class Sections:
exported: int = 0
def __init__(self, limit: int = 0) -> None:
# Query the DB to retrieve all articles sorted by publication date
if limit > 0:
self.articles = (
SpipArticles.select().order_by(SpipArticles.date.desc()).limit(limit)
)
else:
self.articles = SpipArticles.select().order_by(SpipArticles.date.desc())
self.toExport: int = len(self.articles)
def remaining(self):
return self.toExport - self.exported
def __iter__(self):
return self
def __next__(self):
if self.remaining() <= 0:
raise StopIteration
self.exported += 1
section = Section(self.articles[self.exported - 1])
return (
{"exported": self.exported, "remaining": self.remaining()},
section,
)

View File

@ -4,10 +4,10 @@ import sys
from os import makedirs, mkdir
from shutil import rmtree
from articles import Article, Articles
from config import config
from converter import highlight_unknown_chars
from database import db
from lib import Article, Sections
# Define terminal escape sequences to stylize output
R: str = "\033[91m"
@ -35,7 +35,8 @@ if __name__ == "__main__":
unknown_chars_articles: list[Article] = []
# Loop among first maxexport articles & export them
for counter, article in Articles(maxexport):
for counter, section in Sections():
for counter, article in section.get_articles(maxexport):
if (counter["exported"] - 1) % 100 == 0:
print(
f"\n{BOLD}Exporting {R}{counter['remaining']+1}{RESET}"