Making a Manga Checker Script For Shits and Giggles

5 years ago March 16, 2019 - 09:03 am

I've been reading manga for years now and one problem I always struggled with is checking whether or not an ongoing manga series I've been reading has already updated for the month (or year depending on the artist). Back then, I had to bookmark and check them all manually one by one.

Obviously this isn't going to work in the long run. The list of manga I'll be watching out for will inevitably grow.

So after a day of banging around code, I made a script that automatically looks out for a new chapter through a certain website, manganelo.com, and opens the browser automatically when there is an update.

Here's the full script for your full copy pasting pleasure.

import webbrowser
import requests
import csv
import re
import os
import argparse
from bs4 import BeautifulSoup

def checkManga():
	mangaList = []
	if (os.path.isfile('manga_list.csv')):
		with open('manga_list.csv', mode='r') as manga_entry:
			data_reader = csv.reader(manga_entry)
			for row in data_reader:
				mangaList.append(row)

			if (mangaList != []):
				for data in mangaList:
					mangaName = data[0]
					url = data[1]
					chapterNumber = data[2]
					latest_chapter = checkData(url)
						
				if (latest_chapter != chapterNumber):
					updateManga(mangaName, url, latest_chapter)
					webbrowser.open('https://manganelo.com/chapter/'+mangaName+'/chapter_'+latest_chapter, new=2)
					print(mangaName+" has updated! Latest chapter is "+latest_chapter+".")
				else:
					print("No new updates for "+mangaName+" :(")
			else:
				print("manga_list.csv file is empty. Please add some manga to watch out for and try again.")
	else:
		print("manga_list.csv does not exist! Please add a manga and the file will be made for you.")			

def checkData(url):
	page = requests.get(url)
	soup = BeautifulSoup(page.text, "lxml")
	#Retrieves all html content under the div tag with a class 'chapter-list'
	#It then finds all the numbers in the page and retrieves the first one
	result = soup.find_all('div', class_='chapter-list')
	latest_chapter = re.findall(r'\b\d+\b', str(result))[0]
	return latest_chapter

def updateManga(mangaName, url, newChapterNumber):
	mangaList = []
	try:
		with open('manga_list.csv', mode='r') as manga_entry:
			data_reader = csv.reader(manga_entry)
			for row in data_reader:
				if (row[0] == mangaName):
					row[2] = newChapterNumber
					mangaList.append(row)
				else:
					mangaList.append(row)	
		
		if (mangaList != []):
			with open('manga_list.csv', mode='w') as manga_entry:
				manga_writer = csv.writer(manga_entry)
				for row in mangaList:
					manga_writer.writerow([row[0], row[1], row[2]])
		else:
			print("manga_list.csv is empty!")
	except:
		print("manga_list.csv does not exist. Add manga using -u or --url then the manganelo link of the manga you want.")					

def addManga(url):
	mangaName = url.split("/manga/",1)[1]
	
	page = requests.get(url)
	soup = BeautifulSoup(page.text, "lxml")
	result = soup.find_all('div', class_='chapter-list')
	latest_chapter = re.findall(r'\b\d+\b', str(result))[0]
	
	mangaAlreadyExists = False
	#Goes through the entire csv file and checks if the provided url is there.
	with open('manga_list.csv', mode='r') as manga_entry:
			data_reader = csv.reader(manga_entry)
			for row in data_reader:
				if (url in row):
					mangaAlreadyExists = True
					break

	if (mangaAlreadyExists is False):
		with open('manga_list.csv', mode='a') as manga_entry:
			manga_writer = csv.writer(manga_entry)
			manga_writer.writerow([mangaName, url, latest_chapter])		
	else:
		print("Manga is already added in the csv file! \n")		


def get_args():
	parser = argparse.ArgumentParser(description = 'Checks for any manga update using a single command (Manganelo is the only site supported for now)')
	parser.add_argument(
		'-u', '--url', help='adds a manga in the csv file', default=False)

	return parser.parse_args()

def main():
	arg = get_args()
	if (arg.url):
		addManga(arg.url)

	checkManga();

main()