-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmana.py
More file actions
108 lines (83 loc) · 2.94 KB
/
mana.py
File metadata and controls
108 lines (83 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#-*- coding: utf-8 -*-
# Requires:
# python-telegram-bot
import os
import time
import datetime
import re
import json
import urllib.parse
from requests import get
from datetime import datetime, timedelta
from telegram.ext import Updater
from telegram.bot import Bot
def curdir(newDir):
return os.path.join(os.path.dirname(os.path.abspath(__file__)), newDir)
class Telegram:
api_key = ""
updater = None
bot = None
def registerKey(self, path):
api_key_dir = curdir(path)
try:
f = open(api_key_dir, 'r')
self.api_key = f.readline()
self.updater = Updater(token=self.api_key)
self.bot = Bot(token=self.api_key)
except FileNotFoundError:
print("Telegram API key does not exists.")
quit()
def isValid(self):
return (len(self.api_key) > 0)
def checkValid(self):
print("Telegram Object is " + ("Valid!" if self.isValid() else "Invalid..?"))
def sendMessage(self, message):
self.bot.send_message(chat_id='538074142', text=message) #yeah it's my id buzz off
class Mana():
name = ""
url = ""
def __init__(self, name, url):
self.name = name
self.url = url
def check():
pass
def getLastest():
pass
urlRegex = re.compile("<a\s+[A-z=\"_\s]*href=\"([\d\w:/.가-힝,\s]+)\">([\d\w:/.가-힝,\s]+)<\/a>?")
dontSend = False
if __name__ == "__main__":
mana_olddatabase = {}
mana_newdatabase = {}
mana_list = {
}
tele = Telegram()
tele.registerKey('telegram_key') #open telegram_key on script's path
tele.checkValid()
jsonFile = curdir('manga_aware.json')
with open(jsonFile) as input:
mana_olddatabase = json.load(input)
count = 0
telegramPayload = "새로운 만화를 찾았습니다.\n"
print("Checking latest mana")
for key, mana in mana_list.items():
request = get(mana.url)
text = request.text
startIndex = text.find("<div class=\"col-sm-12\">")
endIndex = text.find("<div class=\"latest-news-wrapper\">")
results = re.findall(urlRegex, text[startIndex:endIndex])
resultCount = len(results)
print(f"Found {resultCount} of {mana.name}")
mana_newdatabase[key] = {}
for value in results:
name = value[1]
url = value[0]
mana_newdatabase[key][url] = name
if ((not key in mana_olddatabase) or (not url in mana_olddatabase[key])):
telegramPayload += f"새로운 '{name}' 만화: {urllib.parse.quote(url, safe=':/')}\n"
count += 1
telegramPayload += f"\n... 이상 {count}개의 새로운 만화였습니다."
print(f"Found {count} of new mangas.")
if (count > 0 and not dontSend):
tele.sendMessage(telegramPayload) #yeah it's my id buzz off
with open(jsonFile, 'w') as output:
json.dump(mana_newdatabase, output)