Skip to content

Instantly share code, notes, and snippets.

@jeffehobbs
Created November 21, 2016 19:05
Show Gist options
  • Select an option

  • Save jeffehobbs/8da85631f2f58405a1fb451f85fe538c to your computer and use it in GitHub Desktop.

Select an option

Save jeffehobbs/8da85631f2f58405a1fb451f85fe538c to your computer and use it in GitHub Desktop.
Script to batch check XML feeds via text file input
#!/usr/bin/env python
# d2p-check.py // jhobbs@advance.net // Oct 2016
import requests, feedparser, arrow, argparse
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", help="input text file containing feed URLS", default="feeds.txt")
parser.add_argument("-f", "--full", help="check feed content", action="store_true")
ARGS = parser.parse_args()
with open (ARGS.input, "r") as myfile:
DATA=myfile.readlines()
SUCCESS = 0
WARNING = 0
FAILURE = 0
REPORT = []
def checkFeeds(DATA):
global SUCCESS
global WARNING
global FAILURE
global REPORT
global ARGS
for feed in DATA:
feedStripped = feed.replace('\n', '').replace('\r', '')
if not feedStripped.strip():
print bcolors.BOLD + feedStripped + bcolors.ENDC
elif (feedStripped[0] == "#" ):
print bcolors.BOLD + feedStripped + bcolors.ENDC
else:
try:
r = requests.head(feedStripped)
if r.status_code == 200:
print(bcolors.OKGREEN + str(r.status_code) + ": " + str(feedStripped) + bcolors.ENDC)
SUCCESS = SUCCESS + 1
if ARGS.full:
parseFeed(feed)
elif r.status_code == 302:
print(bcolors.WARNING + str(r.status_code) + ": " + str(feedStripped) + bcolors.ENDC)
WARNING = WARNING + 1
if ARGS.full:
parseFeed(feed)
else:
print(bcolors.FAIL + str(r.status_code) + ": " + str(feedStripped) + bcolors.ENDC)
FAILURE = FAILURE + 1
REPORT.append(str(r.status_code) + ": " + str(feedStripped) + "\n")
if ARGS.full:
print " No metadata available."
except requests.ConnectionError:
print(bcolors.FAIL + "CONNECTION ERROR: " + str(feedStripped) + bcolors.ENDC)
def parseFeed(feed):
d = feedparser.parse(feed)
try:
try:
title = d['feed']['title']
updated = d['feed']['updated']
pubdate = arrow.get(updated)
print " " + title + ": updated " + pubdate.humanize()
except:
title = "HSSN Feed"
updated = d.entries[0].published
print " " + title + ": updated " + updated
except:
print " Feed parsing error."
def showResults():
print "\n---FAILURE---\n"
finalList = ''.join(REPORT)
print finalList
print "\n---RESULTS---\n"
print(bcolors.OKGREEN + "SUCCESS: " + str(SUCCESS)+ bcolors.ENDC)
print(bcolors.WARNING + "WARNING: " + str(WARNING)+ bcolors.ENDC)
print(bcolors.FAIL + "FAILURE: " + str(FAILURE)+ bcolors.ENDC)
print "\n-----FIN-----\n"
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
if __name__ == "__main__":
#clear terminal
print(chr(27) + "[2J")
checkFeeds(DATA)
showResults()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment