User:Eleanorg/Thematic1.1/Ship Tracker 2 - image crossfade

From XPUB & Lens-Based wiki

Uses a regex to scrape ship's current location and convert to a %, controlling the opacity of an image laid over another image. Regex currently breaking for unknown reason.

Next:

  • Find out why regex match breaks
#!/usr/bin/python
#-*- coding:utf-8 -*-

import urllib2, re

## version 4, integrating regex longitude grab with % calculator

text = urllib2.urlopen("http://www.marinetraffic.com/ais/shipdetails.aspx?mmsi=235080274&header=true").read()
    
for m in re.findall(r"(\d+\.\d+) ?˚ ?/ ?\d*(\d+\.\d\d\d\d)\d* ?˚", text):
    longitude = str(m[1])                        # must be a string for next regex to work

print longitude


# ------------------CONVERT TO INTEGER---------------

longint = int(re.sub("\.", "", longitude)) 	 ##removes decimal point and converts back to an integer


# -------------------CONVERT TO %-------------------
	 ##converts longint into a percentage, where 12528 = 0% and 41285 = 100%
	 ##range between numbers is 28703, so 1% of range is 28703/100 ---> 287.03. Longitude equivalent to 1% will be 12528 + (28703/100)
	 ##thus to convert longitude to percentage, work out its distance from 12528 and divide this distance by 287.03.
# for testing (comment out when running for real):
#percent = 20
percent = float(int((longint - 12528) / 287.03))
         ## now move decimal point two places left and convert to a string, for use with CSS
opacity = str(percent / 100)


### ------------------USE IN HTML---------------------


print "Content-Type: text/html"
print
print """
  <html>
    <body>
       <div>
         <p>percent is: """ + opacity + """</p>

         <img src="http://farm5.staticflickr.com/4039/4552559587_d35d7942ab_b.jpg" alt="http://www.flickr.com/photos/whiteknuckled/4552559587/" style="position:absolute; opacity:""" + opacity + """;" />

         <img src="http://farm3.staticflickr.com/2665/4228521046_0c421e03f0_o.jpg" />
         
       </div>
    </body>
  </html>"""