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
4
.gitignore
vendored
4
.gitignore
vendored
@ -4,4 +4,6 @@ __pycache__
|
|||||||
samirelanduk
|
samirelanduk
|
||||||
secrets.py
|
secrets.py
|
||||||
secrets1.py
|
secrets1.py
|
||||||
secrets2.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 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
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)
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
30
utilities.py
30
utilities.py
@ -6,9 +6,9 @@ import youtube_dl
|
|||||||
import re
|
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)
|
||||||
@ -105,7 +106,7 @@ def save_media(post, location):
|
|||||||
with open(os.path.join(location, "media", filename), "wb") as f:
|
with open(os.path.join(location, "media", filename), "wb") as f:
|
||||||
f.write(response.content)
|
f.write(response.content)
|
||||||
return filename
|
return filename
|
||||||
|
|
||||||
# Is this a v.redd.it link?
|
# Is this a v.redd.it link?
|
||||||
if domain == "redd.it":
|
if domain == "redd.it":
|
||||||
downloader = Downloader(max_q=True, log=False)
|
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())
|
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":
|
||||||
for extension in IMAGE_EXTENSIONS:
|
for extension in IMAGE_EXTENSIONS:
|
||||||
direct_url = f'https://i.{url[url.find("//") + 2:]}.{extension}'
|
direct_url = f'https://i.{url[url.find("//") + 2:]}.{extension}'
|
||||||
direct_url = direct_url.replace("i.imgur.com", "imgur.com")
|
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,
|
"nocheckcertificate": True, "quiet": True, "no_warnings": True,
|
||||||
"ignoreerrors": True,
|
"ignoreerrors": True,
|
||||||
"outtmpl": os.path.join(
|
"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:
|
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):
|
def add_media_preview_to_html(post_html, media):
|
||||||
"""Takes post HTML and returns a modified version with the preview
|
"""Takes post HTML and returns a modified version with the preview
|
||||||
inserted."""
|
inserted."""
|
||||||
|
|
||||||
extension = media.split(".")[-1]
|
extension = media.split(".")[-1]
|
||||||
location = "/".join(["media", media])
|
location = "/".join(["media", media])
|
||||||
if extension in IMAGE_EXTENSIONS:
|
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):
|
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]"
|
||||||
@ -234,4 +236,4 @@ def get_comment_html(comment, children=True, op=None):
|
|||||||
for child in comment.replies:
|
for child in comment.replies:
|
||||||
children_html.append(get_comment_html(child, children=False, op=op))
|
children_html.append(get_comment_html(child, children=False, op=op))
|
||||||
html = html.replace("<!--children-->", "\n".join(children_html))
|
html = html.replace("<!--children-->", "\n".join(children_html))
|
||||||
return html
|
return html
|
||||||
|
Loading…
Reference in New Issue
Block a user