-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrending.py
216 lines (164 loc) · 7.03 KB
/
trending.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
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
from websocket import create_connection
from steem import Steem
import threading
import steem
import time
import random
import math
import socket
import sys
from memo_saving import interpret
from memo_saving import main
import operator
import copy
# Fix imports
import reward_system
import json
class Main():
def __init__(self):
self.posts = {} #{ratio:[post_link, ad_token]} account memos also have add_time:int to know when to delete
self.time_out = 86400 * 3 #(three days) # seconds until an account is removed and it is assumed there will be no duplication after this time
self.locks = {"posts":threading.Lock()}
self.main_account = "co-in"
self.memo_account = "comedy-central"
self.account_memo_account = "co-in-memo"
self.active_key = ""
self.node = "wss://steemd.minnowsupportproject.org"
self.post_time_period = 60 * 10 * 3 # posts an updated trending every 30 min
self.TCP_IP = '127.0.0.1'
self.BUFFER_SIZE = 1024
self.TCP_PORT = 11001
thread = threading.Thread(target=self.post_loop)
thread.daemon = True
thread.start()
thread2 = threading.Thread(target=self.trending_loop)
thread2.daemon = True
thread2.start()
self.communication_loop()
def trending_loop(self):
while True:
try:
print("trending start")
self.create_trending()
except Exception as e:
print("err 4")
print(e)
pass
time.sleep(self.post_time_period)
def communication_loop(self):
# waits for internal socket connections (from celery in the flask_app sections)
# takes the json sent, and then makes a new thread to process it
# also processes jsons sent to get status data of tasks, which is blocking
TCP_IP = self.TCP_IP
TCP_PORT = self.TCP_PORT
BUFFER_SIZE = self.BUFFER_SIZE
while True:
try:
num = 1
# creates re-usable socket and listens until connection is made.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((TCP_IP, TCP_PORT))
s.listen(0)
print("LISTING")
while True:
num += 1
conn, addr = s.accept()
data = ""
if addr[0] == TCP_IP:
try:
# gives id for retrieval of status for tasks
id_num = random.randrange(1000000000000000000000000)
while True:
new_data = conn.recv(BUFFER_SIZE)
if not new_data: break
if not len(new_data) > 0: break
data += new_data.decode()
if not len(new_data) >= BUFFER_SIZE: break
try:
new_list = []
sent = False
if True:
thread = threading.Thread(target=self.read_json, args=([data]))
thread.daemon = True
thread.start()
#conn.send(json.dumps({"idnum": id_num}).encode())
except Exception as e:
print("err 4")
print(e)
conn.send(json.dumps({"success": False, "error": -1}).encode())
except Exception as e:
print("err 3")
print(e)
pass
conn.close()
# creates thread to do stuff with inputs
except Exception as e:
print("err 2")
print(e)
pass
def post_loop(self):
while True:
try:
del_list = []
time.sleep(30)
with self.locks["posts"]:
for i in self.posts:
print(i, self.posts[i], (self.posts[i][2] + self.time_out) - time.time())
if self.posts[i][2] + self.time_out < time.time():
del_list.append(i)
for i in del_list:
del self.accounts[i]
except Exception as e:
print("err 1")
print(e)
def read_json(self, info):
print(info)
info = json.loads(info)
account = info["post_link"].split("/")[4].split("@")[1]
account = interpret.get_account_info(account,self.active_key,self.main_account,self.account_memo_account,self.node)
print(type(account), type(info))
print(account )
with self.locks["posts"]:
if account != None:
account = account[2]
self.posts[info["ratio"]] = [info["post_link"],account["ad-token-perm"], time.time()]
else:
self.posts[info["ratio"]] = [info["post_link"],0]
pass
def create_trending(self):
trending_list = []
with self.locks["posts"]:
for i in self.posts:
trending_list.append([i *(1 + self.posts[i][1]/100),self.posts[i][0]])
trending_list.sort(key=operator.itemgetter(0), reverse=True)
print(trending_list)
self.publish_trending(trending_list)
def publish_trending(self, trending_post_list):
print("PUBLISH TRENDING")
print(len(trending_post_list))
trending_json_list = []
trending_list = {"number":0,"type":"trending","time":round(time.time(),2), "posts":[]}
while True:
print("WHILE TRUE")
print(trending_list)
print(trending_post_list)
if len(trending_post_list) == 0:
trending_json_list.append(json.dumps(trending_list))
break
trending_post = trending_post_list.pop(0)
print("TRENDING POST")
print(trending_post)
copy_list = copy.deepcopy(trending_list)
print(trending_list)
if len(json.dumps(copy_list["posts"].append(trending_post))) > 1950:
trending_json_list.append(json.dumps(trending_list))
trending_list = {"type": "trending","number":trending_list[0]["number"] + 1, "time": round(time.time(), 2), "posts":[]}
else:
print("splitting post")
trending_list["posts"].append([trending_post[0],trending_post[1].split("@")[1]])
print("END WHILE TRUE")
for i in trending_json_list:
print("SAVE MEMO")
main.save_memo(json.loads(i), self.memo_account, self.main_account, self.active_key, node =self.node)
main = Main()