forked from Jonpro03/jonprobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_audit_purchases.py
More file actions
243 lines (222 loc) · 7.44 KB
/
3_audit_purchases.py
File metadata and controls
243 lines (222 loc) · 7.44 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import tinydb
from tinydb.queries import where
from tinydb.middlewares import CachingMiddleware
from tinydb.storages import JSONStorage
from os import system, remove
from os.path import exists
import time
import hashlib
from shutil import copy
def get_reddit_post(post_id, sdb):
# Try portfolio/share db first
q = tinydb.Query()
posts = sdb.search(q.id == post_id)
if len(posts) > 0:
return posts[0]
subs = [
"GME",
"Superstonk",
"GMEJungle",
"DDintoGME",
"GME_Computershare",
"infinitypool",
"GMEOrphans"
]
for sub in subs:
db = tinydb.TinyDB(f'{sub}.json')
posts = db.search(q.id == post_id)
if len(posts) > 0:
post = posts[0]
return {
"id": post["id"],
"image_text": post["image_text"],
"image_path": post["img_path"],
"value": None,
"sub": post["subreddit"],
"u": post["author"],
"url": "https://reddit.com"+post["permalink"],
"created": post["created"],
"audited": False
}
return None
def print_post(post):
print(f'ID: {post["id"]}')
print(f'SubReddit: {post["sub"]}')
print(f'Author: {post["u"]}')
print(f'Value: {post["value"]}')
def post_time(post):
global earliest_update
post_time = post["created"]
if post_time < earliest_update:
earliest_update = post_time
def get_new_value(post, sdb):
while True:
ans = input("How much was spent? ")
value = None
try:
value = float(ans)
except:
continue
post["value"] = value
q = tinydb.Query()
sdb.upsert(post, q.id == post["id"])
return
def audit_new():
sdb = tinydb.TinyDB("new_shares_db.json")
q = tinydb.Query()
posts = sdb.search(q.value == None)
posts.extend(sdb.search(q.value == ""))
print(f"{len(posts)} posts to audit.")
for post in posts:
if '.' in post["image_path"]:
system(f"codium {post['image_path']}")
ans = input("Is this a Computershare purchase receipt with visible value? Y/n ")
if 'n' in ans.lower():
post["audited"] = True
post["value"] = 0
sdb.update(post, doc_ids=[post.doc_id])
try:
remove(post['image_path'])
except:
pass
continue
get_new_value(post, sdb)
while True:
ans = input("Audit a record manually? Y/n ")
if 'n' in ans.lower():
break
post_id = input("Post ID: ")
post = get_reddit_post(post_id, sdb)
if post is not None:
post["audited"] = True
sdb.upsert(post, q.id == post_id)
if '.' in post["image_path"]:
if not exists(post["image_path"]):
post["value"] = 0
post["audited"] = True
sdb.update(post, doc_ids=[post.doc_id])
continue
system(f"codium {post['image_path']}")
print_post(post)
ans = input("What should change? Value | Delete | Skip ")
if 's' in ans.lower():
continue
if 'v' in ans.lower():
get_new_value(post, sdb)
if 'd' in ans.lower():
post["value"] = 0
sdb.update(post, doc_ids=[post.doc_id])
try:
remove(post['image_path'])
except:
pass
else:
ans = input("Post not found. Add? ")
if 'n' in ans.lower():
continue
post = {
"id": post_id,
"image_text": "None",
"image_path": "None",
"sub": input("What subreddit? "),
"u": input("Username? "),
"url": input("URL? "),
"created": time.time(),
"audited": True
}
sdb.insert(post)
get_new_value(post, sdb)
total = 0
posts = sdb.all()
sdb.close()
count = len(posts)
for post in posts:
total += post["value"]
print(f'Total shares transferred so far: {total}')
print(f'Total records of transferred shares: {count}')
def audit_all():
sdb = tinydb.TinyDB("new_shares_db.json")
q = tinydb.Query()
posts = sdb.search(~(q.value == 0) & (q.audited == False))
print(f"{len(posts)} posts to audit.")
for post in posts:
post["audited"] = True
sdb.update(post, doc_ids=[post.doc_id])
system(f"codium {post['image_path']}")
print(post["url"])
ans = input(f"{post['value']} Audit this? Y/n ")
if 'y' in ans.lower():
get_new_value(post, sdb)
total = 0
posts = sdb.search(q.value > 0)
sdb.close()
count = len(posts)
for post in posts:
total += post["value"]
print(f'Total value of shares purchased so far: {total}')
print(f'Total records of share purchases: {count}')
def hashfile(path, blocksize=65536):
afile = open(path, 'rb')
hasher = hashlib.md5()
buf = afile.read(blocksize)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(blocksize)
afile.close()
return hasher.hexdigest()
def identify_dupes():
identified = []
sdb = tinydb.TinyDB("new_shares_db.json", storage=CachingMiddleware(JSONStorage))
q = tinydb.Query()
posts = sdb.search((q.img_hash == None) | (q.img_hash == ""))
print(f'Hashing {len(posts)} post images.')
for post in posts:
if post["image_path"] and post["image_path"] != "None":
if not exists(post["image_path"]):
filename = post["image_path"].split('/')[-1]
if exists('images/'+filename):
copy('images/'+filename, 'new_purchase_images/'+filename)
else:
continue
img_hash = hashfile(post["image_path"])
post["img_hash"] = img_hash
sdb.update(post, doc_ids=[post.doc_id])
else:
print(f'No image for {post["url"]}.')
posts = sdb.search(~(q.img_hash == None) & ~(q.value == 0))
print(f'Comparing {len(posts)} post images.')
for post in posts:
dupes = sdb.search((q.img_hash == post["img_hash"]) & ~(q.value == 0))
if len(dupes) > 1:
if dupes[0] not in identified:
identified.extend(dupes)
print("Dupe:")
for dupe in dupes:
print(dupe["sub"],dupe["id"])
sdb.close()
if __name__ == "__main__":
sdb = tinydb.TinyDB("new_shares_db.json")
q = tinydb.Query()
for post in sdb.all():
try:
post["img_hash"]
except:
post["img_hash"] = ""
sdb.update(post, doc_ids=[post.doc_id])
with open("earliest_update.txt", "r+") as f:
earliest_update = int(f.read())
identify_dupes()
audit_new()
audit_all()
with open("earliest_update.txt", "w+") as f:
f.write(str(earliest_update))
total = 0
unique_users = []
posts = sdb.search(q.value > 0)
count = len(posts)
for post in posts:
total += post["value"]
unique_users.append(post['u'])
print(f'Total value of shares purchased so far: {total}')
print(f'Total records of share purchases: {count}')
print(f'Unique purchasers: {len(set(unique_users))}')