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.depth: int = section.profondeur
self.agenda: int = section.agenda self.agenda: int = section.agenda
def get_articles(self, limit: int):
return Articles(limit)
class Articles: class Articles:
exported: int = 0 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 # Query the DB to retrieve all articles sorted by publication date
self.articles = ( 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) self.toExport: int = len(self.articles)
@ -163,3 +166,33 @@ class Articles:
{"exported": self.exported, "remaining": self.remaining()}, {"exported": self.exported, "remaining": self.remaining()},
article, 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 os import makedirs, mkdir
from shutil import rmtree from shutil import rmtree
from articles import Article, Articles
from config import config from config import config
from converter import highlight_unknown_chars from converter import highlight_unknown_chars
from database import db from database import db
from lib import Article, Sections
# Define terminal escape sequences to stylize output # Define terminal escape sequences to stylize output
R: str = "\033[91m" R: str = "\033[91m"
@ -35,25 +35,26 @@ if __name__ == "__main__":
unknown_chars_articles: list[Article] = [] unknown_chars_articles: list[Article] = []
# Loop among first maxexport articles & export them # Loop among first maxexport articles & export them
for counter, article in Articles(maxexport): for counter, section in Sections():
if (counter["exported"] - 1) % 100 == 0: for counter, article in section.get_articles(maxexport):
if (counter["exported"] - 1) % 100 == 0:
print(
f"\n{BOLD}Exporting {R}{counter['remaining']+1}{RESET}"
+ f"{BOLD} SPIP articles to Markdown & YAML files{RESET}\n"
)
empty: str = "EMPTY " if len(article.text) < 1 else ""
print( print(
f"\n{BOLD}Exporting {R}{counter['remaining']+1}{RESET}" f"{BOLD}{counter['exported']}. {empty}{RESET}"
+ f"{BOLD} SPIP articles to Markdown & YAML files{RESET}\n" + highlight_unknown_chars(article.title, R, RESET)
) )
empty: str = "EMPTY " if len(article.text) < 1 else "" fullpath: str = config.output_dir + "/" + article.get_path()
print( print(f"{BOLD}>{RESET} {fullpath}{article.get_filename()}")
f"{BOLD}{counter['exported']}. {empty}{RESET}" makedirs(fullpath, exist_ok=True)
+ highlight_unknown_chars(article.title, R, RESET) with open(fullpath + article.get_filename(), "w") as f:
) f.write(article.get_article())
fullpath: str = config.output_dir + "/" + article.get_path() # Store detected unknown characters
print(f"{BOLD}>{RESET} {fullpath}{article.get_filename()}") if len(article.get_unknown_chars()) > 0:
makedirs(fullpath, exist_ok=True) unknown_chars_articles.append(article)
with open(fullpath + article.get_filename(), "w") as f:
f.write(article.get_article())
# Store detected unknown characters
if len(article.get_unknown_chars()) > 0:
unknown_chars_articles.append(article)
for article in unknown_chars_articles: for article in unknown_chars_articles:
unknown_chars_apparitions: list[str] = article.get_unknown_chars() unknown_chars_apparitions: list[str] = article.get_unknown_chars()