diff --git a/markdown_utils/MarkdownUtils.py b/markdown_utils/MarkdownUtils.py new file mode 100644 index 0000000..110ff0d --- /dev/null +++ b/markdown_utils/MarkdownUtils.py @@ -0,0 +1,35 @@ + +def generate_markdown_link(text, url): + return "[{}]({})".format(text, url) + + +class MarkdownTable: + + headers = [] + rows = [] + + def __init__(self, headers): + self.headers = headers + + def add_row(self, row_data): + if len(row_data) != len(self.headers): + raise Exception("Wrong number of row entries.") + else: + self.rows.append(row_data) + + def __str__(self): + table = "| " + + for header in self.headers: + table += "{} | ".format(header) + + table += "\n" + + table += "|" + table += " - |" * len(self.headers) + + for row in self.rows: + table += "\n" + table += " | ".join(row) + + return table diff --git a/markdown_utils/__init__.py b/markdown_utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/vcinema_utils/VCinemaUtils.py b/vcinema_utils/VCinemaUtils.py index 1e80148..9d0b7de 100644 --- a/vcinema_utils/VCinemaUtils.py +++ b/vcinema_utils/VCinemaUtils.py @@ -2,8 +2,9 @@ from collections import Counter from concurrent.futures import ThreadPoolExecutor import csv -from imdb_utils import IMDbUtils from bookstack import Bookstack +from imdb_utils import IMDbUtils +from markdown_utils import MarkdownUtils JACKNET_WIKI_URL = "https://wiki.jacknet.io" @@ -130,13 +131,9 @@ def get_film_list(films): return "" -def generate_markdown_link(text, url): - return "[{}]({})".format(text, url) - - def generate_imdb_film_link(film): - return generate_markdown_link(film['title'], "https://www.imdb.com/title/tt{}/".format(film['imdb_id'])) + return MarkdownUtils.generate_markdown_link(film['title'], "https://www.imdb.com/title/tt{}/".format(film['imdb_id'])) def generate_wikipedia_page_link(page_title): - return generate_markdown_link(page_title, "https://en.wikipedia.org/wiki/{}".format(page_title.replace(" ", "_"))) + return MarkdownUtils.generate_markdown_link(page_title, "https://en.wikipedia.org/wiki/{}".format(page_title.replace(" ", "_"))) diff --git a/wiki_pages/FilmsByCountry.py b/wiki_pages/FilmsByCountry.py index 0d33842..40264ce 100644 --- a/wiki_pages/FilmsByCountry.py +++ b/wiki_pages/FilmsByCountry.py @@ -7,6 +7,7 @@ import worldmap import warnings from bookstack import Bookstack +from markdown_utils import MarkdownUtils from vcinema_utils import VCinemaUtils warnings.filterwarnings("ignore") @@ -60,7 +61,7 @@ def build_table(films_by_country): flags = get_flags_dict() - table = "| Country | Films |\n| - | - |" + table = MarkdownUtils.MarkdownTable(["Country", "Films"]) for country, films in films_by_country_sorted.items(): table += "\n" @@ -75,7 +76,7 @@ def build_table(films_by_country): row_data.append(country_label) row_data.append(VCinemaUtils.get_film_list(films)) - table += " | ".join(row_data) + table.add_row(row_data) return table diff --git a/wiki_pages/FilmsByReference.py b/wiki_pages/FilmsByReference.py index 2c7b8aa..ccfcf05 100644 --- a/wiki_pages/FilmsByReference.py +++ b/wiki_pages/FilmsByReference.py @@ -2,6 +2,7 @@ from collections import OrderedDict import string from bookstack import Bookstack +from markdown_utils import MarkdownUtils from vcinema_utils import VCinemaUtils # Page ID of https://wiki.jacknet.io/books/vcinema/page/references @@ -23,15 +24,11 @@ def update_page(token_id, token_secret, films_by_reference_keyword): def build_page(films_by_reference_keyword): reference_keywords_sorted = OrderedDict(sorted(films_by_reference_keyword.items(), key=lambda t: t[0])) - table = "| Referenced | Films |\n| - | - |" + table = MarkdownUtils.MarkdownTable(["Referenced", "Films"]) for year in reference_keywords_sorted.keys(): - table += "\n" - - row_data = [] - row_data.append(VCinemaUtils.generate_wikipedia_page_link(string.capwords(year[13:].replace("-", " ")))) - row_data.append(VCinemaUtils.get_film_list(reference_keywords_sorted[year])) - - table += " | ".join(row_data) + row_data = [VCinemaUtils.generate_wikipedia_page_link(string.capwords(year[13:].replace("-", " "))), + VCinemaUtils.get_film_list(reference_keywords_sorted[year])] + table.add_row(row_data) return table diff --git a/wiki_pages/FilmsByYear.py b/wiki_pages/FilmsByYear.py index d4bcd9b..1b9ffdf 100644 --- a/wiki_pages/FilmsByYear.py +++ b/wiki_pages/FilmsByYear.py @@ -1,6 +1,7 @@ from collections import OrderedDict from bookstack import Bookstack +from markdown_utils import MarkdownUtils from vcinema_utils import VCinemaUtils # Page ID of https://wiki.jacknet.io/books/vcinema/page/films-by-release-year @@ -21,15 +22,10 @@ def update_page(token_id, token_secret, films_by_year): def build_page(films_by_year): films_by_year_sorted = OrderedDict(sorted(films_by_year.items(), key=lambda t: t[0], reverse=True)) - page = "| Year | Films |\n| - | - |" + table = MarkdownUtils.MarkdownTable(["Year", "Films"]) for year in films_by_year_sorted.keys(): - page += "\n" + row_data = [str(year), VCinemaUtils.get_film_list(films_by_year_sorted[year])] + table.add_row(row_data) - row_data = [] - row_data.append(str(year)) - row_data.append(VCinemaUtils.get_film_list(films_by_year_sorted[year])) - - page += " | ".join(row_data) - - return page + return table diff --git a/wiki_pages/HiddenThemes.py b/wiki_pages/HiddenThemes.py index 23d6ddc..1ecce28 100644 --- a/wiki_pages/HiddenThemes.py +++ b/wiki_pages/HiddenThemes.py @@ -1,6 +1,7 @@ from collections import OrderedDict from bookstack import Bookstack +from markdown_utils import MarkdownUtils from vcinema_utils import VCinemaUtils # Page ID of https://wiki.jacknet.io/books/vcinema/page/films-by-reference @@ -60,7 +61,7 @@ def update_page(token_id, token_secret, hidden_themes): def build_page(hidden_themes): hidden_themes = OrderedDict(sorted(hidden_themes.items(), key=lambda t: t[0])) - table = "| Date | Films | Hidden Themes |\n| - | - | - |" + table = MarkdownUtils.MarkdownTable(["Date", "Films", "Hidden Themes"]) for date, data in hidden_themes.items(): table += "\n" @@ -81,6 +82,6 @@ def build_page(hidden_themes): else: row_data.append("N/A") - table += " | ".join(row_data) + table.add_row(row_data) return table diff --git a/wiki_pages/KeywordScores.py b/wiki_pages/KeywordScores.py index e5afca9..4812bf9 100644 --- a/wiki_pages/KeywordScores.py +++ b/wiki_pages/KeywordScores.py @@ -5,6 +5,7 @@ from concurrent.futures import ThreadPoolExecutor from bookstack import Bookstack from imdb_utils import IMDbUtils +from markdown_utils import MarkdownUtils from vcinema_utils import VCinemaUtils # Page ID of https://wiki.jacknet.io/books/vcinema/page/keyword-scores @@ -66,7 +67,7 @@ def build_page(keyword_data, minimum_score=1.0): keyword_data = {k: v for k, v in keyword_data.items() if 'score' in v and v['score'] >= minimum_score} keyword_data = OrderedDict(sorted(keyword_data.items(), key=lambda t: t[1]['score'], reverse=True)) - table = "| Keyword | Number of VCinema Films | Total IMDb entries | Score |\n| - | - | - | - |" + table = MarkdownUtils.MarkdownTable(["Keyword", "Number of VCinema Films", "Total IMDb entries", "Score"]) for keyword, data in keyword_data.items(): table += "\n" @@ -76,6 +77,6 @@ def build_page(keyword_data, minimum_score=1.0): row_data.append(str(len(data['vcinema_films']))) row_data.append(str(data['total'])) row_data.append(str(round(data['score'], 3))) - table += " | ".join(row_data) + table.add_row(row_data) return table