Fixed ImportError: cannot import name 'token_urlsafe' from 'secrets' #7
Fixed UnicodeEncodeError: 'charmap' codec can't encode characters in position 28919-28920: character maps to <undefined>
This commit is contained in:
parent
6ab2bea361
commit
1d51f88973
2
.gitignore
vendored
2
.gitignore
vendored
@ -5,3 +5,5 @@ samirelanduk
|
||||
secrets.py
|
||||
secrets1.py
|
||||
secrets2.py
|
||||
/logindata.py
|
||||
logindata.py
|
||||
|
@ -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 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
|
||||
REDDIT_USERNAME = "spez"
|
||||
|
4
logindata.py.example
Normal file
4
logindata.py.example
Normal file
@ -0,0 +1,4 @@
|
||||
REDDIT_USERNAME = "username"
|
||||
REDDIT_PASSWORD = "password"
|
||||
REDDIT_CLIENT_ID = "id"
|
||||
REDDIT_SECRET = "secret"
|
4
save.py
4
save.py
@ -63,7 +63,7 @@ else:
|
||||
post_html = add_media_preview_to_html(post_html, media)
|
||||
posts_html.append(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)
|
||||
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("<!--posts-->", "\n".join(posts_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)
|
||||
|
||||
|
||||
|
22
utilities.py
22
utilities.py
@ -7,8 +7,8 @@ import re
|
||||
from datetime import datetime
|
||||
|
||||
try:
|
||||
from secrets import REDDIT_USERNAME, REDDIT_PASSWORD
|
||||
from secrets import REDDIT_CLIENT_ID, REDDIT_SECRET
|
||||
from logindata import REDDIT_USERNAME, REDDIT_PASSWORD
|
||||
from logindata import REDDIT_CLIENT_ID, REDDIT_SECRET
|
||||
except ImportError:
|
||||
REDDIT_USERNAME = os.getenv("REDDIT_USERNAME")
|
||||
REDDIT_PASSWORD = os.getenv("REDDIT_PASSWORD")
|
||||
@ -19,6 +19,7 @@ IMAGE_EXTENSIONS = ["gif", "gifv", "jpg", "jpeg", "png"]
|
||||
VIDEO_EXTENSIONS = ["mp4"]
|
||||
PLATFORMS = ["redgifs.com", "gfycat.com", "imgur.com", "youtube.com"]
|
||||
|
||||
|
||||
def make_client():
|
||||
"""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
|
||||
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()
|
||||
dt = datetime.utcfromtimestamp(post.created_utc)
|
||||
html = html.replace("<!--title-->", post.title)
|
||||
@ -128,10 +129,11 @@ def save_media(post, location):
|
||||
match = re.search(r"http([\dA-Za-z\+\:\/\.]+)\.mp4", html.decode())
|
||||
if match:
|
||||
url = match.group()
|
||||
else: return None
|
||||
else:
|
||||
return None
|
||||
|
||||
# Is this an imgur image?
|
||||
if domain =="imgur.com" and extension != "gifv":
|
||||
if domain == "imgur.com" and extension != "gifv":
|
||||
for extension in IMAGE_EXTENSIONS:
|
||||
direct_url = f'https://i.{url[url.find("//") + 2:]}.{extension}'
|
||||
direct_url = direct_url.replace("i.imgur.com", "imgur.com")
|
||||
@ -149,7 +151,7 @@ def save_media(post, location):
|
||||
"nocheckcertificate": True, "quiet": True, "no_warnings": True,
|
||||
"ignoreerrors": True,
|
||||
"outtmpl": os.path.join(
|
||||
location, "media", f"{readable_name}_{post.id}" + ".%(ext)s"
|
||||
location, "media", f"{readable_name}_{post.id}" + ".%(ext)s"
|
||||
)
|
||||
}
|
||||
with youtube_dl.YoutubeDL(options) as ydl:
|
||||
@ -184,7 +186,7 @@ def add_media_preview_to_html(post_html, media):
|
||||
def create_post_page_html(post, post_html):
|
||||
"""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 = html.replace("<!--title-->", post.title)
|
||||
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/'
|
||||
))
|
||||
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>")
|
||||
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>")
|
||||
comments_html = []
|
||||
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
|
||||
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()
|
||||
dt = datetime.utcfromtimestamp(comment.created_utc)
|
||||
author = "[deleted]"
|
||||
|
Loading…
Reference in New Issue
Block a user