-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·226 lines (167 loc) · 6.06 KB
/
main.py
File metadata and controls
executable file
·226 lines (167 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""Reddit to Discord Mirror
Get your latest reddit updates right in Discord
Also with Imgur archiving to deal with those pesky deleted Reddit posts!
"""
import os
import time
import asyncio
from urllib.parse import urlparse
import asyncpraw
import discord
from discord.ext import tasks
import pyimgur
import imgbbpy
import aiohttp
import aiofiles
from config import Config
from db import does_post_exist, insert_post_to_db
FILES_PATH = "/tmp/discord_cache/"
if not os.path.exists(FILES_PATH):
os.mkdir(FILES_PATH)
SUBREDDITS = [
{"name": "pics", "channel_id": 1011474405368797184},
{"name": "space", "channel_id": 1011788355237578285}
]
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
reddit = asyncpraw.Reddit(
client_id=Config.PRAW_CLIENT_ID,
client_secret=Config.PRAW_SECRET_ID,
user_agent=Config.PRAW_USER_AGENT,
username=Config.PRAW_USERNAME,
)
imgur_client = pyimgur.Imgur(Config.IMGUR_CLIENT_ID)
imgbb_client = imgbbpy.AsyncClient(Config.IMGBB_API_KEY)
async def download_file(url) -> None:
"""Download and save file
This will download a file and save it
to the FILE_PATH location by filename.
Args:
url (str): Full address of file to download
"""
fname = url.split("/")[-1]
sema = asyncio.BoundedSemaphore(5)
async with sema, aiohttp.ClientSession() as session:
async with session.get(url) as resp:
assert resp.status == 200
data = await resp.read()
async with aiofiles.open(os.path.join(FILES_PATH, fname), "wb") as outfile:
await outfile.write(data)
async def send_channel_message(submission, channel_id) -> None:
"""Send message to Discord
Args:
submission (object): Praw submission object
channel_id (int): Discord Channel ID Destination
"""
channel_to_upload_to = client.get_channel(channel_id)
try:
embed = discord.Embed(
title=submission.title,
url=f"https://reddit.com{submission.permalink}",
description=submission.selftext,
color=0xFF5733,
)
embed.set_image(url=submission.url)
await channel_to_upload_to.send(embed=embed)
except Exception as error:
print(f"Message Send failed: {error}")
async def upload_to_imgur(file_name: str, post_title: str = None) -> object:
"""Upload to Imgur
Args:
file_name (str): Name of file to upload
post_title (str): submission.title from Praw
Returns:
object: Imgur Response Object
"""
imgur_resp = None
try:
imgur_resp = imgur_client.upload_image(file_name, title=post_title)
except Exception as error:
print(f"Failed to upload to Img:{file_name} error:{error}")
return imgur_resp.link
async def upload_to_imgbb(file_name: str) -> object:
"""Upload to Imgbb
Args:
file_name (str): Name of file to upload
post_title (str): submission.title from Praw
Returns:
object: Imgur Response Object
"""
imgur_resp = None
try:
imgur_resp = await imgbb_client.upload(file=file_name)
except Exception as error:
print(f"Failed to upload to Img:{file_name} error:{error}")
return imgur_resp.url
async def update_subreddit_posts(subreddit_name: str, channel_id: int) -> None:
"""Process a subreddits posts
Get all new posts in a subreddit and store
then to a database if they don't already exist.
If a submission.url exists on i.redd.it, it is
mirrored to imgur for safe keeping.
Args:
subreddit_name (str): Name of the subreddit
channel_id (int): Discord Channel Destnation
"""
subreddit = await reddit.subreddit(subreddit_name)
count = 0
bad = 0
async for submission in subreddit.new():
if not does_post_exist(submission.id):
# Check url for images
if submission.url:
# Is it an i.reddit.it url
if urlparse(submission.url).netloc == "i.redd.it":
# Download the file, saves it locally as /tmp/{filename}
await download_file(submission.url)
# Get the url of the mirrored iamge
fname = submission.url.split("/")[-1]
# imgur_response = await upload_to_imgur(
# f"{FILES_PATH}{fname}", submission.title
# )
imgur_response = await upload_to_imgbb(
f"{FILES_PATH}{fname}"
)
# Update object with our new URL
submission.url = imgur_response
# Store post to DB
await insert_post_to_db(submission)
# Send notification
await send_channel_message(submission, channel_id)
count = count + 1
time.sleep(1)
else:
bad = bad + 1
print(f"Update Complete for {subreddit_name}, new: {count} existing: {bad}")
async def subreddit_main():
"""Subreddit Iterator
This iterates over each of our Subreddits.
"""
for subreddit in SUBREDDITS:
await update_subreddit_posts(subreddit["name"], subreddit["channel_id"])
async def cleanup_files() -> None:
"""Remove temp files
This is to remove any files we've downloaded
before pushing them to Imgur.
"""
for file_name in os.listdir(FILES_PATH):
try:
os.remove(os.path.join(FILES_PATH, file_name))
except Exception as error:
print(f"Delete File Failed, file:{file_name} error:{error}")
@client.event
async def on_ready() -> None:
"""Start Tasks
Now that we're connected to Discord, start
the scheduled tasks. If we don't wait, the
firt send will fail.
"""
print(f"{client.user} has connected to Discord, starting Tasks!")
scheduled_tasks.start()
@tasks.loop(minutes=1.0)
async def scheduled_tasks() -> None:
"""Task loop to update Reddit posts"""
await subreddit_main()
await cleanup_files()
client.run(Config.DISCORD_TOKEN)