Skip to content

Instantly share code, notes, and snippets.

@Bilguun132
Created September 13, 2019 06:52
Show Gist options
  • Save Bilguun132/d8b05b8ee4c9194906cf925d4f405518 to your computer and use it in GitHub Desktop.
Save Bilguun132/d8b05b8ee4c9194906cf925d4f405518 to your computer and use it in GitHub Desktop.
wuxiaworld_updater_wuxiaworldclass
class WuxiaUpdateThread(object):
""" Threading example class
The run() method will be started and it will run in the background
until the application exits.
"""
def __init__(self, interval=UPDATE_INTERVAL):
""" Constructor
:type interval: int
:param interval: Check interval, in seconds
"""
self.interval = interval
thread = threading.Thread(target=self.run, args=())
thread.daemon = True # Daemonize thread
thread.start() # Start the execution
def run(self):
""" Method that runs forever """
while True:
new_novels = populate_novels()
for novel in new_novels:
existing_record = novels_collection.find_one(
{'name': novel['name']})
if novel['chapterUrl'] == existing_record['chapterUrl']:
print('No need to update subscribers for ', novel['name'])
else:
print('New chapter found for {}'.format(
novel))
novels_collection.find_one_and_update({'name':novel["name"]}, {'$set': {'latestChapter': novel["latestChapter"], 'chapterUrl': novel["chapterUrl"]}})
novel = novels_collection.find_one({'name':novel["name"], 'subscribers': {'$exists': True}})
if novel is not None:
print(novel)
for subscriber in novel["subscribers"]:
print("Found a subscriber: {}".format(subscriber))
bot.send_message(chat_id=subscriber,
text="*{}-{}({})*\n ".format(novel["name"], novel["latestChapter"], novel["chapterUrl"]),
parse_mode=telegram.ParseMode.MARKDOWN)
logging.info('Updating again in %s seconds.', self.interval)
time.sleep(self.interval)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment