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.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,
|
||||||
|
)
|
@ -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,7 +35,8 @@ 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():
|
||||||
|
for counter, article in section.get_articles(maxexport):
|
||||||
if (counter["exported"] - 1) % 100 == 0:
|
if (counter["exported"] - 1) % 100 == 0:
|
||||||
print(
|
print(
|
||||||
f"\n{BOLD}Exporting {R}{counter['remaining']+1}{RESET}"
|
f"\n{BOLD}Exporting {R}{counter['remaining']+1}{RESET}"
|
||||||
|
Loading…
Reference in New Issue
Block a user