From: Claromes Date: Mon, 24 Jun 2024 14:39:43 +0000 (-0300) Subject: add pagination to the generated HTML X-Git-Url: https://git.claromes.com/?a=commitdiff_plain;h=e3d3b14ee71fb7411d99b34473d5cb3ce3168af7;p=waybacktweets.git add pagination to the generated HTML --- diff --git a/waybacktweets/api/visualize.py b/waybacktweets/api/visualize.py index e1e9e8e..6621667 100644 --- a/waybacktweets/api/visualize.py +++ b/waybacktweets/api/visualize.py @@ -54,9 +54,18 @@ class HTMLTweetsVisualizer: Returns: The generated HTML string. """ + tweets_per_page = 24 + total_pages = (len(self.json_path) + tweets_per_page - 1) // tweets_per_page - html = f"\n\n" - html += f"\n\n@{self.username}'s archived tweets\n" + html = "\n" + html = f"\n" + + html += "\n" + html += '\n' + html += '\n' + html += f"@{self.username}'s archived tweets\n" + + # Adds styling html += "\n" + html += "\n\n" + html += f"

@{self.username}'s archived tweets

\n" - html += '
\n' - - for index, tweet in enumerate(self.json_path): - html += '
\n' - - if not tweet["available_tweet_text"]: - iframe_src = { - "Archived Tweet": tweet["archived_tweet_url"], - "Parsed Archived Tweet": tweet["parsed_archived_tweet_url"], - "Original Tweet": tweet["original_tweet_url"], - "Parsed Tweet": tweet["parsed_tweet_url"], - } - - for key, value in iframe_src.items(): - key_cleaned = key.replace(" ", "_") - - html += f'

{key}↗\n' - html += '

\n' - html += ( - f'\n' - ) - html += f'\n' - html += '
\n' - - html += f'
Loading...
\n' - html += f'\n' - html += "
\n" - html += "
\n" - - html += """ - - """.format( - index=index, url=value, key_cleaned=key_cleaned - ) - - if tweet["available_tweet_text"]: - html += "
\n" - html += f'

Available Tweet Content: {tweet["available_tweet_text"]}

\n' - html += f'

Available Tweet Is Retweet: {tweet["available_tweet_is_RT"]}

\n' - html += f'

Available Tweet Username: {tweet["available_tweet_info"]}

\n' - - html += "
\n" - html += f'

Archived Tweet: {tweet["archived_tweet_url"]}

\n' - html += f'

Parsed Archived Tweet: {tweet["parsed_archived_tweet_url"]}

\n' - html += f'

Original Tweet: {tweet["original_tweet_url"]}

\n' - html += ( - f'

Parsed Tweet: {tweet["parsed_tweet_url"]}

\n' - ) - html += f'

Archived URL Key: {tweet["archived_urlkey"]}

\n' - html += f'

Archived Timestamp: {timestamp_parser(tweet["archived_timestamp"])} ({tweet["archived_timestamp"]})

\n' - html += f'

Archived mimetype: {tweet["archived_mimetype"]}

\n' - html += f'

Archived Statuscode: {tweet["archived_statuscode"]}

\n' - html += ( - f'

Archived Digest: {tweet["archived_digest"]}

\n' - ) + + html += ( + '

Building pagination with JavaScript...

\n' + ) + + for page in range(1, total_pages + 1): html += ( - f'

Archived Length: {tweet["archived_length"]}

\n' + f'\n" + html += '
\n' + + start_index = (page - 1) * tweets_per_page + end_index = min(start_index + tweets_per_page, len(self.json_path)) + + for index in range(start_index, end_index): + tweet = self.json_path[index] + html += '
\n' + + if not tweet["available_tweet_text"]: + iframe_src = { + "Archived Tweet": tweet["archived_tweet_url"], + "Parsed Archived Tweet": tweet["parsed_archived_tweet_url"], + "Original Tweet": tweet["original_tweet_url"], + "Parsed Tweet": tweet["parsed_tweet_url"], + } + + for key, value in iframe_src.items(): + key_cleaned = key.replace(" ", "_") + + html += '
\n' + html += f'\n' + html += f'\n' + html += '
\n' + + html += f'
Loading...
\n' + html += f'\n' + html += "
\n" + html += "
\n" + + html += """ + + """.format( + index=index, url=value, key_cleaned=key_cleaned + ) + + if tweet["available_tweet_text"]: + html += "
\n" + html += f'

Available Tweet Content: {tweet["available_tweet_text"]}

\n' + html += f'

Available Tweet Is Retweet: {tweet["available_tweet_is_RT"]}

\n' + html += f'

Available Tweet Username: {tweet["available_tweet_info"]}

\n' + html += "
\n" + html += f'

Archived Tweet: {tweet["archived_tweet_url"]}

\n' + html += f'

Parsed Archived Tweet: {tweet["parsed_archived_tweet_url"]}

\n' + html += f'

Original Tweet: {tweet["original_tweet_url"]}

\n' + html += f'

Parsed Tweet: {tweet["parsed_tweet_url"]}

\n' + html += f'

Archived URL Key: {tweet["archived_urlkey"]}

\n' + html += f'

Archived Timestamp: {timestamp_parser(tweet["archived_timestamp"])} ({tweet["archived_timestamp"]})

\n' + html += f'

Archived mimetype: {tweet["archived_mimetype"]}

\n' + html += f'

Archived Statuscode: {tweet["archived_statuscode"]}

\n' + html += ( + f'

Archived Digest: {tweet["archived_digest"]}\n' + ) + html += f'

Archived Length: {tweet["archived_length"]}

\n' + html += "
\n" + + html += "
\n
\n" # Closes the page div and the container + + html += "
\n" + + # Adds navigation for the pages + html += '\n" - html += '

generated by Wayback Tweets↗

\n' + + html += '

generated by Wayback Tweets↗

\n' + + html += """ + + """.format( + total_pages=total_pages + ) + html += "\n" return html