FFFRssLinkGrabber/root/app/runner_notify.py

66 lines
2.7 KiB
Python
Raw Permalink Normal View History

2023-06-06 18:24:29 +00:00
import feedparser
import lxml.html
2023-06-06 17:28:10 +00:00
from optparse import OptionParser
from configparser import ConfigParser
2023-06-06 18:24:29 +00:00
def main(config):
# Grab content from RSS Feed using link in config file passed in with -c runtime option under header [locations] value "feed"
2023-06-06 18:24:29 +00:00
feed = feedparser.parse(config.get('locations', 'feed'))
# Load existing urls in file located at path in config file under header [locations] value "output"
2023-06-07 18:57:42 +00:00
with open(config.get('locations', 'output'), 'r') as file:
lines = [line.rstrip() for line in file]
# Keep track of how many works are added to config file (Just for logging purposes)
2023-06-07 18:57:42 +00:00
newworks = 0
# Open file to store urls in using append mode. Getting ready to add new urls
with open(config.get('locations', 'output'), 'a') as f:
#input=/config/fanfiction_file #output=/output/fanfiction_file
2023-06-07 18:57:42 +00:00
#/Users/sam/Desktop/workspace/FFFRssLinkGrabber/root/config.default/fanfiction_file
# iterate through rss feed entries
2023-06-06 18:24:29 +00:00
for entry in feed.entries:
# parse summary html from rss feed entry.
parsedSummary = lxml.html.fragment_fromstring(entry.summary, create_parent='div')
# check if work is in english
if True in ["Language: English" in x for x in parsedSummary.xpath("/div//p/text()")]:
2023-06-07 18:57:42 +00:00
# remove https:// from url, (requirement by FFF which is reading urls from url store file)
if entry.link.replace('https://', '') not in lines:
# count the url as added
newworks += 1
# write url to url store file
f.write(f"{entry.link.replace('https://', '')}\n")
# Log how many new urls are added to url store file
2023-06-07 18:57:42 +00:00
print(f"Added {newworks} works out of {len(feed.entries)} works to list.")
2023-06-06 17:28:10 +00:00
if __name__ == "__main__":
# command line option parser, adds -c option for config file
2023-06-06 17:28:10 +00:00
option_parser = OptionParser(usage="usage: %prog [flags]")
option_parser.add_option(
'-c',
'--config',
action='store',
dest='config',
help='Config file for inputs. Blank config file is provided. No default. If an option is present in whatever config file is passed it, the option will overwrite whatever is passed in through command line arguments unless the option is blank. Do not put any quotation marks in the options.')
(options, args) = option_parser.parse_args()
# read config file using ConfigParser and assign to config variable
2023-06-06 17:28:10 +00:00
if options.config:
config = ConfigParser(allow_no_value=True)
config.read(options.config)
# call main function passing config file in
main(config) #run program with python3 runner_notify.py -c /path/to/config.ini