-
Notifications
You must be signed in to change notification settings - Fork 22
/
url_receiver.py
71 lines (55 loc) · 2.82 KB
/
url_receiver.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
import requests
import json
import time
import pandas as pd
import os
import re
from datetime import datetime, timezone, timedelta
from dateutil.parser import parse
class Receiver:
def __init__(self, params, index):
self.params = params
self.index = index
self.sender_initializer()
self.latest_image_timestamp = datetime.now(timezone.utc) - timedelta(days=1)
self.df = pd.DataFrame(columns = ['prompt', 'url', 'filename', 'is_downloaded'])
def sender_initializer(self):
with open(self.params, "r") as json_file:
params = json.load(json_file)
self.channelid=params['channelid'][self.index]
self.authorization=params['authorization']
self.headers = {'authorization' : self.authorization}
def retrieve_messages(self):
r = requests.get(
f'https://discord.com/api/v10/channels/{self.channelid}/messages?limit={10}', headers=self.headers)
jsonn = json.loads(r.text)
return jsonn
def collecting_results(self):
message_list = self.retrieve_messages()
self.awaiting_list = pd.DataFrame(columns = ['prompt', 'status'])
for message in message_list:
if (message['author']['username'] == 'Midjourney Bot') and ('**' in message['content']):
if len(message['attachments']) > 0:
if (message['attachments'][0]['filename'][-4:] == '.png') or ('(Open on website for full quality)' in message['content']):
id = message['id']
prompt = message['content'].split('**')[1].split(' --')[0]
url = message['attachments'][0]['url']
filename = message['attachments'][0]['filename']
if id not in self.df.index:
self.df.loc[id] = [prompt, url, filename, 0]
self.latest_image_timestamp = parse(message["timestamp"])
else:
id = message['id']
prompt = message['content'].split('**')[1].split(' --')[0]
if ('(fast)' in message['content']) or ('(relaxed)' in message['content']):
try:
status = re.findall("(\w*%)", message['content'])[0]
except:
status = 'unknown status'
self.awaiting_list.loc[id] = [prompt, status]
else:
id = message['id']
prompt = message['content'].split('**')[1].split(' --')[0]
if '(Waiting to start)' in message['content']:
status = 'Waiting to start'
self.awaiting_list.loc[id] = [prompt, status]