Write a python script that gets the top 100 highest rated light novels from anilist and then scrapes them from Zlibrary then sends it to my discord server.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To solve this task, we need to perform the following steps:
Get the top 100 highest-rated light novels from Anilist: Use the Anilist API to fetch the required data.
Scrape the novels from Zlibrary: Use the zlibrary-python package to search for and retrieve book information from Zlibrary.
Send the results to a Discord server: Use a Discord bot to send the book information to your server.
Below is the Python script that implements this:
Full Python Script
python
import requests
import asyncio
from zlibrary_python.a_sync import srape_search_query
import discord
from discord.ext import commands
# Discord bot token and channel ID
DISCORD_BOT_TOKEN = "[redacted possible key]"
DISCORD_CHANNEL_ID = [Redacted InternationalBankingAccountNumber] # Replace with your Discord channel ID
# Anilist GraphQL query to fetch the top 100 light novels
ANILIST_API_URL = "https://graphql.anilist.co"
ANILIST_QUERY = """
query {
Page(page: 1, perPage: 100) {
media(type: NOVEL, format: LIGHT_NOVEL, sort: SCORE_DESC) {
title {
romaji
}
averageScore
}
}
}
"""
# Function to get the top 100 light novels from Anilist
def get_top_light_novels():
response = requests.post(ANILIST_API_URL, json={"query": ANILIST_QUERY})
if response.status_code != 200:
raise Exception(f"Anilist API error: {response.status_code} - {response.text}")
data = response.json()
novels = [
{"title": media["title"]["romaji"], "score": media["averageScore"]}
for media in data["data"]["Page"]["media"]
]
return novels
# Function to scrape Zlibrary for a novel
async def scrape_novel_from_zlibrary(title):
results = await srape_search_query(title, fuzzy=True)
if results:
return results[0] # Return the first result
return None
# Function to send a message to Discord
async def send_to_discord(channel, message):
await channel.send(message)
# Main function to handle the workflow
async def main():
# Step 1: Get top 100 light novels from Anilist
print("Fetching top 100 light novels from Anilist...")
top_novels = get_top_light_novels()
# Step 2: Scrape Zlibrary for the novels
print("Scraping Zlibrary for the novels...")
zlibrary_results = []
for novel in top_novels:
title = novel["title"]
print(f"Searching for '{title}' on Zlibrary...")
result = await scrape_novel_from_zlibrary(title)
if result:
zlibrary_results.append({"title": title, "zlibrary": result})
else:
zlibrary_results.append({"title": title, "zlibrary": None})
# Step 3: Send the results to Discord
print("Sending results to Discord...")
intents = discord.Intents.default()
intents.messages = True
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
print(f"Logged in as {bot.user}")
channel = bot.get_channel(DISCORD_CHANNEL_ID)
if channel:
for result in zlibrary_results:
title = result["title"]
zlibrary = result["zlibrary"]
if zlibrary:
message = (
f"**{title}**\n"
f"Zlibrary URL: {zlibrary['url']}\n"
f"Download URL: {zlibrary['downloadurl']}\n"
f"Author: {zlibrary['author']}\n"
f"Publisher: {zlibrary['publisher']}\n"
f"Year: {zlibrary['year']}\n"
f"Language: {zlibrary['language']}\n"
f"Rating - Interest: {zlibrary['rating']['interest']}, "
f"Quality: {zlibrary['rating']['quality']}"
)
else:
message = f"**{title}**\nNo results found on Zlibrary."
await send_to_discord(channel, message)
await bot.close()
await bot.start(DISCORD_BOT_TOKEN)
# Run the script
if __name__ == "__main__":
asyncio.run(main())
Detailed Explanation
Fetching Top 100 Light Novels from Anilist:
The script uses the Anilist GraphQL API to fetch the top 100 light novels sorted by their average scores. The media query retrieves the titles and scores.
Scraping Zlibrary:
The zlibrary-python package is used to search Zlibrary for each novel title. The srape_search_query function is used to perform this search asynchronously. If results are found, the first result is selected; otherwise, it notes that no results were found.
Sending Information to Discord:
A Discord bot is created using the discord.py library. It logs into your Discord server using a bot token and sends the scraped data to a designated channel. If Zlibrary has results for a novel, it includes details like the Zlibrary URL, download URL, author, publisher, year, language, and ratings in the message.