# flake8: noqa: E501 """ Generates an HTML file to visualize the parsed data. """ import json import os from typing import Any, Dict, List, Union from waybacktweets.utils import timestamp_parser class HTMLTweetsVisualizer: """ Class responsible for generating an HTML file to visualize the parsed data. Args: username (str): The username associated with the tweets. json_path (Union[str, List[str]]): The path of the JSON file or the JSON data itself. html_file_path (str, optional): The path where the HTML file will be saved. """ def __init__( self, username: str, json_path: Union[str, List[str]], html_file_path: str = None, ): self.username = username self.json_path = self._json_loader(json_path) self.html_file_path = html_file_path @staticmethod def _json_loader(json_path: Union[str, List[str]]) -> List[Dict[str, Any]]: """ Reads and loads JSON data from a specified file path or JSON string. Args: json_path (Union[str, List[str]]): The path of the JSON file or the JSON data itself. Returns: The content of the JSON file or data. """ if os.path.isfile(json_path): with open(json_path, "r", encoding="utf-8") as f: return json.load(f) return json.loads(json_path) def generate(self) -> str: """ Generates an HTML string that represents the parsed data. Returns: The generated HTML string. """ tweets_per_page = 24 total_pages = (len(self.json_path) + tweets_per_page - 1) // tweets_per_page html = "\n" html += '\n' html += "\n" html += "
" html += '\n' html += ( '\n' ) html += f"Building pagination with JavaScript...
\n' ) for page in range(1, total_pages + 1): html += ( f'\n" # Closes the page div and the container html += "generated by Wayback Tweets↗
\n' html += """ """.format( total_pages=total_pages ) html += "\n" html += "" return html def save(self, html_content: str) -> None: """ Saves the generated HTML string to a file. Args: html_content (str): The HTML string to be saved. """ with open(self.html_file_path, "w", encoding="utf-8") as f: f.write(html_content)