start big refactoring to iterate over sections, then section’s articles
This commit is contained in:
parent
8021bd395e
commit
d15ad5fd8e
@ -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,
|
||||
)
|
@ -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,25 +35,26 @@ if __name__ == "__main__":
|
||||
unknown_chars_articles: list[Article] = []
|
||||
|
||||
# Loop among first maxexport articles & export them
|
||||
for counter, article in Articles(maxexport):
|
||||
if (counter["exported"] - 1) % 100 == 0:
|
||||
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}"
|
||||
+ f"{BOLD} SPIP articles to Markdown & YAML files{RESET}\n"
|
||||
)
|
||||
empty: str = "EMPTY " if len(article.text) < 1 else ""
|
||||
print(
|
||||
f"\n{BOLD}Exporting {R}{counter['remaining']+1}{RESET}"
|
||||
+ f"{BOLD} SPIP articles to Markdown & YAML files{RESET}\n"
|
||||
f"{BOLD}{counter['exported']}. {empty}{RESET}"
|
||||
+ highlight_unknown_chars(article.title, R, RESET)
|
||||
)
|
||||
empty: str = "EMPTY " if len(article.text) < 1 else ""
|
||||
print(
|
||||
f"{BOLD}{counter['exported']}. {empty}{RESET}"
|
||||
+ highlight_unknown_chars(article.title, R, RESET)
|
||||
)
|
||||
fullpath: str = config.output_dir + "/" + article.get_path()
|
||||
print(f"{BOLD}>{RESET} {fullpath}{article.get_filename()}")
|
||||
makedirs(fullpath, exist_ok=True)
|
||||
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)
|
||||
fullpath: str = config.output_dir + "/" + article.get_path()
|
||||
print(f"{BOLD}>{RESET} {fullpath}{article.get_filename()}")
|
||||
makedirs(fullpath, exist_ok=True)
|
||||
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:
|
||||
unknown_chars_apparitions: list[str] = article.get_unknown_chars()
|
||||
|
Loading…
Reference in New Issue
Block a user