-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemes.py
More file actions
150 lines (123 loc) · 4.82 KB
/
memes.py
File metadata and controls
150 lines (123 loc) · 4.82 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
import aiohttp
import aioipfs
import os
import pprint
import asyncio
from dverify import get_user_token
from config import MEME_BACKEND, ADMIN_TOKEN, DEFAULT_TAG, IMG_DIR
MESSAGE_USER_REACTION_CACHE = {
} # message with 2, meme_id and a list of users who have tried to upvote (either sucesfully or not)
def valid_image_url(url: str):
image_extensions = [
'png', 'jpg', 'jpeg', 'gif', 'bmp', 'PNG', 'JPG', 'JPEG', 'GIF', 'BMP'
]
for image_extension in image_extensions:
if url.endswith('.' + image_extension):
return True
return False
async def upload_to_ipfs(files, file_hash_dict):
print('now adding to ipfs')
client = aioipfs.AsyncIPFS()
async for added_file in client.add(*files, recursive=True):
print('Imported file {0}, CID: {1}'.format(added_file['Name'],
added_file['Hash']))
file_hash_dict[added_file['Name']] = added_file['Hash']
await client.close()
async def meme_party_post(image_name, message, file_hash_dict):
print('now adding to meme_party')
discord_username = str(message.author)
url = f"{MEME_BACKEND}/museum/memes/"
headers = {
"Authorization": f"Token {ADMIN_TOKEN}",
"Content-Type": "application/json"
}
json = {
"title": f'{image_name}',
"meme_lord": f"{discord_username}",
"description": "",
"image":
f"http://139.59.103.146:8080/ipfs/{file_hash_dict[image_name]}",
"tags": [{
"name": f"{DEFAULT_TAG}"
}],
"discord_username": discord_username
}
async with aiohttp.ClientSession() as session:
async with session.post(url, json=json, headers=headers) as resp:
print(resp.status)
try:
meme = await resp.json()
except:
meme = await resp.text()
open('erro_log.html', 'w').write(meme)
return
logging_fields = ['image', 'id', 'poaster', 'meme_lord']
meme = {k: meme[k] for k in logging_fields}
pprint.pprint(meme)
return resp.status, meme
async def download_image(url: str, images_path: str = ""):
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
if resp.status == 200:
image_name = os.path.basename(url)
with open(images_path, "wb") as f:
f.write(await resp.read())
async def handle_image(attachment, message):
file_hash_dict = {}
print(f'Processing New Meme From: {message.author}')
await download_image(attachment.url,
os.path.join(IMG_DIR, attachment.filename))
await upload_to_ipfs([os.path.join(IMG_DIR, attachment.filename)],
file_hash_dict)
upload_status, meme = await meme_party_post(attachment.filename, message,
file_hash_dict)
if upload_status in [200, 201]:
await message.add_reaction("✅")
MESSAGE_USER_REACTION_CACHE[message.id] = {
"meme_id": meme["id"],
"upvoters": []
}
else:
await message.add_reaction("❌")
print(MESSAGE_USER_REACTION_CACHE)
async def handle_upvote(user, message):
max_times_to_sleep = 3
while message.id not in MESSAGE_USER_REACTION_CACHE and max_times_to_sleep > 0:
await asyncio.sleep(10)
max_times_to_sleep -= 1
if message.id not in MESSAGE_USER_REACTION_CACHE:
return
if str(user) in MESSAGE_USER_REACTION_CACHE[message.id]["upvoters"]:
return
user_auth_token = await get_user_token(user)
if user_auth_token is None:
return
print('now handling upvote')
meme_id = int(MESSAGE_USER_REACTION_CACHE[message.id]["meme_id"])
url = f"{MEME_BACKEND}/museum/upvote/"
headers = {
"Authorization": f"Token {user_auth_token}",
"Content-Type": "application/json"
}
json = {
"id": meme_id,
}
async with aiohttp.ClientSession() as session:
async with session.post(url, json=json, headers=headers) as resp:
print(resp.status)
try:
meme = await resp.json()
except:
meme = await resp.text()
open('erro_log.html', 'w').write(meme)
return
logging_fields = ['image', 'id', 'poaster', 'meme_lord']
meme = {k: meme[k] for k in logging_fields}
print(f'upvoted {meme["id"]} on behalf of {user}')
# pprint.pprint(meme)
MESSAGE_USER_REACTION_CACHE[message.id]["upvoters"].append(
str(user))
print(MESSAGE_USER_REACTION_CACHE)
return resp.status, meme
# reaction handling
# if message.id is not in cache then asyncio.sleep for upto 3 times