Custom "getopt" as Class

Easier to use, an can now take value:
* CLI: morss key=value http://...
* HTTP: http://path/to/morss/:key=value/http://...
master
pictuga 2013-11-03 13:18:09 +01:00
parent 53c9b07d19
commit 97acdd9530
1 changed files with 50 additions and 33 deletions

View File

@ -66,33 +66,48 @@ def countWord(txt):
else: else:
return 0 return 0
def parseOptions(): class ParseOptions:
url = '' def __init__(self):
options = [] self.url = ''
self.options = {}
roptions = []
if 'REQUEST_URI' in os.environ: if 'REQUEST_URI' in os.environ:
url = os.environ['REQUEST_URI'][1:] self.url = os.environ['REQUEST_URI'][1:]
if 'REDIRECT_URL' not in os.environ: if 'REDIRECT_URL' not in os.environ:
url = url[len(os.environ['SCRIPT_NAME']):] self.url = self.url[len(os.environ['SCRIPT_NAME']):]
if url.startswith(':'): if self.url.startswith(':'):
options = url.split('/')[0].split(':')[1:] roptions = self.url.split('/')[0].split(':')[1:]
url = url.split('/', 1)[1] self.url = self.url.split('/', 1)[1]
else:
if len(sys.argv) <= 1:
return (None, [])
if urlparse.urlparse(url).scheme not in PROTOCOL: roptions = sys.argv[1:-1]
url = 'http://' + url self.url = sys.argv[-1]
else:
if len(sys.argv) <= 1:
return (None, [])
options = sys.argv[1:-1] if urlparse.urlparse(self.url).scheme not in PROTOCOL:
url = sys.argv[-1] self.url = 'http://' + self.url
if urlparse.urlparse(url).scheme not in PROTOCOL: for option in roptions:
url = 'http://' + url split = option.split('=', 1)
if len(split) > 1:
if split[0].lower() == 'true':
self.options[split[0]] = True
if split[0].lower() == 'false':
self.options[split[0]] = False
return (url, options) self.options[split[0]] = split[1]
else:
self.options[split[0]] = True
def __getattr__(self, key):
if key in self.options:
return self.options[key]
else:
return False
class Cache: class Cache:
"""Light, error-prone caching system.""" """Light, error-prone caching system."""
@ -444,7 +459,7 @@ def Gather(url, cachePath, options):
startTime = time.time() startTime = time.time()
for i, item in enumerate(rss.items): for i, item in enumerate(rss.items):
item = Fix(item, url) item = Fix(item, url)
if 'progress' in options: if options.progress:
if MAX_ITEM == 0: if MAX_ITEM == 0:
print '%s/%s' % (i+1, size) print '%s/%s' % (i+1, size)
else: else:
@ -462,10 +477,10 @@ def Gather(url, cachePath, options):
Fill(item, cache, url) Fill(item, cache, url)
if item.desc and item.content: if item.desc and item.content:
if 'clip' in options: if options.clip:
item.content = item.desc + "<br/><br/><center>* * *</center><br/><br/>" + item.content item.content = item.desc + "<br/><br/><center>* * *</center><br/><br/>" + item.content
del item.desc del item.desc
if 'keep' not in options: if not options.keep:
del item.desc del item.desc
log(len(rss.items)) log(len(rss.items))
@ -474,11 +489,13 @@ def Gather(url, cachePath, options):
return rss.tostring(xml_declaration=True, encoding='UTF-8') return rss.tostring(xml_declaration=True, encoding='UTF-8')
if __name__ == '__main__': if __name__ == '__main__':
url, options = parseOptions() options = ParseOptions()
DEBUG = 'debug' in options url = options.url
DEBUG = bool(options.debug)
if 'REQUEST_URI' in os.environ: if 'REQUEST_URI' in os.environ:
if 'HTTP_IF_NONE_MATCH' in os.environ and 'force' not in options: if 'HTTP_IF_NONE_MATCH' in os.environ and not options.force:
if time.time() - int(os.environ['HTTP_IF_NONE_MATCH'][1:-1]) < DELAY: if time.time() - int(os.environ['HTTP_IF_NONE_MATCH'][1:-1]) < DELAY:
print 'Status: 304' print 'Status: 304'
print print
@ -489,15 +506,15 @@ if __name__ == '__main__':
print 'Status: 200' print 'Status: 200'
print 'ETag: "%s"' % int(time.time()) print 'ETag: "%s"' % int(time.time())
if 'html' in options: if options.html:
print 'Content-Type: text/html' print 'Content-Type: text/html'
elif 'debug' in options: elif options.debug:
print 'Content-Type: text/plain' print 'Content-Type: text/plain'
elif 'progress' in options: elif options.progress:
print 'Content-Type: application/octet-stream' print 'Content-Type: application/octet-stream'
else: else:
print 'Content-Type: text/xml' print 'Content-Type: text/xml'
print print ''
cache = os.getcwd() + '/cache' cache = os.getcwd() + '/cache'
else: else:
@ -507,14 +524,14 @@ if __name__ == '__main__':
print 'Please provide url.' print 'Please provide url.'
sys.exit(1) sys.exit(1)
if 'progress' in options: if options.progress:
MAX_TIME = -1 MAX_TIME = -1
if 'cache' in options: if options.cache:
MAX_TIME = 0 MAX_TIME = 0
RSS = Gather(url, cache, options) RSS = Gather(url, cache, options)
if RSS is not False and 'progress' not in options and not DEBUG: if RSS is not False and not options.progress and not DEBUG:
print RSS print RSS
if RSS is False and 'progress' not in options: if RSS is False and 'progress' not in options: