import feedparser import lxml.html from optparse import OptionParser from configparser import ConfigParser def main(config): # Grab content from RSS Feed using link in config file passed in with -c runtime option under header [locations] value "feed" feed = feedparser.parse(config.get('locations', 'feed')) # Load existing urls in file located at path in config file under header [locations] value "output" 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) 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 #/Users/sam/Desktop/workspace/FFFRssLinkGrabber/root/config.default/fanfiction_file # iterate through rss feed entries 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()")]: # 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 print(f"Added {newworks} works out of {len(feed.entries)} works to list.") if __name__ == "__main__": # command line option parser, adds -c option for config file 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 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