forked from indymedia/epicyon
Move news daemon functions
parent
72bf620eb7
commit
7bcabea021
|
@ -205,8 +205,8 @@ from devices import E2EEdevicesCollection
|
||||||
from devices import E2EEvalidDevice
|
from devices import E2EEvalidDevice
|
||||||
from devices import E2EEaddDevice
|
from devices import E2EEaddDevice
|
||||||
from newswire import getRSSfromDict
|
from newswire import getRSSfromDict
|
||||||
from newswire import runNewswireWatchdog
|
from newsdaemon import runNewswireWatchdog
|
||||||
from newswire import runNewswireDaemon
|
from newsdaemon import runNewswireDaemon
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
__filename__ = "newsdaemon.py"
|
||||||
|
__author__ = "Bob Mottram"
|
||||||
|
__license__ = "AGPL3+"
|
||||||
|
__version__ = "1.1.0"
|
||||||
|
__maintainer__ = "Bob Mottram"
|
||||||
|
__email__ = "bob@freedombone.net"
|
||||||
|
__status__ = "Production"
|
||||||
|
|
||||||
|
import time
|
||||||
|
from newswire import getDictFromNewswire
|
||||||
|
|
||||||
|
|
||||||
|
def runNewswireDaemon(baseDir: str, httpd, unused: str):
|
||||||
|
"""Periodically updates RSS feeds
|
||||||
|
"""
|
||||||
|
# initial sleep to allow the system to start up
|
||||||
|
time.sleep(50)
|
||||||
|
while True:
|
||||||
|
# has the session been created yet?
|
||||||
|
if not httpd.session:
|
||||||
|
print('Newswire daemon waiting for session')
|
||||||
|
time.sleep(60)
|
||||||
|
continue
|
||||||
|
|
||||||
|
# try to update the feeds
|
||||||
|
newNewswire = None
|
||||||
|
try:
|
||||||
|
newNewswire = getDictFromNewswire(httpd.session, baseDir)
|
||||||
|
except Exception as e:
|
||||||
|
print('WARN: unable to update newswire ' + str(e))
|
||||||
|
time.sleep(120)
|
||||||
|
continue
|
||||||
|
|
||||||
|
httpd.newswire = newNewswire
|
||||||
|
print('Newswire updated')
|
||||||
|
# wait a while before the next feeds update
|
||||||
|
time.sleep(1200)
|
||||||
|
|
||||||
|
|
||||||
|
def runNewswireWatchdog(projectVersion: str, httpd) -> None:
|
||||||
|
"""This tries to keep the newswire update thread running even if it dies
|
||||||
|
"""
|
||||||
|
print('Starting newswire watchdog')
|
||||||
|
newswireOriginal = \
|
||||||
|
httpd.thrPostSchedule.clone(runNewswireDaemon)
|
||||||
|
httpd.thrNewswireDaemon.start()
|
||||||
|
while True:
|
||||||
|
time.sleep(50)
|
||||||
|
if not httpd.thrNewswireDaemon.isAlive():
|
||||||
|
httpd.thrNewswireDaemon.kill()
|
||||||
|
httpd.thrNewswireDaemon = \
|
||||||
|
newswireOriginal.clone(runNewswireDaemon)
|
||||||
|
httpd.thrNewswireDaemon.start()
|
||||||
|
print('Restarting newswire daemon...')
|
50
newswire.py
50
newswire.py
|
@ -70,6 +70,10 @@ def xml2StrToDict(xmlStr: str) -> {}:
|
||||||
continue
|
continue
|
||||||
title = rssItem.split('<title>')[1]
|
title = rssItem.split('<title>')[1]
|
||||||
title = title.split('</title>')[0]
|
title = title.split('</title>')[0]
|
||||||
|
description = ''
|
||||||
|
if '<description>' in rssItem and '</description>' in rssItem:
|
||||||
|
description = rssItem.split('<description>')[1]
|
||||||
|
description = description.split('</description>')[0]
|
||||||
link = rssItem.split('<link>')[1]
|
link = rssItem.split('<link>')[1]
|
||||||
link = link.split('</link>')[0]
|
link = link.split('</link>')[0]
|
||||||
pubDate = rssItem.split('<pubDate>')[1]
|
pubDate = rssItem.split('<pubDate>')[1]
|
||||||
|
@ -78,7 +82,7 @@ def xml2StrToDict(xmlStr: str) -> {}:
|
||||||
try:
|
try:
|
||||||
publishedDate = \
|
publishedDate = \
|
||||||
datetime.strptime(pubDate, "%a, %d %b %Y %H:%M:%S %z")
|
datetime.strptime(pubDate, "%a, %d %b %Y %H:%M:%S %z")
|
||||||
result[str(publishedDate)] = [title, link, [], '']
|
result[str(publishedDate)] = [title, link, [], '', description]
|
||||||
parsed = True
|
parsed = True
|
||||||
except BaseException:
|
except BaseException:
|
||||||
pass
|
pass
|
||||||
|
@ -316,47 +320,3 @@ def getDictFromNewswire(session, baseDir: str) -> {}:
|
||||||
# sort into chronological order, latest first
|
# sort into chronological order, latest first
|
||||||
sortedResult = OrderedDict(sorted(result.items(), reverse=True))
|
sortedResult = OrderedDict(sorted(result.items(), reverse=True))
|
||||||
return sortedResult
|
return sortedResult
|
||||||
|
|
||||||
|
|
||||||
def runNewswireDaemon(baseDir: str, httpd, unused: str):
|
|
||||||
"""Periodically updates RSS feeds
|
|
||||||
"""
|
|
||||||
# initial sleep to allow the system to start up
|
|
||||||
time.sleep(50)
|
|
||||||
while True:
|
|
||||||
# has the session been created yet?
|
|
||||||
if not httpd.session:
|
|
||||||
print('Newswire daemon waiting for session')
|
|
||||||
time.sleep(60)
|
|
||||||
continue
|
|
||||||
|
|
||||||
# try to update the feeds
|
|
||||||
newNewswire = None
|
|
||||||
try:
|
|
||||||
newNewswire = getDictFromNewswire(httpd.session, baseDir)
|
|
||||||
except Exception as e:
|
|
||||||
print('WARN: unable to update newswire ' + str(e))
|
|
||||||
time.sleep(120)
|
|
||||||
continue
|
|
||||||
|
|
||||||
httpd.newswire = newNewswire
|
|
||||||
print('Newswire updated')
|
|
||||||
# wait a while before the next feeds update
|
|
||||||
time.sleep(1200)
|
|
||||||
|
|
||||||
|
|
||||||
def runNewswireWatchdog(projectVersion: str, httpd) -> None:
|
|
||||||
"""This tries to keep the newswire update thread running even if it dies
|
|
||||||
"""
|
|
||||||
print('Starting newswire watchdog')
|
|
||||||
newswireOriginal = \
|
|
||||||
httpd.thrPostSchedule.clone(runNewswireDaemon)
|
|
||||||
httpd.thrNewswireDaemon.start()
|
|
||||||
while True:
|
|
||||||
time.sleep(50)
|
|
||||||
if not httpd.thrNewswireDaemon.isAlive():
|
|
||||||
httpd.thrNewswireDaemon.kill()
|
|
||||||
httpd.thrNewswireDaemon = \
|
|
||||||
newswireOriginal.clone(runNewswireDaemon)
|
|
||||||
httpd.thrNewswireDaemon.start()
|
|
||||||
print('Restarting newswire daemon...')
|
|
||||||
|
|
35
posts.py
35
posts.py
|
@ -1193,6 +1193,41 @@ def createBlogPost(baseDir: str,
|
||||||
return blog
|
return blog
|
||||||
|
|
||||||
|
|
||||||
|
def createNewsPost(baseDir: str,
|
||||||
|
nickname: str, domain: str, port: int, httpPrefix: str,
|
||||||
|
rssTitle: str, rssDescription: str,
|
||||||
|
attachImageFilename: str, mediaType: str,
|
||||||
|
imageDescription: str, useBlurhash: bool) -> {}:
|
||||||
|
"""Converts title and description from an rss feed into a post
|
||||||
|
"""
|
||||||
|
inReplyTo = None
|
||||||
|
inReplyToAtomUri = None
|
||||||
|
schedulePost = False
|
||||||
|
eventDate = None
|
||||||
|
eventTime = None
|
||||||
|
location = None
|
||||||
|
schedulePost = False
|
||||||
|
eventDate = None
|
||||||
|
eventTime = None
|
||||||
|
location = None
|
||||||
|
clientToServer = False
|
||||||
|
saveToFile = False
|
||||||
|
followersOnly = False
|
||||||
|
|
||||||
|
blog = \
|
||||||
|
createPublicPost(baseDir,
|
||||||
|
nickname, domain, port, httpPrefix,
|
||||||
|
rssDescription, followersOnly, saveToFile,
|
||||||
|
clientToServer,
|
||||||
|
attachImageFilename, mediaType,
|
||||||
|
imageDescription, useBlurhash,
|
||||||
|
inReplyTo, inReplyToAtomUri, rssTitle,
|
||||||
|
schedulePost,
|
||||||
|
eventDate, eventTime, location)
|
||||||
|
blog['object']['type'] = 'Article'
|
||||||
|
return blog
|
||||||
|
|
||||||
|
|
||||||
def createQuestionPost(baseDir: str,
|
def createQuestionPost(baseDir: str,
|
||||||
nickname: str, domain: str, port: int, httpPrefix: str,
|
nickname: str, domain: str, port: int, httpPrefix: str,
|
||||||
content: str, qOptions: [],
|
content: str, qOptions: [],
|
||||||
|
|
Loading…
Reference in New Issue