|
|
@ -125,6 +125,7 @@ async def main():
|
|
|
|
# use this event type to store our url cache
|
|
|
|
# use this event type to store our url cache
|
|
|
|
cache_event_type = 'de.lubiland.snowstorm-matrix.cache'
|
|
|
|
cache_event_type = 'de.lubiland.snowstorm-matrix.cache'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cache = {}
|
|
|
|
while True:
|
|
|
|
while True:
|
|
|
|
# do sync first to e.g. accept an admin room invite
|
|
|
|
# do sync first to e.g. accept an admin room invite
|
|
|
|
sync = await matrix.sync(timeout=30000, sync_filter=sync_filter)
|
|
|
|
sync = await matrix.sync(timeout=30000, sync_filter=sync_filter)
|
|
|
@ -136,20 +137,22 @@ async def main():
|
|
|
|
|
|
|
|
|
|
|
|
if next_update < datetime.now():
|
|
|
|
if next_update < datetime.now():
|
|
|
|
# refresh url cache
|
|
|
|
# refresh url cache
|
|
|
|
cache_state = await matrix.room_get_state_event(room_id=admin_room,
|
|
|
|
old_cache = cache
|
|
|
|
event_type=cache_event_type,
|
|
|
|
cache = {}
|
|
|
|
state_key=category)
|
|
|
|
for category in category_list:
|
|
|
|
if hasattr(cache_state, 'content') and 'url_list' in cache_state.content:
|
|
|
|
cache_state = await matrix.room_get_state_event(room_id=admin_room,
|
|
|
|
cache = cache_state.content['url_list']
|
|
|
|
event_type=cache_event_type,
|
|
|
|
else:
|
|
|
|
state_key=category)
|
|
|
|
print('cache is empty')
|
|
|
|
if hasattr(cache_state, 'content') and 'url_list' in cache_state.content:
|
|
|
|
cache = []
|
|
|
|
if not hasattr(cache, category):
|
|
|
|
|
|
|
|
cache[category] = []
|
|
|
|
|
|
|
|
cache[category] += cache_state.content['url_list']
|
|
|
|
|
|
|
|
|
|
|
|
# scrape all blog posts and process them
|
|
|
|
# scrape all blog posts and process them
|
|
|
|
blog = get_blog()
|
|
|
|
blog = get_blog()
|
|
|
|
for post in blog:
|
|
|
|
for post in blog:
|
|
|
|
# check if post url is in cache and matches our category
|
|
|
|
# check if post url is in cache and matches our category
|
|
|
|
if post['url'] not in cache and post['category'] == category:
|
|
|
|
if post['category'] in category_list and hasattr(cache, post['category']) and post['url'] not in cache[post['category']]:
|
|
|
|
# post url not found in cache
|
|
|
|
# post url not found in cache
|
|
|
|
# announce new post to matrix rooms
|
|
|
|
# announce new post to matrix rooms
|
|
|
|
print('new post: '+post['url'])
|
|
|
|
print('new post: '+post['url'])
|
|
|
@ -169,21 +172,24 @@ async def main():
|
|
|
|
content=content)
|
|
|
|
content=content)
|
|
|
|
|
|
|
|
|
|
|
|
# add url to cache
|
|
|
|
# add url to cache
|
|
|
|
cache += [post['url']]
|
|
|
|
cache[post['category']] += [post['url']]
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
# no new posts found
|
|
|
|
# no new posts found
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
# trim the cache
|
|
|
|
# cleanup cache and push it as room state
|
|
|
|
while len(cache) > 100:
|
|
|
|
for category in cache.keys():
|
|
|
|
cache.remove(cache[0])
|
|
|
|
# trim the cache
|
|
|
|
|
|
|
|
while len(cache[category]) > 100:
|
|
|
|
|
|
|
|
cache[category].remove(cache[category][0])
|
|
|
|
|
|
|
|
|
|
|
|
# set new cache event
|
|
|
|
# set new cache event
|
|
|
|
await matrix.room_put_state(room_id=admin_room,
|
|
|
|
if hasattr(old_cache, 'category') and old_cache[category] != cache[category]:
|
|
|
|
event_type=cache_event_type,
|
|
|
|
await matrix.room_put_state(room_id=admin_room,
|
|
|
|
state_key=category,
|
|
|
|
event_type=cache_event_type,
|
|
|
|
content={'url_list': cache})
|
|
|
|
state_key=category,
|
|
|
|
|
|
|
|
content={'url_list': cache[category]})
|
|
|
|
|
|
|
|
|
|
|
|
# wait between 15min and 30min to randomize scraping
|
|
|
|
# wait between 15min and 30min to randomize scraping
|
|
|
|
next_update = datetime.now() + timedelta(minutes=randrange(15, 30))
|
|
|
|
next_update = datetime.now() + timedelta(minutes=randrange(15, 30))
|
|
|
@ -202,8 +208,9 @@ print('accesstoken_file: '+environ['ACCESSTOKEN_FILE'])
|
|
|
|
admin_room = environ['ADMIN_ROOM']
|
|
|
|
admin_room = environ['ADMIN_ROOM']
|
|
|
|
print('admin_room: '+admin_room)
|
|
|
|
print('admin_room: '+admin_room)
|
|
|
|
|
|
|
|
|
|
|
|
category = environ['CATEGORY']
|
|
|
|
category_list = environ['CATEGORY'].split(',')
|
|
|
|
print('category: '+category)
|
|
|
|
print('categories:')
|
|
|
|
|
|
|
|
print(category_list)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
asyncio.run(main())
|
|
|
|
asyncio.run(main())
|
|
|
|