mirror of https://gitlab.com/bashrc2/epicyon
				
				
				
			Convert rss feed items to activitypub posts
							parent
							
								
									7bcabea021
								
							
						
					
					
						commit
						6208a3f00f
					
				| 
						 | 
				
			
			@ -11534,7 +11534,9 @@ def runDaemon(newsInstance: bool,
 | 
			
		|||
    print('Creating newswire thread')
 | 
			
		||||
    httpd.thrNewswireDaemon = \
 | 
			
		||||
        threadWithTrace(target=runNewswireDaemon,
 | 
			
		||||
                        args=(baseDir, httpd, 'newswire'), daemon=True)
 | 
			
		||||
                        args=(baseDir, httpd,
 | 
			
		||||
                              httpPrefix, domain, port,
 | 
			
		||||
                              httpd.translate), daemon=True)
 | 
			
		||||
 | 
			
		||||
    # flags used when restarting the inbox queue
 | 
			
		||||
    httpd.restartInboxQueueInProgress = False
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,11 +6,76 @@ __maintainer__ = "Bob Mottram"
 | 
			
		|||
__email__ = "bob@freedombone.net"
 | 
			
		||||
__status__ = "Production"
 | 
			
		||||
 | 
			
		||||
import os
 | 
			
		||||
import time
 | 
			
		||||
from newswire import getDictFromNewswire
 | 
			
		||||
from posts import createNewsPost
 | 
			
		||||
from utils import saveJson
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def runNewswireDaemon(baseDir: str, httpd, unused: str):
 | 
			
		||||
def updateFeedsIndex(baseDir: str, filename: str) -> None:
 | 
			
		||||
    """Updates the index used for imported RSS feeds
 | 
			
		||||
    """
 | 
			
		||||
    indexFilename = baseDir + '/accounts/feeds.index'
 | 
			
		||||
 | 
			
		||||
    if os.path.isfile(indexFilename):
 | 
			
		||||
        if filename not in open(indexFilename).read():
 | 
			
		||||
            try:
 | 
			
		||||
                with open(indexFilename, 'r+') as feedsFile:
 | 
			
		||||
                    content = feedsFile.read()
 | 
			
		||||
                    feedsFile.seek(0, 0)
 | 
			
		||||
                    feedsFile.write(filename + '\n' + content)
 | 
			
		||||
                    print('DEBUG: feeds post added to index')
 | 
			
		||||
            except Exception as e:
 | 
			
		||||
                print('WARN: Failed to write entry to feeds posts index ' +
 | 
			
		||||
                      indexFilename + ' ' + str(e))
 | 
			
		||||
    else:
 | 
			
		||||
        feedsFile = open(indexFilename, 'w+')
 | 
			
		||||
        if feedsFile:
 | 
			
		||||
            feedsFile.write(filename + '\n')
 | 
			
		||||
            feedsFile.close()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def convertRSStoActivityPub(baseDir: str, httpPrefix: str,
 | 
			
		||||
                            domain: str, port: int,
 | 
			
		||||
                            newswire: {},
 | 
			
		||||
                            translate: {}) -> None:
 | 
			
		||||
    """Converts rss items in a newswire into posts
 | 
			
		||||
    """
 | 
			
		||||
    basePath = baseDir + '/accounts/feeds'
 | 
			
		||||
    if not os.path.isdir(basePath):
 | 
			
		||||
        os.mkdir(basePath)
 | 
			
		||||
 | 
			
		||||
    nickname = 'feeds'
 | 
			
		||||
 | 
			
		||||
    for dateStr, item in newswire.items():
 | 
			
		||||
        dateStr = dateStr.replace(' ', 'T')
 | 
			
		||||
        dateStr = dateStr.replace('+00:00', 'Z')
 | 
			
		||||
 | 
			
		||||
        filename = basePath + '/' + dateStr + '.json'
 | 
			
		||||
        if os.path.isfile(filename):
 | 
			
		||||
            continue
 | 
			
		||||
 | 
			
		||||
        rssTitle = item[0]
 | 
			
		||||
        url = item[1]
 | 
			
		||||
        rssDescription = item[4]
 | 
			
		||||
        if rssDescription:
 | 
			
		||||
            rssDescription += \
 | 
			
		||||
                '\n\n' + translate['Read more...'] + '\n' + url
 | 
			
		||||
        else:
 | 
			
		||||
            rssDescription = url
 | 
			
		||||
        blog = createNewsPost(baseDir,
 | 
			
		||||
                              nickname, domain, port,
 | 
			
		||||
                              httpPrefix, dateStr,
 | 
			
		||||
                              rssTitle, rssDescription,
 | 
			
		||||
                              None, None, None, False)
 | 
			
		||||
        if saveJson(blog, filename):
 | 
			
		||||
            updateFeedsIndex(baseDir, filename)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def runNewswireDaemon(baseDir: str, httpd,
 | 
			
		||||
                      httpPrefix: str, domain: str, port: int,
 | 
			
		||||
                      translate: {}) -> None:
 | 
			
		||||
    """Periodically updates RSS feeds
 | 
			
		||||
    """
 | 
			
		||||
    # initial sleep to allow the system to start up
 | 
			
		||||
