From 1d51f88973debccffb2d5284c77d0672c424a089 Mon Sep 17 00:00:00 2001 From: Z0pyrus <67390601+Z0pyrus@users.noreply.github.com> Date: Sun, 10 Jul 2022 22:33:02 +0200 Subject: [PATCH] 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 --- .gitignore | 4 +++- README.md | 2 +- logindata.py.example | 4 ++++ save.py | 4 ++-- utilities.py | 30 ++++++++++++++++-------------- 5 files changed, 26 insertions(+), 18 deletions(-) create mode 100644 logindata.py.example diff --git a/.gitignore b/.gitignore index fddbddb..26c2ba2 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ __pycache__ samirelanduk secrets.py secrets1.py -secrets2.py \ No newline at end of file +secrets2.py +/logindata.py +logindata.py diff --git a/README.md b/README.md index dd08c75..b3525fa 100644 --- a/README.md +++ b/README.md @@ -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" diff --git a/logindata.py.example b/logindata.py.example new file mode 100644 index 0000000..4389555 --- /dev/null +++ b/logindata.py.example @@ -0,0 +1,4 @@ +REDDIT_USERNAME = "username" +REDDIT_PASSWORD = "password" +REDDIT_CLIENT_ID = "id" +REDDIT_SECRET = "secret" \ No newline at end of file diff --git a/save.py b/save.py index 63cde91..312a44b 100644 --- a/save.py +++ b/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("", f"") html = html.replace("", "\n".join(posts_html)) html = html.replace("", "\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) diff --git a/utilities.py b/utilities.py index 45fb559..7b38d70 100644 --- a/utilities.py +++ b/utilities.py @@ -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("", 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("", post.title) html = html.replace("", post_html.replace("h2>", "h1>").replace( @@ -193,9 +195,9 @@ def create_post_page_html(post, post_html): '", f"") - 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("", f"") 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("", "\n".join(children_html)) - return html \ No newline at end of file + return html