-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_fic_update.py
97 lines (78 loc) · 3.04 KB
/
web_fic_update.py
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
import asyncio
import pytz
from novel_list import novels
from bs4 import BeautifulSoup
import requests
from telegram.ext import CallbackContext, JobQueue, CommandHandler
from telegram import Update
from telegram.ext import Application, MessageHandler, filters
from datetime import datetime, time, timedelta
import os
TOKEN = os.environ['TOKEN']
CHAT_ID = os.environ['CHAT_ID']
def get_fic_update():
# a list of the novel links
novel_links = {}
for novel_id in novels:
link = 'https://www.royalroad.com/fiction/{0}'.format(novel_id)
novel_links[novel_id] = link
# a list of all the responses obtained from opening the links
responses = {}
for novel_id in novel_links:
response = requests.get(novel_links[novel_id], timeout=3)
responses[novel_id] = response
# a list of the contents obtained from accessing each response
contents = {}
for novel_id in responses:
content = BeautifulSoup(responses[novel_id].content, "html.parser")
contents[novel_id] = content
# storing the latest chapter name for each novel ID
latest = {}
for novel_id in contents:
chapters = ['']
for tag in contents[novel_id].find_all(style="cursor: pointer"):
for tag_2 in tag.find('td'):
chapters.append(tag_2)
length = len(chapters)
latest[novel_id] = chapters[length-2]['href']
# reading the previous links stored in file
chap_file = open('chapter_list.txt', 'r')
previous = chap_file.read().splitlines()
chap_file.close()
i = 0
message_list = []
for i, novel_id in enumerate(latest):
try:
if previous[i] != latest[novel_id]:
text = 'https://www.royalroad.com' + latest[novel_id]
subject = 'New chapter for {0}'.format(novels[novel_id])
print(subject)
print(text)
message = subject + '\n' + text
else:
message = f'No new chapter for {novels[novel_id]} )-:'
i += 1
except:
print('No previous record of {0}. New record created'.format(novels[novel_id]))
message = f'No previous record of {novels[novel_id]}. New record created'
message_list.append(message)
# overriding the previous file to store the latest chapter links
chap_file = open('chapter_list.txt', 'w')
for novel_id in latest:
chap_file.write(latest[novel_id]+'\n')
chap_file.close()
return message_list
async def send_message_to_user_now_direct(bot):
message_list = get_fic_update()
for message in message_list:
if 'No new chapter' in message:
continue
await bot.send_message(chat_id=CHAT_ID, text=message)
async def main() -> None:
"""Run the bot."""
# Create the Application and pass it your bot's token.
application = Application.builder().token(TOKEN).build()
await send_message_to_user_now_direct(application.bot)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())