Script to create a file containing an RSS feed based on a specific tag in writefreely.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
writefreelytag2rss/writefreelytag2rss.py

79 lines
2.0 KiB

from bs4 import BeautifulSoup
from datetime import datetime
from feedgen.feed import FeedGenerator
import requests
import configparser
import sys
if len(sys.argv) < 2:
print("Please specify a config file")
sys.exit(1)
configfile = sys.argv[1]
config = configparser.ConfigParser()
config.read(configfile)
options = config.options('CONFIG')
conf = {}
for opt in options:
conf[opt] = config.get('CONFIG', opt)
fg = FeedGenerator()
fg.id(conf['url'])
fg.title(conf['feedtitle'])
fg.subtitle(conf['description'])
#fg.link(href=selflink, rel='self' )
fg.author( {'name': conf['authorname'],'email': conf['authormail']} )
fg.link(href=conf['url'], rel='alternate' )
#fg.logo('http://ex.com/logo.jpg')
#fg.subtitle('This is a cool feed!')
#fg.language('en')
page = requests.get(conf['url'])
soup = BeautifulSoup(page.content,features="lxml")
for i in soup.find_all('article'):
title = i.find("h2", {"class": "post-title"}).find("a").contents[0]
#print(title)
link = i.find("a", href=True)['href']
print(link)
dtpublished = i.find("time", {"class": "dt-published"})["content"]
try:
date = dtpublished.split(".")[0]
tz = dtpublished.split(".")[1].split(" ")[1]
except:
date = dtpublished.split(" ")[0]+" "+dtpublished.split(" ")[1]
tz = dtpublished.split(" ")[2]
print(date)
time_in_datetime = datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
pubdate = time_in_datetime.strftime('%a, %d %b %Y %H:%M:%S')+" "+tz
content = ""
for p in i.find_all('p'):
#print(p.get_text())
content=content+p.get_text()
#print("-------",content)
fe = fg.add_entry()
fe.id(link)
fe.title(title)
fe.pubDate(pubdate)
fe.link(href=link)
fe.content(content)
atomfeed = fg.atom_str(pretty=True) # Get the ATOM feed as string
rssfeed = fg.rss_str(pretty=True) # Get the RSS feed as string
# fg.atom_file(conf['destpath'], pretty=True) # Write the ATOM feed to a file
fg.rss_file(conf['destpath'], pretty=True) # Write the RSS feed to a file