forked from Chandan8186/cs3103-grp-30
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_link.py
163 lines (137 loc) · 6.09 KB
/
image_link.py
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
from datetime import timedelta, date
from flask import flash
import aiohttp
import asyncio
# Don't overwhelm the slow server
REQUEST_LIMIT = 25
# Unlikely to check email after 90 days
EXPIRY_DATE = (date.today() + timedelta(days=90)).strftime("%m/%d/%Y")
"""
A coroutine to generate a single image link.
"""
async def _make_image_link(session, unique_id) -> str:
params = {"url": "https://upload.wikimedia.org/wikipedia/commons/c/ca/1x1.png",
"type": "json",
"private": "1",
"expire": EXPIRY_DATE,
# Ensures the input is a string, as there is no type checking for unique_id
"custom": str(unique_id)}
for i in range(3): # Retries
if i > 0:
asyncio.sleep(1)
async with session.get("https://ulvis.net/API/write/get", params=params) as redirect:
if not redirect.ok:
print("make_image_links Error:", redirect.status)
continue
try:
parsed_redirect = await redirect.json()
except aiohttp.ContentTypeError:
print("make_image_links Error:", redirect.status)
continue
if "data" not in parsed_redirect or "url" not in parsed_redirect["data"]:
error_msg = f"make_image_links Error for {unique_id}."
# Check if error message can be found
if "data" in parsed_redirect and "status" in parsed_redirect["data"]:
error_msg += f" {parsed_redirect['data']['status']}."
print(error_msg)
continue
return parsed_redirect["data"]["url"]
# flash("Ulvis server is down... View stats may not function properly.")
return "https://ulvis.net/" + str(unique_id)
"""
Asynchronously generates unique links for each unique id.
Usage: asyncio.run(make_image_links(unique_id_list))
Parameters:
unique_id_list (list[str]): List of unique ids.
Returns:
list[str]: List of unique links.
"""
async def make_image_links(unique_id_list) -> list[str]:
conn = aiohttp.TCPConnector(limit=REQUEST_LIMIT)
async with aiohttp.ClientSession(connector = conn) as session:
coroutines = [_make_image_link(session, unique_id) for unique_id in unique_id_list]
return await asyncio.gather(*coroutines)
"""
Manages obtaining image counts.
Unique ids are used to identify and retrieve links for getting image counts.
"""
class Image_Count_Manager:
def __init__(self):
self.unique_id_list = []
self.session = None
# Manually manage an event loop for this instance, to allow session (aiohttp.ClientSession)
# to be persistent across each call of get_image_counts(), making it much more responsive.
self.event_loop = asyncio.new_event_loop()
self.event_loop.run_until_complete(self._create_session())
def __del__(self):
# Connector has to be manually closed since session.close() is a coroutine, and it may not be able
# to run as event loops could be closed when exiting the program. The connector is then
# manually detached to prevent false error messages of connector not being closed.
self.session.connector.close()
self.session.detach()
self.event_loop.close()
async def _create_session(self):
conn = aiohttp.TCPConnector(limit=REQUEST_LIMIT)
self.session = aiohttp.ClientSession(connector = conn)
"""
Updates the unique ids which are used to obtain image view counts.
"""
def update_unique_id_list(self, unique_id_list):
self.unique_id_list = unique_id_list
"""
A coroutine to obtain a single image count.
"""
async def _get_image_count(self, unique_id) -> int:
if unique_id == None:
return "error"
params = {"type": "json",
# Ensures the input is a string, as there is no type checking for unique_id
"id": str(unique_id)}
for i in range(3): # Retries
if i > 0:
asyncio.sleep(1)
async with self.session.get("https://ulvis.net/API/read/get", params=params) as redirect:
if not redirect.ok:
print("Image_Link Error:", redirect.status)
continue
try:
parsed_redirect = await redirect.json()
except aiohttp.ContentTypeError:
print("Image_Link Error:", redirect.status)
continue
if "data" not in parsed_redirect or "hits" not in parsed_redirect["data"]:
error_msg = f"Image_Link Error for {unique_id}."
# Check if error message can be found
if "error" in parsed_redirect and "msg" in parsed_redirect["error"]:
error_msg += f" {parsed_redirect['error']['msg']}."
print(error_msg)
continue
return parsed_redirect["data"]["hits"]
# flash("View stats failed to load for an email.")
return "error"
"""
Asynchronously gets image download count for each unique id.
Usage: asyncio.run(_get_image_counts(unique_id_list))
Parameters:
unique_id_list (list[str]): List of unique ids.
Returns:
list[str]: List of image download counts.
"""
async def _get_image_counts(self) -> list[str]:
coroutines = [self._get_image_count(unique_id) for unique_id in self.unique_id_list]
return await asyncio.gather(*coroutines)
"""
Asynchronously gets image download count for each unique id in self.unique_id_list.
"""
def get_image_counts(self) -> list[str]:
try:
coroutine = self._get_image_counts()
image_counts = self.event_loop.run_until_complete(coroutine)
except RuntimeError: # Previous loop still running
coroutine.close()
return []
# Remove invalid ids to prevent unnecessary server spam
for i in range(len(image_counts)):
if image_counts[i] == "error":
self.unique_id_list[i] = None
return image_counts