174 lines
4.7 KiB
Python
Executable File
174 lines
4.7 KiB
Python
Executable File
#!/usr/bin/python3
|
|
|
|
import sys,importlib
|
|
|
|
from utils import debug,notify,warn,error,success
|
|
import opml
|
|
|
|
network = True
|
|
verbosity = 3
|
|
args = {}
|
|
arg_count = 0
|
|
output_format = "url"
|
|
output_opml = None
|
|
output_filename = None
|
|
output = None
|
|
|
|
config = None
|
|
try:
|
|
from pathlib import Path
|
|
home = str(Path.home())
|
|
local_config_path = home + "/.config/rsstube/config"
|
|
config = open(local_config_path,"r")
|
|
except FileNotFoundError:
|
|
try:
|
|
global_config_path = "/etc/rsstube/config"
|
|
config = open(global_config_path,"r")
|
|
except FileNotFoundError:
|
|
# no change
|
|
config = None
|
|
file_params = ""
|
|
if not config is None:
|
|
for line in config:
|
|
line = line.strip()
|
|
# comment lines should begin with # after stripping
|
|
if line != "" and line[0] != "#":
|
|
file_params += " " + line
|
|
from options import options
|
|
|
|
def process_args (
|
|
network,
|
|
verbosity,
|
|
args,
|
|
arg_count,
|
|
output_format,
|
|
output_filename,
|
|
network_new,
|
|
verbosity_new,
|
|
args_new,
|
|
arg_count_new,
|
|
output_format_new,
|
|
output_filename_new
|
|
):
|
|
if network_new == False:
|
|
network = network_new
|
|
if not verbosity_new is None:
|
|
verbosity = verbosity_new
|
|
for i in args_new:
|
|
args[i] = args_new[i]
|
|
arg_count = arg_count_new
|
|
if output_format_new != "":
|
|
output_format = output_format_new
|
|
if not output_filename_new is None:
|
|
output_filename = output_filename_new
|
|
return network,verbosity,args,arg_count,output_format,output_filename
|
|
|
|
# config file options
|
|
if not file_params == "":
|
|
network_new,verbosity_new,args_new,arg_count_new,output_format_new,output_filename_new = options(file_params.split())
|
|
network,verbosity,args,arg_count,output_format,output_filename = process_args(
|
|
network,
|
|
verbosity,
|
|
args,
|
|
arg_count,
|
|
output_format,
|
|
output_filename,
|
|
network_new,
|
|
verbosity_new,
|
|
args_new,
|
|
arg_count_new,
|
|
output_format_new,
|
|
output_filename_new
|
|
)
|
|
|
|
# command-line options
|
|
network_new,verbosity_new,args_new,arg_count_new,output_format_new,output_filename_new = options(sys.argv[1:])
|
|
network,verbosity,args,arg_count,output_format,output_filename = process_args(
|
|
network,
|
|
verbosity,
|
|
args,
|
|
arg_count,
|
|
output_format,
|
|
output_filename,
|
|
network_new,
|
|
verbosity_new,
|
|
args_new,
|
|
arg_count_new,
|
|
output_format_new,
|
|
output_filename_new
|
|
)
|
|
|
|
if output_format == "opml":
|
|
debug ("Formatting output as OPML.", verbosity)
|
|
output_opml = opml.Opml("rsstube feeds")
|
|
|
|
if not output_filename is None and output_filename != "":
|
|
debug ("Output will be saved in " + output_filename, verbosity)
|
|
output = open(output_filename, "w")
|
|
|
|
if len(sys.argv) == arg_count+1:
|
|
error ("Please provide one or more URL.", verbosity)
|
|
|
|
for url in sys.argv[arg_count+1:]:
|
|
from determine_site import determine_site
|
|
debug ("Attempting to determine site...", verbosity)
|
|
site = determine_site (url)
|
|
if not site is None:
|
|
debug ("Site identified as " + site, verbosity)
|
|
notify ("Trying " + site + " extractor...", verbosity)
|
|
|
|
# get appropriate extractor
|
|
extractor = importlib.import_module("extractors." + site)
|
|
feed = extractor.extract(url, None, network, verbosity, args)
|
|
if feed is None:
|
|
error ("Unable to get RSS feed for " + url, verbosity, site)
|
|
else:
|
|
if not output_opml is None:
|
|
output_opml.add_feed (feed, site + ": " + url, url)
|
|
else:
|
|
success (feed, output)
|
|
elif network:
|
|
from download_page import download
|
|
page = download (None, url, args, verbosity)
|
|
if page is None:
|
|
error ("Failed to download " + url, verbosity)
|
|
continue
|
|
|
|
# try to get feed for common software like PeerTube
|
|
debug ("Attempting to determine software from page...", verbosity)
|
|
from determine_software import determine_software
|
|
software = determine_software (page)
|
|
if not software is None:
|
|
debug ("Software identified as " + software, verbosity)
|
|
notify ("Trying " + software + " extractor...", verbosity)
|
|
extractor = importlib.import_module("extractors." + software)
|
|
feed = extractor.extract(url, page, network, verbosity, args)
|
|
if feed is None:
|
|
notify ("Unable to get RSS feed for " + url + " with " + software + " extractor", verbosity, software)
|
|
else:
|
|
if not output_opml is None:
|
|
output_opml.add_feed (feed, software + ": " + url, url)
|
|
else:
|
|
success (feed, output)
|
|
continue
|
|
|
|
# try generic extractor even if software is known
|
|
debug ("Trying generic extractor...", verbosity)
|
|
extractor = importlib.import_module("extractors.generic")
|
|
feed = extractor.extract(url, page, network, verbosity, args)
|
|
if feed is None:
|
|
error ("Unable to get RSS feed for " + url, verbosity, "generic")
|
|
else:
|
|
if not output_opml is None:
|
|
output_opml.add_feed (feed, url, url)
|
|
else:
|
|
success (feed, output)
|
|
else:
|
|
error ("Unable to get RSS feed for " + url + " without downloading page", verbosity)
|
|
|
|
if not output_opml is None:
|
|
success (output_opml.get_opml(), output)
|
|
|
|
if not output is None:
|
|
output.close()
|