2023-04-08 03:05:36 +00:00
|
|
|
import re
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
2023-04-11 17:01:23 +00:00
|
|
|
import bot.scraper as Scraper
|
2023-04-08 03:05:36 +00:00
|
|
|
|
2023-04-22 22:56:52 +00:00
|
|
|
from bot.entries.jitenon_kotowaza import JitenonKotowazaEntry
|
|
|
|
from bot.yomichan.export import JitenonKotowazaExporter
|
2023-04-08 03:05:36 +00:00
|
|
|
|
2023-04-22 22:56:52 +00:00
|
|
|
from bot.entries.jitenon_yoji import JitenonYojiEntry
|
|
|
|
from bot.yomichan.export import JitenonYojiExporter
|
2023-04-10 16:14:52 +00:00
|
|
|
|
|
|
|
|
2023-04-22 22:56:52 +00:00
|
|
|
class Crawler():
|
|
|
|
def __init__(self):
|
|
|
|
self.crawl_map = {}
|
|
|
|
self.entries = []
|
2023-04-10 16:14:52 +00:00
|
|
|
|
2023-04-22 22:56:52 +00:00
|
|
|
def make_entries(self):
|
|
|
|
entries_len = len(self.crawl_map)
|
|
|
|
items = self.crawl_map.items()
|
|
|
|
for idx, (entry_id, entry_path) in enumerate(items):
|
|
|
|
update = f"Reading entry {idx+1}/{entries_len}"
|
|
|
|
print(update, end='\r', flush=True)
|
|
|
|
entry = self.entry_class(entry_id)
|
|
|
|
entry.add_document(entry_path)
|
|
|
|
self.entries.append(entry)
|
|
|
|
print()
|
2023-04-10 16:14:52 +00:00
|
|
|
|
2023-04-22 22:56:52 +00:00
|
|
|
def make_yomichan_dictionary(self):
|
|
|
|
self.yomi_exporter.export(self.entries)
|
|
|
|
|
|
|
|
|
|
|
|
class JitenonYojiCrawler(Crawler):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.entry_class = JitenonYojiEntry
|
|
|
|
self.yomi_exporter = JitenonYojiExporter()
|
|
|
|
|
|
|
|
def crawl(self):
|
|
|
|
print("Scraping jitenon-yoji...")
|
|
|
|
jitenon = Scraper.Jitenon()
|
|
|
|
gojuon_doc, _ = jitenon.scrape("https://yoji.jitenon.jp/cat/gojuon.html")
|
|
|
|
gojuon_soup = BeautifulSoup(gojuon_doc, features="html.parser")
|
|
|
|
for gojuon_a in gojuon_soup.select(".kana_area a", href=True):
|
|
|
|
gojuon_href = gojuon_a['href']
|
|
|
|
kana_doc, _ = jitenon.scrape(gojuon_href)
|
|
|
|
kana_soup = BeautifulSoup(kana_doc, features="html.parser")
|
|
|
|
for kana_a in kana_soup.select(".word_box a", href=True):
|
|
|
|
kana_href = kana_a['href']
|
|
|
|
entry_id = int(re.search(r"([0-9]+).html", kana_href).group(1))
|
|
|
|
if entry_id in self.crawl_map:
|
|
|
|
continue
|
|
|
|
_, entry_path = jitenon.scrape(kana_href)
|
|
|
|
self.crawl_map[entry_id] = entry_path
|
|
|
|
entries_len = len(self.crawl_map)
|
|
|
|
print(f"Finished scraping {entries_len} entries")
|
|
|
|
|
|
|
|
|
|
|
|
class JitenonKotowazaCrawler(Crawler):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.entry_class = JitenonKotowazaEntry
|
|
|
|
self.yomi_exporter = JitenonKotowazaExporter()
|
|
|
|
|
|
|
|
def crawl(self):
|
|
|
|
print("Scraping jitenon-kotowaza...")
|
|
|
|
jitenon = Scraper.Jitenon()
|
|
|
|
gojuon_doc, _ = jitenon.scrape("https://kotowaza.jitenon.jp/cat/gojuon.php")
|
|
|
|
gojuon_soup = BeautifulSoup(gojuon_doc, features="html.parser")
|
|
|
|
for gojuon_a in gojuon_soup.select(".kana_area a", href=True):
|
|
|
|
gojuon_href = gojuon_a['href']
|
|
|
|
kana_doc, _ = jitenon.scrape(gojuon_href)
|
|
|
|
kana_soup = BeautifulSoup(kana_doc, features="html.parser")
|
|
|
|
for kana_a in kana_soup.select(".word_box a", href=True):
|
|
|
|
kana_href = kana_a['href']
|
|
|
|
m = re.search(r"([0-9]+).php", kana_href)
|
|
|
|
if not m:
|
|
|
|
continue
|
|
|
|
entry_id = int(m.group(1))
|
|
|
|
if entry_id in self.crawl_map:
|
|
|
|
continue
|
|
|
|
_, entry_path = jitenon.scrape(kana_href)
|
|
|
|
self.crawl_map[entry_id] = entry_path
|
|
|
|
entries_len = len(self.crawl_map)
|
|
|
|
print(f"Finished scraping {entries_len} entries")
|