Skip to content

Commit

Permalink
github workflow不再支持python 2, 使用2to3转了一下
Browse files Browse the repository at this point in the history
  • Loading branch information
k committed May 11, 2024
1 parent 661b727 commit ce5dab3
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 27 deletions.
54 changes: 27 additions & 27 deletions .build/validator.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env python2
#!/usr/bin/env python3

from os import sys,path,environ

environ['LANGUAGE'] = 'en'

import urllib2
import urllib.request, urllib.error, urllib.parse
# OK I know I cannot write python
sys.path.append(path.join(path.dirname(__file__), 'feedvalidator', 'src'))

Expand Down Expand Up @@ -33,26 +33,26 @@

for entry in entries:
title = entry.get('title').encode('utf-8')
print '=== Validating %s ===' % title
print('=== Validating %s ===' % title)

site = entry.get('htmlUrl')
code = -1
print "Testing HTTP connectivity...: %s" % site
print("Testing HTTP connectivity...: %s" % site)
try:
resp = urllib2.urlopen(site)
resp = urllib.request.urlopen(site)
code = resp.getcode()
except Exception as e:
print "Cannot connect to site: %s" % str(e)
print("Cannot connect to site: %s" % str(e))
siteFailed.append([title, entry])

if code >= 200 and code < 400:
# Is a valid response
print "Site successfully responded with code %s" % code
print("Site successfully responded with code %s" % code)
elif code >= 0:
print "Site responded with unexpected response code %s" % code
print("Site responded with unexpected response code %s" % code)
siteFailed.append([title, entry])

print "Fetching feeds..."
print("Fetching feeds...")
feed = entry.get('xmlUrl')

events = None
Expand All @@ -61,7 +61,7 @@
except feedvalidator.logging.ValidationFailure as vf:
events = [vf.event]
except Exception as e:
print "Unable to fetch feed: %s" % str(e)
print("Unable to fetch feed: %s" % str(e))
feedCritical.append(e)

if events is not None:
Expand All @@ -70,38 +70,38 @@

criticals = compatibility.A(events)
if len(criticals) > 0:
print "Feed failed validation with critical errors:"
print("Feed failed validation with critical errors:")
output = Formatter(criticals)
print "\n".join(output)
print("\n".join(output))
feedCritical.append([title, entry])
else:
warnings = compatibility.AAA(events)
if len(warnings) > 0:
print "Feed passed validation with warnings:"
print("Feed passed validation with warnings:")
output = Formatter(warnings)
print "\n".join(output)
print("\n".join(output))
feedWarning.append([title, entry])
else:
print "Feed passed validation with no error or warning"
print("Feed passed validation with no error or warning")

print ""
print("")

print "### SUMMARY ###"
print "In total of %s entries:" % len(entries)
print "%s entries failed the connectivity test:" % len(siteFailed)
print("### SUMMARY ###")
print("In total of %s entries:" % len(entries))
print("%s entries failed the connectivity test:" % len(siteFailed))
for [title, entry] in siteFailed:
print "\t%s: %s" % (title, entry)
print ""
print("\t%s: %s" % (title, entry))
print("")

print "%s entries failed the feed validation:" % len(feedCritical)
print("%s entries failed the feed validation:" % len(feedCritical))
for [title, entry] in feedCritical:
print "\t%s: %s" % (title, entry)
print ""
print("\t%s: %s" % (title, entry))
print("")

print "%s entries passed the feed validation with warnings:" % len(feedWarning)
print("%s entries passed the feed validation with warnings:" % len(feedWarning))
for [title, entry] in feedWarning:
print "\t%s: %s" % (title, entry)
print ""
print("\t%s: %s" % (title, entry))
print("")

if len(feedCritical) > 0:
sys.exit(1)
107 changes: 107 additions & 0 deletions .build/validator.python2.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env python2

from os import sys,path,environ

environ['LANGUAGE'] = 'en'

import urllib2
# OK I know I cannot write python
sys.path.append(path.join(path.dirname(__file__), 'feedvalidator', 'src'))

from bs4 import BeautifulSoup
import feedvalidator
import socket
socket.setdefaulttimeout(5)

with open(sys.argv[1], 'r') as opmlFile:

opml = opmlFile.read().decode('utf-8')
opml = BeautifulSoup(opml, 'xml')

entries = opml.find_all('outline')

total = len(entries)

# Ones that failed the connectivity test
siteFailed = []

# Ones that failed the feed validator test
feedCritical = []

# Ones that triggered feed validator warnings
feedWarning = []

for entry in entries:
title = entry.get('title').encode('utf-8')
print '=== Validating %s ===' % title

site = entry.get('htmlUrl')
code = -1
print "Testing HTTP connectivity...: %s" % site
try:
resp = urllib2.urlopen(site)
code = resp.getcode()
except Exception as e:
print "Cannot connect to site: %s" % str(e)
siteFailed.append([title, entry])

if code >= 200 and code < 400:
# Is a valid response
print "Site successfully responded with code %s" % code
elif code >= 0:
print "Site responded with unexpected response code %s" % code
siteFailed.append([title, entry])

print "Fetching feeds..."
feed = entry.get('xmlUrl')

events = None
try:
events = feedvalidator.validateURL(feed, firstOccurrenceOnly=1)['loggedEvents']
except feedvalidator.logging.ValidationFailure as vf:
events = [vf.event]
except Exception as e:
print "Unable to fetch feed: %s" % str(e)
feedCritical.append(e)

if events is not None:
from feedvalidator import compatibility
from feedvalidator.formatter.text_plain import Formatter

criticals = compatibility.A(events)
if len(criticals) > 0:
print "Feed failed validation with critical errors:"
output = Formatter(criticals)
print "\n".join(output)
feedCritical.append([title, entry])
else:
warnings = compatibility.AAA(events)
if len(warnings) > 0:
print "Feed passed validation with warnings:"
output = Formatter(warnings)
print "\n".join(output)
feedWarning.append([title, entry])
else:
print "Feed passed validation with no error or warning"

print ""

print "### SUMMARY ###"
print "In total of %s entries:" % len(entries)
print "%s entries failed the connectivity test:" % len(siteFailed)
for [title, entry] in siteFailed:
print "\t%s: %s" % (title, entry)
print ""

print "%s entries failed the feed validation:" % len(feedCritical)
for [title, entry] in feedCritical:
print "\t%s: %s" % (title, entry)
print ""

print "%s entries passed the feed validation with warnings:" % len(feedWarning)
for [title, entry] in feedWarning:
print "\t%s: %s" % (title, entry)
print ""

if len(feedCritical) > 0:
sys.exit(1)

0 comments on commit ce5dab3

Please sign in to comment.