| 
						 | 
				
			
			@ -33,6 +98,12 @@ def runNewswireDaemon(baseDir: str, httpd, unused: str):
 | 
			
		|||
 | 
			
		||||
        httpd.newswire = newNewswire
 | 
			
		||||
        print('Newswire updated')
 | 
			
		||||
 | 
			
		||||
        convertRSStoActivityPub(baseDir,
 | 
			
		||||
                                httpPrefix, domain, port,
 | 
			
		||||
                                newNewswire, translate)
 | 
			
		||||
        print('Newswire feed converted to ActivityPub')
 | 
			
		||||
 | 
			
		||||
        # wait a while before the next feeds update
 | 
			
		||||
        time.sleep(1200)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,7 +7,6 @@ __email__ = "bob@freedombone.net"
 | 
			
		|||
__status__ = "Production"
 | 
			
		||||
 | 
			
		||||
import os
 | 
			
		||||
import time
 | 
			
		||||
import requests
 | 
			
		||||
from socket import error as SocketError
 | 
			
		||||
import errno
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										3
									
								
								posts.py
								
								
								
								
							
							
						
						
									
										3
									
								
								posts.py
								
								
								
								
							| 
						 | 
				
			
			@ -1195,7 +1195,7 @@ def createBlogPost(baseDir: str,
 | 
			
		|||
 | 
			
		||||
def createNewsPost(baseDir: str,
 | 
			
		||||
                   nickname: str, domain: str, port: int, httpPrefix: str,
 | 
			
		||||
                   rssTitle: str, rssDescription: str,
 | 
			
		||||
                   published: str, rssTitle: str, rssDescription: str,
 | 
			
		||||
                   attachImageFilename: str, mediaType: str,
 | 
			
		||||
                   imageDescription: str, useBlurhash: bool) -> {}:
 | 
			
		||||
    """Converts title and description from an rss feed into a post
 | 
			
		||||
| 
						 | 
				
			
			@ -1225,6 +1225,7 @@ def createNewsPost(baseDir: str,
 | 
			
		|||
                         schedulePost,
 | 
			
		||||
                         eventDate, eventTime, location)
 | 
			
		||||
    blog['object']['type'] = 'Article'
 | 
			
		||||
    blog['object']['published'] = published
 | 
			
		||||
    return blog
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -304,5 +304,6 @@
 | 
			
		|||
    "Vote": "تصويت",
 | 
			
		||||
    "Remove Vote": "إزالة التصويت",
 | 
			
		||||
    "This is a news instance": "هذا مثال أخبار",
 | 
			
		||||
    "News": "أخبار"
 | 
			
		||||
    "News": "أخبار",
 | 
			
		||||
    "Read more...": ""
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -304,5 +304,6 @@
 | 
			
		|||
    "Vote": "Notícies",
 | 
			
		||||
    "Remove Vote": "Elimina el vot",
 | 
			
		||||
    "This is a news instance": "Aquesta és una instància de notícies",
 | 
			
		||||
    "News": "Notícies"
 | 
			
		||||
    "News": "Notícies",
 | 
			
		||||
    "Read more...": ""
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -304,5 +304,6 @@
 | 
			
		|||
    "Vote": "Newyddion",
 | 
			
		||||
    "Remove Vote": "Tynnwch y Bleidlais",
 | 
			
		||||
    "This is a news instance": "Dyma enghraifft newyddion",
 | 
			
		||||
    "News": "Newyddion"
 | 
			
		||||
    "News": "Newyddion",
 | 
			
		||||
    "Read more...": ""
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -304,5 +304,6 @@
 | 
			
		|||
    "Vote": "Abstimmung",
 | 
			
		||||
    "Remove Vote": "Abstimmung entfernen",
 | 
			
		||||
    "This is a news instance": "Dies ist eine Nachrichteninstanz",
 | 
			
		||||
    "News": "Nachrichten"
 | 
			
		||||
    "News": "Nachrichten",
 | 
			
		||||
    "Read more...": ""
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -304,5 +304,6 @@
 | 
			
		|||
    "Vote": "Vote",
 | 
			
		||||
    "Remove Vote": "Remove Vote",
 | 
			
		||||
    "This is a news instance": "This is a news instance",
 | 
			
		||||
    "News": "News"
 | 
			
		||||
    "News": "News",
 | 
			
		||||
    "Read more...": "Read more..."
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -304,5 +304,6 @@
 | 
			
		|||
    "Vote": "Votar",
 | 
			
		||||
    "Remove Vote": "Eliminar voto",
 | 
			
		||||
    "This is a news instance": "Esta es una instancia de noticias",
 | 
			
		||||
    "News": "Noticias"
 | 
			
		||||
    "News": "Noticias",
 | 
			
		||||
    "Read more...": ""
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -304,5 +304,6 @@
 | 
			
		|||
    "Vote": "Voter",
 | 
			
		||||
    "Remove Vote": "Supprimer le vote",
 | 
			
		||||
    "This is a news instance": "Ceci est une instance d'actualité",
 | 
			
		||||
    "News": "Nouvelles"
 | 
			
		||||
    "News": "Nouvelles",
 | 
			
		||||
    "Read more...": ""
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -304,5 +304,6 @@
 | 
			
		|||
    "Vote": "Vóta",
 | 
			
		||||
    "Remove Vote": "Bain Vóta",
 | 
			
		||||
    "This is a news instance": "Is sampla nuachta é seo",
 | 
			
		||||
    "News": "Nuacht"
 | 
			
		||||
    "News": "Nuacht",
 | 
			
		||||
    "Read more...": ""
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -304,5 +304,6 @@
 | 
			
		|||
    "Vote": "वोट",
 | 
			
		||||
    "Remove Vote": "वोट हटा दें",
 | 
			
		||||
    "This is a news instance": "यह एक समाचार का उदाहरण है",
 | 
			
		||||
    "News": "समाचार"
 | 
			
		||||
    "News": "समाचार",
 | 
			
		||||
    "Read more...": ""
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -304,5 +304,6 @@
 | 
			
		|||
    "Vote": "Votazione",
 | 
			
		||||
    "Remove Vote": "Rimuovi voto",
 | 
			
		||||
    "This is a news instance": "Questa è un'istanza di notizie",
 | 
			
		||||
    "News": "Notizia"
 | 
			
		||||
    "News": "Notizia",
 | 
			
		||||
    "Read more...": ""
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -304,5 +304,6 @@
 | 
			
		|||
    "Vote": "投票",
 | 
			
		||||
    "Remove Vote": "投票を削除",
 | 
			
		||||
    "This is a news instance": "これはニュースインスタンスです",
 | 
			
		||||
    "News": "ニュース"
 | 
			
		||||
    "News": "ニュース",
 | 
			
		||||
    "Read more...": ""
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -300,5 +300,6 @@
 | 
			
		|||
    "Vote": "Vote",
 | 
			
		||||
    "Remove Vote": "Remove Vote",
 | 
			
		||||
    "This is a news instance": "This is a news instance",
 | 
			
		||||
    "News": "News"
 | 
			
		||||
    "News": "News",
 | 
			
		||||
    "Read more...": "Read more..."
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -304,5 +304,6 @@
 | 
			
		|||
    "Vote": "Voto",
 | 
			
		||||
    "Remove Vote": "Remover voto",
 | 
			
		||||
    "This is a news instance": "Esta é uma instância de notícias",
 | 
			
		||||
    "News": "Notícia"
 | 
			
		||||
    "News": "Notícia",
 | 
			
		||||
    "Read more...": ""
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -304,5 +304,6 @@
 | 
			
		|||
    "Vote": "Голос",
 | 
			
		||||
    "Remove Vote": "Удалить голос",
 | 
			
		||||
    "This is a news instance": "Это новостной экземпляр",
 | 
			
		||||
    "News": "Новости"
 | 
			
		||||
    "News": "Новости",
 | 
			
		||||
    "Read more...": ""
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -304,5 +304,6 @@
 | 
			
		|||
    "Vote": "投票",
 | 
			
		||||
    "Remove Vote": "删除投票",
 | 
			
		||||
    "This is a news instance": "这是一个新闻实例",
 | 
			
		||||
    "News": "新闻"
 | 
			
		||||
    "News": "新闻",
 | 
			
		||||
    "Read more...": ""
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue