/
chan_objects.py
190 lines (156 loc) · 7.09 KB
/
chan_objects.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
import json
import logging
import random
from urllib.parse import urlparse
import bleach
from sortedcontainers import SortedListWithKey
import config
from database.models import Messages
from database.utils import session_scope
from utils.download import download_and_extract
from utils.files import LF
DB_PATH = 'sqlite:///' + config.DATABASE_BITCHAN
logger = logging.getLogger('bitchan.chan_objects')
class ChanPost:
def __init__(self, message_id):
self.thread_id = None
self.target_posts = set([])
self.message_id = bleach.clean(message_id)
self.post_id = self.message_id[-config.ID_LENGTH:].upper()
with session_scope(DB_PATH) as new_session:
message_post = new_session.query(Messages).filter(
Messages.message_id == self.message_id).first()
if message_post and message_post.thread and message_post.thread.chan:
self.thread_id = message_post.thread.thread_hash
self.timestamp = message_post.timestamp_received
self.subject = message_post.subject
self.from_address = message_post.address_from
self.to_address = message_post.thread.chan.address
self.chan = message_post.thread.chan.address
# Check for post replies
if message_post.replies:
for each_reply in json.loads(message_post.replies):
self.target_posts.add(each_reply)
def allow_download(self):
try:
logger.info("{}: Allowing download".format(self.message_id[-config.ID_LENGTH:].upper()))
with session_scope(DB_PATH) as new_session:
message = new_session.query(Messages).filter(
Messages.message_id == self.message_id).first()
if message:
file_path = "{}/{}".format(
config.FILE_DIRECTORY, message.saved_file_filename)
# Pick a download slot to fill (2 slots per domain)
domain = urlparse(message.file_url).netloc
lockfile1 = "/var/lock/upload_{}_1.lock".format(domain)
lockfile2 = "/var/lock/upload_{}_2.lock".format(domain)
lf = LF()
lockfile = random.choice([lockfile1, lockfile2])
if lf.lock_acquire(lockfile, to=600):
try:
(file_download_successful,
file_size,
file_amount,
file_do_not_download,
file_sha256_hashes_match,
media_info,
message_steg) = download_and_extract(
message.thread.chan.address,
self.message_id,
message.file_url,
json.loads(message.file_upload_settings),
json.loads(message.file_extracts_start_base64),
message.upload_filename,
file_path,
message.file_sha256_hash,
message.file_enc_cipher,
message.file_enc_key_bytes,
message.file_enc_password)
finally:
lf.lock_release(lockfile)
if file_download_successful:
if file_size:
message.file_size = file_size
if file_amount:
message.file_amount = file_amount
message.file_download_successful = file_download_successful
message.file_do_not_download = file_do_not_download
message.file_sha256_hashes_match = file_sha256_hashes_match
message.media_info = json.dumps(media_info)
message.message_steg = json.dumps(message_steg)
new_session.commit()
except Exception as e:
logger.error("{}: Error allowing download: {}".format(self.message_id[-config.ID_LENGTH:].upper(), e))
finally:
with session_scope(DB_PATH) as new_session:
message = new_session.query(Messages).filter(
Messages.message_id == self.message_id).first()
message.file_currently_downloading = False
new_session.commit()
class ChanThread:
def __init__(self, chan, thread_id):
self.posts = SortedListWithKey(key=lambda post: post.timestamp)
# post_id -> set(replies)
self.replies_by_post_id = {}
self.timestamp = 0
self.thread_id = thread_id
self.chan = chan
def get_posts(self):
return self.posts
def delete_post(self, post):
try:
self.posts.remove(post)
except Exception as e:
logger.exception("Exception removing post: {}".format(e))
def add_post(self, post):
self.posts.add(post)
self.update_post_links(post)
if post.timestamp > self.timestamp:
self.timestamp = post.timestamp
def update_post_links(self, post):
for postId in post.target_posts:
if postId not in self.replies_by_post_id:
self.replies_by_post_id[postId] = set([])
self.replies_by_post_id[postId].add(post.post_id)
def get_post_replies(self, post_id):
if post_id not in self.replies_by_post_id:
return set([])
return self.replies_by_post_id[post_id]
class ChanList:
def __init__(self, chan):
self.chan = chan
def add_to_list(self, post):
pass
class ChanBoard:
def __init__(self, chan):
self._threads = SortedListWithKey(key=lambda thread: -thread.timestamp)
self._threads_by_id = {}
self.chan = chan
def get_thread_count(self):
return len(self._threads_by_id)
def get_threads(self, start_index, end_index):
return self._threads[start_index:end_index]
def get_thread(self, thread_id):
if thread_id in self._threads_by_id:
return self._threads_by_id[thread_id]
return None
def delete_post(self, post):
thread = self.get_thread(post.thread_id)
if thread:
thread.delete_post(post)
def delete_thread(self, thread_id):
if thread_id in self._threads_by_id:
thread = self._threads_by_id[thread_id]
self._threads.remove(thread)
def add_post(self, post, thread_id):
thread_id = thread_id
thread = self.get_thread(thread_id)
if not thread:
thread = ChanThread(self.chan, thread_id)
self._threads_by_id[thread.thread_id] = thread
else:
# Remove it because we need to re-insert it in sorted order
self._threads.remove(thread)
# logger.info("Updating thread: {}".format(thread.subject))
thread.add_post(post)
self._threads.add(thread)