monolith/sources/ch4.py

175 lines
5.8 KiB
Python
Raw Permalink Normal View History

# Python modules can't start with a number...
2022-09-04 20:40:04 +00:00
import asyncio
import random
import string
from math import ceil
from os import getenv
2022-09-04 20:40:04 +00:00
import aiohttp
from numpy import array_split
import db
import util
2022-09-04 20:29:00 +00:00
# CONFIGURATION #
# Number of 4chan threads to request at once
2022-09-20 17:13:46 +00:00
THREADS_CONCURRENT = int(getenv("MONOLITH_CH4_THREADS_CONCURRENT", 1000))
# Seconds to wait between every THREADS_CONCURRENT requests
2022-09-20 17:13:46 +00:00
THREADS_DELAY = float(getenv("MONOLITH_CH4_THREADS_DELAY", 0.1))
# Seconds to wait between crawls
2022-09-20 17:13:46 +00:00
CRAWL_DELAY = int(getenv("MONOLITH_CH4_CRAWL_DELAY", 5))
# Semaphore value ?
2022-09-20 17:13:46 +00:00
THREADS_SEMAPHORE = int(getenv("MONOLITH_CH4_THREADS_SEMAPHORE", 1000))
# CONFIGURATION END #
class Chan4(object):
"""
4chan indexer, crawler and ingester.
"""
def __init__(self):
name = self.__class__.__name__
self.log = util.get_logger(name)
self.api_endpoint = "https://a.4cdn.org"
2022-09-04 20:40:04 +00:00
# self.boards = ["out", "g", "a", "3", "pol"] #
2022-09-04 20:29:00 +00:00
self.boards = []
2022-09-04 20:40:04 +00:00
# self.thread_deferreds = []
# self.content_deferreds = []
self.log.info(f"Starting crawler bot to {self.api_endpoint}")
self.hash_key = db.r.get("hashing_key")
if not self.hash_key:
letters = string.ascii_lowercase
self.hash_key = "".join(random.choice(letters) for i in range(16))
self.log.debug(f"Created new hash key: {self.hash_key}")
db.r.set("hashing_key", self.hash_key)
else:
self.hash_key = self.hash_key.decode("ascii")
self.log.debug(f"Decoded hash key: {self.hash_key}")
2022-09-04 18:44:25 +00:00
async def run(self):
await self.get_board_list()
while True:
await self.get_thread_lists(self.boards)
await asyncio.sleep(CRAWL_DELAY)
2022-09-04 18:44:25 +00:00
async def get_board_list(self):
2022-09-04 20:29:00 +00:00
responses = await self.api_call({"_": "boards.json"})
for mapped, response in responses:
if not response:
continue
for board in response["boards"]:
self.boards.append(board["board"])
self.log.debug(f"Got boards: {self.boards}")
2022-09-04 18:44:25 +00:00
async def get_thread_lists(self, boards):
# self.log.debug(f"Getting thread list for {boards}")
2022-09-04 18:44:25 +00:00
board_urls = {board: f"{board}/catalog.json" for board in boards}
responses = await self.api_call(board_urls)
to_get = []
flat_map = [board for board, thread in responses]
self.log.debug(f"Got thread list for {flat_map}: {len(responses)}")
2022-09-04 18:44:25 +00:00
for mapped, response in responses:
if not response:
continue
for page in response:
2022-09-04 12:47:32 +00:00
for threads in page["threads"]:
no = threads["no"]
2022-09-04 18:44:25 +00:00
to_get.append((mapped, no))
if not to_get:
return
split_threads = array_split(to_get, ceil(len(to_get) / THREADS_CONCURRENT))
for threads in split_threads:
await self.get_threads_content(threads)
await asyncio.sleep(THREADS_DELAY)
# await self.get_threads_content(to_get)
2022-09-04 18:44:25 +00:00
def take_items(self, dict_list, n):
i = 0
try:
for x in list(dict_list.keys()):
for item in list(dict_list[x]):
yield (x, item)
dict_list[x].remove(item)
i += 1
if i == n:
raise StopIteration
except StopIteration:
print("Take items took", i, "items")
2022-09-04 18:44:25 +00:00
async def get_threads_content(self, thread_list):
2022-09-04 20:40:04 +00:00
thread_urls = {
(board, thread): f"{board}/thread/{thread}.json"
for board, thread in thread_list
}
# self.log.debug(f"Getting information for threads: {thread_urls}")
2022-09-04 18:44:25 +00:00
responses = await self.api_call(thread_urls)
self.log.debug(f"Got information for {len(responses)} threads")
2022-09-04 20:29:00 +00:00
all_posts = {}
2022-09-04 18:44:25 +00:00
for mapped, response in responses:
if not response:
continue
board, thread = mapped
2022-09-04 20:29:00 +00:00
all_posts[mapped] = response["posts"]
if not all_posts:
return
await self.handle_posts(all_posts)
2022-09-04 20:29:00 +00:00
async def handle_posts(self, posts):
2022-09-04 20:29:00 +00:00
to_store = []
for key, post_list in posts.items():
board, thread = key
for index, post in enumerate(post_list):
posts[key][index]["type"] = "msg"
posts[key][index]["src"] = "4ch"
posts[key][index]["net"] = board
posts[key][index]["channel"] = thread
2022-09-04 20:29:00 +00:00
to_store.append(posts[key][index])
2022-09-04 18:44:25 +00:00
if to_store:
await db.queue_message_bulk(to_store)
2022-09-04 18:44:25 +00:00
async def fetch(self, url, session, mapped):
async with session.get(url) as response:
2022-09-04 20:29:00 +00:00
try:
return (mapped, await response.json())
2022-09-04 20:40:04 +00:00
except: # noqa
2022-09-04 20:29:00 +00:00
return (mapped, None)
2022-09-04 18:44:25 +00:00
async def bound_fetch(self, sem, url, session, mapped):
# Getter function with semaphore.
async with sem:
try:
return await self.fetch(url, session, mapped)
2022-09-04 20:40:04 +00:00
except: # noqa
2022-09-04 18:44:25 +00:00
return (mapped, None)
async def api_call(self, methods={}):
tasks = []
sem = asyncio.Semaphore(THREADS_SEMAPHORE)
2022-09-04 18:44:25 +00:00
connector = aiohttp.TCPConnector(limit=None)
async with aiohttp.ClientSession(connector=connector) as session:
for mapped, method in methods.items():
url = f"{self.api_endpoint}/{method}"
# self.log.debug(f"GET {url}")
2022-09-04 18:44:25 +00:00
task = asyncio.create_task(self.bound_fetch(sem, url, session, mapped))
2022-09-04 20:40:04 +00:00
# task = asyncio.ensure_future(self.bound_fetch(sem, url, session))
2022-09-04 18:44:25 +00:00
tasks.append(task)
responses = await asyncio.gather(*tasks)
return responses