2022-09-13 21:17:46 +00:00
|
|
|
import asyncio
|
|
|
|
import os
|
2022-09-14 17:32:32 +00:00
|
|
|
import random
|
2022-09-13 21:17:46 +00:00
|
|
|
|
|
|
|
# For key generation
|
|
|
|
import string
|
2022-09-14 17:32:32 +00:00
|
|
|
from concurrent.futures import ProcessPoolExecutor
|
2022-09-13 21:17:46 +00:00
|
|
|
|
|
|
|
# For timestamp processing
|
2022-09-14 17:32:32 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from math import ceil
|
|
|
|
|
|
|
|
import ujson
|
2022-09-13 21:17:46 +00:00
|
|
|
|
|
|
|
# For 4chan message parsing
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
from numpy import array_split
|
2022-09-14 17:32:32 +00:00
|
|
|
from siphashc import siphash
|
|
|
|
|
|
|
|
import db
|
|
|
|
import util
|
|
|
|
|
|
|
|
# 4chan schema
|
|
|
|
from schemas.ch4_s import ATTRMAP
|
2022-09-13 21:17:46 +00:00
|
|
|
|
|
|
|
log = util.get_logger("process")
|
|
|
|
|
|
|
|
# Maximum number of CPU threads to use for post processing
|
|
|
|
CPU_THREADS = os.cpu_count()
|
|
|
|
|
|
|
|
p = ProcessPoolExecutor(CPU_THREADS)
|
|
|
|
|
2022-09-14 17:32:32 +00:00
|
|
|
|
2022-09-13 21:17:46 +00:00
|
|
|
def get_hash_key():
|
|
|
|
hash_key = db.r.get("hashing_key")
|
|
|
|
if not hash_key:
|
|
|
|
letters = string.ascii_lowercase
|
|
|
|
hash_key = "".join(random.choice(letters) for i in range(16))
|
|
|
|
log.debug(f"Created new hash key: {hash_key}")
|
|
|
|
db.r.set("hashing_key", hash_key)
|
|
|
|
else:
|
|
|
|
hash_key = hash_key.decode("ascii")
|
|
|
|
log.debug(f"Decoded hash key: {hash_key}")
|
|
|
|
return hash_key
|
|
|
|
|
2022-09-14 17:32:32 +00:00
|
|
|
|
2022-09-13 21:17:46 +00:00
|
|
|
hash_key = get_hash_key()
|
|
|
|
|
2022-09-14 17:32:32 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
2022-09-13 21:17:46 +00:00
|
|
|
async def spawn_processing_threads(data):
|
2022-09-14 17:32:32 +00:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
tasks = []
|
|
|
|
oldts = [x["now"] for x in data if "now" in x]
|
2022-09-13 21:17:46 +00:00
|
|
|
if len(data) < CPU_THREADS:
|
|
|
|
split_data = [data]
|
|
|
|
else:
|
|
|
|
msg_per_core = int(len(data) / CPU_THREADS)
|
|
|
|
print("MSG PER CORE", msg_per_core)
|
|
|
|
split_data = array_split(data, ceil(len(data) / msg_per_core))
|
2022-09-14 17:32:32 +00:00
|
|
|
for index, split in enumerate(split_data):
|
2022-09-13 21:17:46 +00:00
|
|
|
print("DELEGATING TO THREAD", len(split))
|
2022-09-14 17:32:32 +00:00
|
|
|
future = loop.run_in_executor(p, process_data, data)
|
|
|
|
# future = p.submit(process_data, split)
|
|
|
|
tasks.append(future)
|
|
|
|
# results = [x.result(timeout=50) for x in tasks]
|
|
|
|
results = await asyncio.gather(*tasks)
|
|
|
|
print("RESULTS", len(results))
|
|
|
|
|
|
|
|
# Join the results back from the split list
|
|
|
|
flat_list = [item for sublist in results for item in sublist]
|
|
|
|
print("LENFLAT", len(flat_list))
|
|
|
|
print("LENDATA", len(data))
|
|
|
|
|
|
|
|
newts = [x["ts"] for x in flat_list if "ts" in x]
|
|
|
|
print("lenoldts", len(oldts))
|
|
|
|
print("lennewts", len(newts))
|
|
|
|
allts = all(["ts" in x for x in flat_list])
|
|
|
|
print("ALLTS", allts)
|
|
|
|
alllen = [len(x) for x in flat_list]
|
|
|
|
print("ALLLEN", alllen)
|
|
|
|
await db.store_kafka_batch(flat_list)
|
|
|
|
|
|
|
|
|
|
|
|
# @asyncio.coroutine
|
|
|
|
# def process_data_thread(data):
|
|
|
|
# """
|
|
|
|
# Helper to spawn threads to process a list of data.
|
|
|
|
# """
|
|
|
|
# loop = asyncio.get_event_loop()
|
|
|
|
# if len(data) < CPU_THREADS:
|
|
|
|
# split_data = [data]
|
|
|
|
# else:
|
|
|
|
# msg_per_core = int(len(data) / CPU_THREADS)
|
|
|
|
# print("MSG PER CORE", msg_per_core)
|
|
|
|
# split_data = array_split(data, ceil(len(data) / msg_per_core))
|
|
|
|
# for index, split in enumerate(split_data):
|
|
|
|
# print("DELEGATING TO THREAD", len(split))
|
|
|
|
# #f = process_data_thread(split)
|
|
|
|
# yield loop.run_in_executor(p, process_data, data)
|
2022-09-13 21:17:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def process_data(data):
|
2022-09-14 17:32:32 +00:00
|
|
|
print("PROCESS DATA START")
|
|
|
|
# to_store = []
|
2022-09-13 21:17:46 +00:00
|
|
|
for index, msg in enumerate(data):
|
2022-09-14 17:32:32 +00:00
|
|
|
# print("PROCESSING", msg)
|
2022-09-13 21:17:46 +00:00
|
|
|
if msg["src"] == "4ch":
|
|
|
|
board = msg["net"]
|
|
|
|
thread = msg["channel"]
|
|
|
|
# Calculate hash for post
|
|
|
|
post_normalised = ujson.dumps(msg, sort_keys=True)
|
|
|
|
hash = siphash(hash_key, post_normalised)
|
|
|
|
hash = str(hash)
|
|
|
|
redis_key = f"cache.{board}.{thread}.{msg['no']}"
|
|
|
|
key_content = db.r.get(redis_key)
|
|
|
|
if key_content:
|
|
|
|
key_content = key_content.decode("ascii")
|
|
|
|
if key_content == hash:
|
2022-09-14 17:32:32 +00:00
|
|
|
del data[index]
|
2022-09-13 21:17:46 +00:00
|
|
|
continue
|
|
|
|
else:
|
2022-09-14 17:32:32 +00:00
|
|
|
data[index]["type"] = "update"
|
2022-09-13 21:17:46 +00:00
|
|
|
db.r.set(redis_key, hash)
|
2022-09-14 17:32:32 +00:00
|
|
|
if "now" not in data[index]:
|
|
|
|
print("NOW NOT IN INDEX", data[index])
|
|
|
|
for key2, value in list(data[index].items()):
|
2022-09-13 21:17:46 +00:00
|
|
|
if key2 in ATTRMAP:
|
2022-09-14 17:32:32 +00:00
|
|
|
data[index][ATTRMAP[key2]] = data[index][key2]
|
2022-09-13 21:17:46 +00:00
|
|
|
del data[index][key2]
|
2022-09-14 17:32:32 +00:00
|
|
|
if "ts" in data[index]:
|
2022-09-13 21:17:46 +00:00
|
|
|
old_time = data[index]["ts"]
|
|
|
|
# '08/30/22(Tue)02:25:37'
|
|
|
|
time_spl = old_time.split(":")
|
|
|
|
if len(time_spl) == 3:
|
|
|
|
old_ts = datetime.strptime(old_time, "%m/%d/%y(%a)%H:%M:%S")
|
|
|
|
else:
|
|
|
|
old_ts = datetime.strptime(old_time, "%m/%d/%y(%a)%H:%M")
|
|
|
|
# new_ts = old_ts.isoformat()
|
|
|
|
new_ts = int(old_ts.timestamp())
|
|
|
|
data[index]["ts"] = new_ts
|
2022-09-14 17:32:32 +00:00
|
|
|
else:
|
|
|
|
print("MSG WITHOUT TS PROCESS", data[index])
|
|
|
|
continue
|
2022-09-13 21:17:46 +00:00
|
|
|
if "msg" in msg:
|
|
|
|
soup = BeautifulSoup(data[index]["msg"], "html.parser")
|
|
|
|
msg = soup.get_text(separator="\n")
|
2022-09-14 17:32:32 +00:00
|
|
|
data[index]["msg"] = msg
|
|
|
|
# to_store.append(data[index])
|
|
|
|
print("FINISHED PROCESSING DATA")
|
|
|
|
return data
|