Skip to content

Commit

Permalink
update callbacks workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
safirex committed Aug 3, 2022
1 parent e7eca6f commit fe54154
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 46 deletions.
3 changes: 2 additions & 1 deletion archive_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

def dev_tests():
x = Novel('n6912eh', 'My Skills Are Too Strong to Be a Heroine')

x = x.updateObject()
print(type(x))
def check_env():
try:
os.listdir('novel_list')
Expand Down
21 changes: 20 additions & 1 deletion src/Chapters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re

from pandas import array
import requests



Expand Down Expand Up @@ -37,6 +38,21 @@ def setUrl(self) -> str:
def getUrl(self):
return self.url

def processChapter(self,headers):
chapter_rep = requests.get(self.getUrl(), headers=headers)
chapter_rep.encoding = 'utf-8'
chapter_html = chapter_rep.text
self.setTitle(self.parseTitle(chapter_html))
self.setContent(self.parseContent(chapter_html))


def parseTitle(self,html) -> str:
pass

def parseContent(self,html):
pass


def validateTitle(self,title):
rstr = r"[\/\\\:\*\?\"\<\>\|]"
new_title = re.sub(rstr, "_", title)
Expand All @@ -57,6 +73,9 @@ def cleanText(self,chapter_content):
return chapter_content


def save(self,dir):
pass

def createFile(self,dir):
print("titre"+self.title)
print(self.num)
Expand Down
84 changes: 41 additions & 43 deletions src/Downloaders.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,61 @@
# coding: utf-8
from abc import ABC
from gc import callbacks
import requests
import re
from bs4 import BeautifulSoup

from src.Chapters import *

# packages for callbacks
import callback
from inspect import signature
from enum import Enum, auto

class Callbacks(Enum):
chapterListFetched = auto(),
ChapterBeginUpdate = auto(),
NovelBeginUpdate = auto(),

class NovelCallbacks():
# TODO: replace current callback list to 2DList {'enum',[funcs]}
class callbacks(Enum):
chapterListFetched = auto(),
ChapterBeginUpdate = auto(),
NovelBeginUpdate = auto(),

def __init__(self):
self.callbacks_chapterListFetched = []
self.callbacks_ChapterBeginUpdate = []
self.callbacks_NovelBeginUpdate = []

def registerCallback(self, callback_List, callback):
callback_List.append(callback)

def exec_callbacks(self, callback_list, args=None):
for method in callback_list:
"""gives to subclass a"""
def __init__(self, enum: Enum = Callbacks):
self.enum = enum
self.callbacks_dict = dict()
self.init_callback_list()

def init_callback_list(self):
for enum in self.enum:
self.callbacks_dict[enum] = []

def registerCallback(self, hook: Enum, callback):
self.callbacks_dict.get(hook).append(callback)

def exec_callbacks(self,hook: Enum ,args=None):
for method in self.callbacks_dict[hook]:
sig = signature(method)
params = sig.parameters
if (len(params) <= 1):
method()
else:
method(args)

def onChapterListFetched(self, args=None):
self.exec_callbacks(self.callbacks_chapterListFetched, args)

def onChapterBeginUpdate(self, args=None):
self.exec_callbacks(self.callbacks_ChapterBeginUpdate, args)

def onBeginNovelUpdate(self, args=None):
self.exec_callbacks(self.callbacks_NovelBeginUpdate, args)


class Novel(NovelCallbacks):
def __init__(self, codeNovel, titreNovel, keep_text_format=False):
super(Novel, self).__init__()
self.code = codeNovel
self.titre = titreNovel
self.keep_text_format = keep_text_format

def tempFunc(self, args):
self.init_callbacks()
# should be used to return a
if(type(self)==Novel):
self.updateObject()


def init_callbacks(self):
"""create a Novel basic callback"""
self.registerCallback(Callbacks.ChapterBeginUpdate,self.tempFunc)

def tempFunc(self):
print("callback works")

def downloadNovel(self, chapter) -> str:
Expand Down Expand Up @@ -187,11 +188,11 @@ def processNovel(self):

class SyosetuNovel(Novel):
def __init__(self, Novel):
super(SyosetuNovel, self).__init__(Novel.code, Novel.titre, Novel.keep_text_format)
self.site = 'https://ncode.syosetu.com/'
self.setUrl(self.site + self.code + "/")
self.headers = {
"user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"}
super(SyosetuNovel, self).__init__(Novel.code, Novel.titre, Novel.keep_text_format)

def updatePerDate(self, html):
"""check every local file is the same version as online """
Expand Down Expand Up @@ -240,14 +241,15 @@ def parseOnlineChapterList(self, html='') -> list:
return online_chap_list

def fetchTOCPage(self):
if html == '':
url = self.url
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
}
rep = requests.get(url, headers=headers)
rep.encoding = 'utf-8'
html = rep.text
url = self.url
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
}
rep = requests.get(url, headers=headers)
rep.encoding = 'utf-8'
html = rep.text
self.html = html
return html

def parseTocResume(self, html=''):
soup = BeautifulSoup(html, 'html.parser')
Expand All @@ -263,11 +265,7 @@ def parseTocResume(self, html=''):

def processChapter(self, chapter_num):
chapter = SyosetuChapter(self.code, chapter_num)
chapter_rep = requests.get(chapter.getUrl(), headers=self.headers)
chapter_rep.encoding = 'utf-8'
chapter_html = chapter_rep.text
chapter.getTitle(chapter_html)
chapter.getContent(chapter_html)
chapter.processChapter(self.headers)
return chapter
# self.createFile(i,chapter_title,chapter_content)
# self.setLastChapter(i)
Expand Down
4 changes: 3 additions & 1 deletion src/main_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

from typing import List

from src.Downloaders import *

def archiveUpdate(dirList=[],keep_text_format=False):
Expand Down Expand Up @@ -91,7 +93,7 @@ def archiveFullUpdate(dirList=[],force=False):



def getInputFile() -> list[str]:
def getInputFile() -> List[str]:
"""return code and novel name from input.txt"""
inputfile=open('input.txt','r+', encoding='utf-8')
line=inputfile.readline()
Expand Down

0 comments on commit fe54154

Please sign in to comment.