2022-09-02 21:30:45 +00:00
|
|
|
# Python modules can't start with a number...
|
2022-09-04 20:40:04 +00:00
|
|
|
import asyncio
|
2022-09-02 21:30:45 +00:00
|
|
|
import random
|
|
|
|
import string
|
2022-09-05 06:20:30 +00:00
|
|
|
from math import ceil
|
2022-09-02 21:30:45 +00:00
|
|
|
|
2022-09-04 20:40:04 +00:00
|
|
|
import aiohttp
|
2022-09-05 06:20:30 +00:00
|
|
|
from numpy import array_split
|
2022-09-02 21:30:45 +00:00
|
|
|
|
|
|
|
import db
|
|
|
|
import util
|
2022-09-04 20:29:00 +00:00
|
|
|
|
2022-09-05 06:20:30 +00:00
|
|
|
# CONFIGURATION #
|
|
|
|
|
|
|
|
# Number of 4chan threads to request at once
|
2022-09-13 21:17:46 +00:00
|
|
|
THREADS_CONCURRENT = 1000
|
2022-09-05 06:20:30 +00:00
|
|
|
|
|
|
|
# Seconds to wait between every THREADS_CONCURRENT requests
|
2022-09-13 21:17:46 +00:00
|
|
|
THREADS_DELAY = 0.1
|
2022-09-05 06:20:30 +00:00
|
|
|
|
|
|
|
# Seconds to wait between crawls
|
|
|
|
CRAWL_DELAY = 5
|
|
|
|
|
|
|
|
# Semaphore value ?
|
2022-09-13 21:17:46 +00:00
|
|
|
THREADS_SEMAPHORE = 1000
|
2022-09-05 06:20:30 +00:00
|
|
|
|
|
|
|
# CONFIGURATION END #
|
|
|
|
|
2022-09-05 06:20:30 +00:00
|
|
|
|
2022-09-02 21:30:45 +00:00
|
|
|
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-02 21:30:45 +00:00
|
|
|
|
2022-09-04 20:40:04 +00:00
|
|
|
# self.thread_deferreds = []
|
|
|
|
# self.content_deferreds = []
|
2022-09-02 21:30:45 +00:00
|
|
|
|
|
|
|
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()
|
2022-09-05 06:20:30 +00:00
|
|
|
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):
|
2022-09-16 16:09:49 +00:00
|
|
|
# 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 = []
|
2022-09-16 16:09:49 +00:00
|
|
|
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))
|
|
|
|
|
2022-09-05 06:20:30 +00:00
|
|
|
if not to_get:
|
|
|
|
return
|
2022-09-05 06:20:30 +00:00
|
|
|
split_threads = array_split(to_get, ceil(len(to_get) / THREADS_CONCURRENT))
|
2022-09-05 06:20:30 +00:00
|
|
|
for threads in split_threads:
|
|
|
|
await self.get_threads_content(threads)
|
2022-09-05 06:20:30 +00:00
|
|
|
await asyncio.sleep(THREADS_DELAY)
|
2022-09-05 06:20:30 +00:00
|
|
|
# await self.get_threads_content(to_get)
|
2022-09-04 18:44:25 +00:00
|
|
|
|
2022-09-05 06:20:30 +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
|
2022-09-05 06:20:30 +00:00
|
|
|
except StopIteration:
|
2022-09-05 06:20:30 +00:00
|
|
|
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
|
|
|
|
}
|
2022-09-16 16:09:49 +00:00
|
|
|
# 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)
|
2022-09-16 16:09:49 +00:00
|
|
|
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"]
|
|
|
|
|
2022-09-05 06:20:30 +00:00
|
|
|
if not all_posts:
|
|
|
|
return
|
2022-09-14 17:32:32 +00:00
|
|
|
await self.handle_posts(all_posts)
|
2022-09-04 20:29:00 +00:00
|
|
|
|
2022-09-13 21:17:46 +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"
|
2022-09-05 06:20:30 +00:00
|
|
|
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
|
|
|
|
2022-09-05 06:20:30 +00:00
|
|
|
if to_store:
|
2022-09-13 21:17:46 +00:00
|
|
|
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 = []
|
2022-09-05 06:20:30 +00:00
|
|
|
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}"
|
2022-09-16 16:09:49 +00:00
|
|
|
# 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
|