Merge pull request #9 from Z0pyrus/master

Fixed error #7 and an Encode Error I came along
This commit is contained in:
Sam Ireland 2022-07-14 19:54:56 +01:00 committed by GitHub
commit 5c5207a5e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 18 deletions

2
.gitignore vendored
View File

@ -5,3 +5,5 @@ samirelanduk
secrets.py secrets.py
secrets1.py secrets1.py
secrets2.py secrets2.py
/logindata.py
logindata.py

View File

@ -20,7 +20,7 @@ If you get permission errors, try using `sudo` or using a virtual environment.
You will need [ffmpeg](https://ffmpeg.org/) installed somewhere too. You will need [ffmpeg](https://ffmpeg.org/) installed somewhere too.
You then need to create a file in the reddit-save directory called secrets.py. You will need to add four things to this file, your reddit username and password, and a reddit client ID and secret. The latter two are obtained using [the instructions here](https://github.com/reddit-archive/reddit/wiki/OAuth2-Quick-Start-Example#first-steps). The file should look something like this: Rename the file `logindata.py.example` to `logindata.py`. You will need to add four things to this file, your reddit username and password, and a reddit client ID and secret. The latter two are obtained using [the instructions here](https://github.com/reddit-archive/reddit/wiki/OAuth2-Quick-Start-Example#first-steps). The file should look something like this:
```python ```python
REDDIT_USERNAME = "spez" REDDIT_USERNAME = "spez"

4
logindata.py.example Normal file
View File

@ -0,0 +1,4 @@
REDDIT_USERNAME = "username"
REDDIT_PASSWORD = "password"
REDDIT_CLIENT_ID = "id"
REDDIT_SECRET = "secret"

View File

@ -63,7 +63,7 @@ else:
post_html = add_media_preview_to_html(post_html, media) post_html = add_media_preview_to_html(post_html, media)
posts_html.append(post_html) posts_html.append(post_html)
page_html = create_post_page_html(post, post_html) page_html = create_post_page_html(post, post_html)
with open(os.path.join(location, "posts", f"{post.id}.html"), "w") as f: with open(os.path.join(location, "posts", f"{post.id}.html"), "w", encoding="utf-8") as f:
f.write(page_html) f.write(page_html)
posts_html += existing_posts_html posts_html += existing_posts_html
@ -99,7 +99,7 @@ with open(os.path.join("html", "main.js")) as f:
html = html.replace("<script></script>", f"<script>\n{f.read()}\n</script>") html = html.replace("<script></script>", f"<script>\n{f.read()}\n</script>")
html = html.replace("<!--posts-->", "\n".join(posts_html)) html = html.replace("<!--posts-->", "\n".join(posts_html))
html = html.replace("<!--comments-->", "\n".join(comments_html)) html = html.replace("<!--comments-->", "\n".join(comments_html))
with open(os.path.join(location, html_file), "w") as f: with open(os.path.join(location, html_file), "w", encoding="utf-8") as f:
f.write(html) f.write(html)

View File

@ -7,8 +7,8 @@ import re
from datetime import datetime from datetime import datetime
try: try:
from secrets import REDDIT_USERNAME, REDDIT_PASSWORD from logindata import REDDIT_USERNAME, REDDIT_PASSWORD
from secrets import REDDIT_CLIENT_ID, REDDIT_SECRET from logindata import REDDIT_CLIENT_ID, REDDIT_SECRET
except ImportError: except ImportError:
REDDIT_USERNAME = os.getenv("REDDIT_USERNAME") REDDIT_USERNAME = os.getenv("REDDIT_USERNAME")
REDDIT_PASSWORD = os.getenv("REDDIT_PASSWORD") REDDIT_PASSWORD = os.getenv("REDDIT_PASSWORD")
@ -19,6 +19,7 @@ IMAGE_EXTENSIONS = ["gif", "gifv", "jpg", "jpeg", "png"]
VIDEO_EXTENSIONS = ["mp4"] VIDEO_EXTENSIONS = ["mp4"]
PLATFORMS = ["redgifs.com", "gfycat.com", "imgur.com", "youtube.com"] PLATFORMS = ["redgifs.com", "gfycat.com", "imgur.com", "youtube.com"]
def make_client(): def make_client():
"""Creates a PRAW client with the details in the secrets.py file.""" """Creates a PRAW client with the details in the secrets.py file."""
@ -62,7 +63,7 @@ def get_post_html(post):
"""Takes a post object and creates a HTML for it - but not including the """Takes a post object and creates a HTML for it - but not including the
preview HTML.""" preview HTML."""
with open(os.path.join("html", "post-div.html")) as f: with open(os.path.join("html", "post-div.html"), encoding="utf-8") as f:
html = f.read() html = f.read()
dt = datetime.utcfromtimestamp(post.created_utc) dt = datetime.utcfromtimestamp(post.created_utc)
html = html.replace("<!--title-->", post.title) html = html.replace("<!--title-->", post.title)
@ -128,7 +129,8 @@ def save_media(post, location):
match = re.search(r"http([\dA-Za-z\+\:\/\.]+)\.mp4", html.decode()) match = re.search(r"http([\dA-Za-z\+\:\/\.]+)\.mp4", html.decode())
if match: if match:
url = match.group() url = match.group()
else: return None else:
return None
# Is this an imgur image? # Is this an imgur image?
if domain == "imgur.com" and extension != "gifv": if domain == "imgur.com" and extension != "gifv":
@ -184,7 +186,7 @@ def add_media_preview_to_html(post_html, media):
def create_post_page_html(post, post_html): def create_post_page_html(post, post_html):
"""Creates the HTML for a post's own page.""" """Creates the HTML for a post's own page."""
with open(os.path.join("html", "post.html")) as f: with open(os.path.join("html", "post.html"), encoding="utf-8") as f:
html = f.read() html = f.read()
html = html.replace("<!--title-->", post.title) html = html.replace("<!--title-->", post.title)
html = html.replace("<!--post-->", post_html.replace("h2>", "h1>").replace( html = html.replace("<!--post-->", post_html.replace("h2>", "h1>").replace(
@ -193,9 +195,9 @@ def create_post_page_html(post, post_html):
'<source src="media/', '<source src="../media/' '<source src="media/', '<source src="../media/'
)) ))
html = re.sub(r'<a href="posts(.+?)</a>', "", html) html = re.sub(r'<a href="posts(.+?)</a>', "", html)
with open(os.path.join("html", "style.css")) as f: with open(os.path.join("html", "style.css"), encoding="utf-8") as f:
html = html.replace("<style></style>", f"<style>\n{f.read()}\n</style>") html = html.replace("<style></style>", f"<style>\n{f.read()}\n</style>")
with open(os.path.join("html", "main.js")) as f: with open(os.path.join("html", "main.js"), encoding="utf-8") as f:
html = html.replace("<script></script>", f"<script>\n{f.read()}\n</script>") html = html.replace("<script></script>", f"<script>\n{f.read()}\n</script>")
comments_html = [] comments_html = []
post.comments.replace_more(limit=0) post.comments.replace_more(limit=0)
@ -211,7 +213,7 @@ def get_comment_html(comment, children=True, op=None):
"""Takes a post object and creates a HTML for it - it will get its children """Takes a post object and creates a HTML for it - it will get its children
too unless you specify otherwise.""" too unless you specify otherwise."""
with open(os.path.join("html", "comment-div.html")) as f: with open(os.path.join("html", "comment-div.html"), encoding="utf-8") as f:
html = f.read() html = f.read()
dt = datetime.utcfromtimestamp(comment.created_utc) dt = datetime.utcfromtimestamp(comment.created_utc)
author = "[deleted]" author = "[deleted]"