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:
Z0pyrus 2022-07-10 22:33:02 +02:00
parent 6ab2bea361
commit 1d51f88973
5 changed files with 26 additions and 18 deletions

4
.gitignore vendored
View File

@ -4,4 +4,6 @@ __pycache__
samirelanduk
secrets.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 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
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)
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)

View File

@ -6,9 +6,9 @@ import youtube_dl
import re
from datetime import datetime
try:
from secrets import REDDIT_USERNAME, REDDIT_PASSWORD
from secrets import REDDIT_CLIENT_ID, REDDIT_SECRET
try:
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)
@ -105,7 +106,7 @@ def save_media(post, location):
with open(os.path.join(location, "media", filename), "wb") as f:
f.write(response.content)
return filename
# Is this a v.redd.it link?
if domain == "redd.it":
downloader = Downloader(max_q=True, log=False)
@ -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:
@ -165,7 +167,7 @@ def save_media(post, location):
def add_media_preview_to_html(post_html, media):
"""Takes post HTML and returns a modified version with the preview
inserted."""
extension = media.split(".")[-1]
location = "/".join(["media", media])
if extension in IMAGE_EXTENSIONS:
@ -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]"
@ -234,4 +236,4 @@ def get_comment_html(comment, children=True, op=None):
for child in comment.replies:
children_html.append(get_comment_html(child, children=False, op=op))
html = html.replace("<!--children-->", "\n".join(children_html))
return html
return html