import os
import wikipedia
LANGUAGES = {
"sah": "Sakha" }
MAX_SIZE_OF_ARTICLES = 50000
MAX_ARTICLES = 200
output_dir = "./datasets/downloads/"
os.makedirs(output_dir, exist_ok=True)
def get_size_of_all_articles(language):
path = language + "/"
if not os.path.exists(path):
return 0
return sum(os.path.getsize(path + f) for f in os.listdir(path) if os.path.isfile(path + f)) / 1024
for language in LANGUAGES:
i = 1
wikipedia.set_lang(language)
while i <= MAX_ARTICLES and get_size_of_all_articles(language) < MAX_SIZE_OF_ARTICLES:
try:
page = wikipedia.page(wikipedia.random())
except (wikipedia.DisambiguationError, wikipedia.exceptions.PageError):
continue
filename = os.path.join(output_dir, language, f"{i}-{language}.txt")
os.makedirs(os.path.dirname(filename), exist_ok=True)
try:
with open(filename, "w", encoding="utf-8") as f:
f.write(page.content) except UnicodeEncodeError:
continue
i += 1
print(f"Downloaded {i-1} articles for {language}, total size: {get_size_of_all_articles(language):.2f} KB")
print(f"Finished scraping {language}: {i-1} articles, {get_size_of_all_articles(language):.2f} KB")