Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pudquick
Last active December 17, 2015 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pudquick/5536972 to your computer and use it in GitHub Desktop.
Save pudquick/5536972 to your computer and use it in GitHub Desktop.
import re, urllib2, json, webbrowser, console, SimpleHTTPServer, SocketServer, sys
html_template = """
<html><head><title>Updates</title>
<meta name="viewport" id="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<style media="all">
::-webkit-scrollbar { width: 1px; }
html { width: 100%%; height: 100%%; margin: 0; padding: 0; }
body {
font: normal normal normal 13px/1.5 'Helvetica Neue',Arial,Helvetica,sans-serif;
-webkit-tap-highlight-color: rgba(0,0,0,0);
-webkit-font-smoothing: antialiased;
-webkit-overflow-scrolling: touch;
-webkit-text-size-adjust: none;
background: #e8e8e8;
overflow-x: hidden;
overflow-y: scroll;
position: relative;
min-height: 416px;
color: #333;
padding: 0;
margin: 0;
}
ul { margin: 0; padding: 0; }
li {
border-bottom: 1px solid #d7d7d7;
border-right: 1px solid #d7d7d7;
border-left: 1px solid #d7d7d7;
background-color: white;
line-height: 18px;
font-size: 14px;
padding: 0;
margin: 0;
}
li:first-child {
-webkit-border-top-right-radius: 6px;
-webkit-border-top-left-radius: 6px;
border-top: 1px solid #d7d7d7;
}
li:last-child {
-webkit-border-bottom-right-radius: 6px;
-webkit-border-bottom-left-radius: 6px;
}
.content {
-webkit-box-sizing: border-box;
position: relative;
padding: 10px;
}
.item-wrapper { padding: 10px 10px 14px; }
.item-content {
-webkit-box-flex: 1;
min-height: 48px;
margin: -2px 0 -4px 8px;
}
.heading-row {
display: -webkit-box;
-webkit-box-orient: horizontal;
-webkit-box-sizing: border-box;
overflow: hidden;
}
.heading-content {
overflow: hidden;
text-overflow: ellipsis;
-webkit-box-flex: 1;
color: #999;
margin-right: 5px;
}
.timestamp {
display: inline-block;
line-height: 14px;
margin-top: 2px;
font-size: 12px;
color: #ccc;
}
.heading-text {
text-overflow: ellipsis;
vertical-align: bottom;
display: inline-block;
font-weight: bold;
overflow: hidden;
color: #333;
}
.subheading-text { line-height: 14px; font-size: 12px; }
</style></head>
<body><div class="content"><ul>
%s
</ul></div></body></html>
"""
list_item_template = """
<li>
<div class="item-wrapper"><div class="item-content"><div class="heading-row">
<div class="heading-content">
<span class="heading-text">%s</span>
<span class="subheading-text">%s</span>
</div>
<div class="timestamp">%s</div>
</div>
<div class="long-text">%s</div>
</div></div>
</li>
"""
def check_java():
# Pulled from HTML with regular expression
base_url = 'http://java.com/en/download/manual.jsp?locale=en'
ver_match = re.compile(r'<strong>[ ]*Recommended Version (?P<major>[0-9]+) Update (?P<minor>[0-9]+)[ ]*</strong>')
try:
f = urllib2.urlopen(base_url)
soft_ver = ver_match.search(f.read())
f.close()
except BaseException as err:
soft_ver = None
if not soft_ver:
return ['Error - Try Again']
else:
return ['%s update %s' % (soft_ver.group('major'), soft_ver.group('minor'))]
def check_reader():
# Pulled from JSON
base_url = 'http://get.adobe.com/reader/webservices/json/standalone/?platform_type=Windows&platform_dist=Windows%207&platform_arch=&language=English&eventname=readerotherversions'
# To simulate an AJAX request
request = urllib2.Request(base_url)
request.add_header('x-requested-with', 'XMLHttpRequest')
try:
f = urllib2.urlopen(request)
soft_vers = [x[u'Version'] for x in json.loads(f.read())]
f.close()
except BaseException as err:
soft_vers = None
if not soft_vers:
return ['Error - Try Again']
else:
return soft_vers
def check_flash():
# Pulled from HTML with regular expression
base_url = 'http://www.adobe.com/software/flash/about/'
ver_match = re.compile(r'Internet Explorer ActiveX controls and plug-ins.+?\n[^\n]+<td>(?P<ver>[0-9.]+)</td>', re.MULTILINE)
try:
f = urllib2.urlopen(base_url)
soft_ver = ver_match.search(f.read())
f.close()
except BaseException as err:
soft_ver = None
if not soft_ver:
return ['Error - Try Again']
else:
return [soft_ver.group('ver')]
def check_firefox():
# Pulled from HTML with regular expression
base_url = 'http://download-origin.cdn.mozilla.net/pub/mozilla.org/firefox/releases/latest/win32/en-US/'
ver_match = re.compile(r'.exe">Firefox Setup (?P<ver>[0-9.]+).exe</a>', re.MULTILINE)
try:
f = urllib2.urlopen(base_url)
soft_ver = [ver_match.search(f.read()).group('ver')]
f.close()
except BaseException as err:
soft_ver = []
# Pulled from HTML with regular expression
base_url = 'http://download-origin.cdn.mozilla.net/pub/mozilla.org/firefox/releases/latest-esr/win32/en-US/'
ver_match = re.compile(r'.exe">Firefox Setup (?P<ver>[0-9.esr]+).exe</a>', re.MULTILINE)
try:
f = urllib2.urlopen(base_url)
soft_ver.append(ver_match.search(f.read()).group('ver'))
f.close()
except BaseException as err:
soft_ver = soft_ver
if not soft_ver:
return ['Error - Try Again']
else:
return soft_ver
software_library = [
('Oracle Java 7', check_java),
('Adobe Flash Player', check_flash),
('Adobe Acrobat Reader', check_reader),
('Mozilla Firefox', check_firefox)
]
def html_page():
html = ''
page_list = ''
html += html_template
for soft_title, soft_func in software_library:
print '...', soft_title
body = '<br>'.join(soft_func())
page_list += (list_item_template % (soft_title, ' ', ' ', body))
html = html % (page_list)
return html
class SmarterHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
server_version = "SimpleHTTP/0.6"
htmlbody = ''
def do_GET(self):
if self.path.startswith('/version'):
self.get_version()
else:
f = self.send_head()
if f:
self.copyfile(f, self.wfile)
f.close()
def get_version(self):
self.send_response(200)
self.end_headers()
self.wfile.write(self.htmlbody)
def log_message(self, format, *args):
return
class SmarterHTTPD(SocketServer.ThreadingTCPServer):
keep_running = True
requests_left = None
did_timeout = False
def serve_limited(self, timeout=None, max_requests=None):
global ready_to_stop
self.timeout = timeout
if max_requests is None:
self.requests_left = None
else:
self.requests_left = abs(int(max_requests))
self.keep_running = True
self.did_timeout = False
while self.keep_running:
self.handle_request()
if self.requests_left is not None:
self.requests_left -= 1
if self.did_timeout:
self.keep_running = False
# print "EXIT: TIMED OUT"
continue
if self.requests_left is not None:
if self.requests_left <= 0:
self.keep_running = False
# print "EXIT: HIT MAX REQUESTS"
continue
if ready_to_stop:
self.keep_running = False
# print "EXIT: TOLD TO STOP"
continue
def handle_timeout(self):
self.did_timeout = True
def release(self):
try:
self.server_close()
except Exception:
sys.exc_clear()
try:
self.socket.close()
except Exception:
sys.exc_clear()
def main():
global ready_to_stop
ready_to_stop = False
console.clear()
print 'Checking software versions ...'
the_page = html_page()
print '... Done!'
# Set up the webserver
port = 8000
handler = SmarterHTTPRequestHandler
handler.htmlbody = the_page
httpd = SmarterHTTPD(("", port), handler, False)
httpd.allow_reuse_address = True
httpd.server_bind()
httpd.server_activate()
# print "serving at port", port
webbrowser.open('http://localhost:8000/version', stop_when_done=True)
httpd.serve_limited(timeout=5,max_requests=5)
httpd.release()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment