Commit 69817e77 authored by Prateep Bandharangshi's avatar Prateep Bandharangshi
Browse files

Initial commit

parent 1838aac1
[twitter]
consumer_key = foo
consumer_secret = foo
api_key = foo
api_secret = foo
hashtags = whaleshark
twitter_status = The whale shark SHARK_NAME just moved to LAT, LON. Check it out at http://www.whalesharkwatch.org/ %(hashtags)s
[argos]
argos_username = foo
argos_password = foo
program_id = 9999
platforms = 1234, 5678, 9876
import httplib
import re
import xml.dom.minidom
import xml.etree.ElementTree as ET
ARGOS_HOST = "ws-argos.clsamerica.com"
def argosRequest(request):
conn = httplib.HTTPConnection(ARGOS_HOST)
conn.request("POST", "/argosDws/services/DixService", request)
response = conn.getresponse()
#print response.status, response.reason, response.msg
data = response.read()
#print data
conn.close()
return data
def cleanupXml(data):
body = re.search("<return>(.*)</return>", data, flags=re.S)
if (body):
body = body.group(1)
body = re.sub("&lt;", "<", body)
body = re.sub("&gt;", ">", body)
body = re.sub("&amp;", "&", body)
ugly_xml = xml.dom.minidom.parseString(body)
ugly_xml = ugly_xml.toprettyxml(indent=' ')
text_re = re.compile('>\n\s+([^<>\s].*?)\n\s+</', re.DOTALL)
pretty_xml = text_re.sub('>\g<1></', ugly_xml)
return pretty_xml
def cleanupCsv(data):
body = re.search("<return>(.*)</return>", data, flags=re.S)
if (body):
body = body.group(1)
return body
def getPlatforms(username, password, programNumber):
argosXml = getXml(username, password, programNumber, type="program", nbPassByPtt=1)
root = ET.fromstring(argosXml)
platformIds = []
for platform in root.findall(".//platform"):
#ET.dump(platform)
platformIds.append(int(platform.find("platformId").text))
return platformIds
def getCsv(username, password, id, type="platform", nbPassByPtt=10, nbDaysFromNow=10, mostRecentPassages="true", displaySensor="false"):
if (type == "program"):
type = "<typ:programNumber>" + str(id) + "</typ:programNumber>"
else:
type = "<typ:platformId>" + str(id) + "</typ:platformId>"
request = (
'<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:typ="http://service.dataxmldistribution.argos.cls.fr/types">\n'
'<soap:Header/>\n'
'<soap:Body>\n'
'<typ:csvRequest>\n'
'<typ:username>%s</typ:username>\n'
'<typ:password>%s</typ:password>\n'
'%s\n'
'<typ:nbPassByPtt>%d</typ:nbPassByPtt>\n'
'<typ:nbDaysFromNow>%d</typ:nbDaysFromNow>\n'
'<typ:mostRecentPassages>%s</typ:mostRecentPassages>\n'
'<typ:displayLocation>true</typ:displayLocation>\n'
'<typ:displayRawData>false</typ:displayRawData>\n'
'<typ:displaySensor>%s</typ:displaySensor>\n'
'<typ:showHeader>true</typ:showHeader>\n'
'</typ:csvRequest>\n'
'</soap:Body>\n'
'</soap:Envelope>'
) % (username, password, type, nbPassByPtt, nbDaysFromNow, mostRecentPassages, displaySensor)
#print request
data = argosRequest(request)
data = cleanupCsv(data)
return data
def getKml(username, password, id, type="platform", nbPassByPtt=10, nbDaysFromNow=10, mostRecentPassages="true"):
if (type == "program"):
type = "<typ:programNumber>" + str(id) + "</typ:programNumber>"
else:
type = "<typ:platformId>" + str(id) + "</typ:platformId>"
request = (
'<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:typ="http://service.dataxmldistribution.argos.cls.fr/types">\n'
'<soap:Header/>\n'
'<soap:Body>\n'
'<typ:kmlRequest>\n'
'<typ:username>%s</typ:username>\n'
'<typ:password>%s</typ:password>\n'
'%s\n'
'<typ:nbPassByPtt>%d</typ:nbPassByPtt>\n'
'<typ:nbDaysFromNow>%d</typ:nbDaysFromNow>\n'
'<typ:mostRecentPassages>%s</typ:mostRecentPassages>\n'
'<typ:displayDescription>true</typ:displayDescription>\n'
'</typ:kmlRequest>\n'
'</soap:Body>\n'
'</soap:Envelope>'
) % (username, password, type, nbPassByPtt, nbDaysFromNow, mostRecentPassages)
#print request
data = argosRequest(request)
data = cleanupXml(data)
return data
def getXml(username, password, id, type="platform", nbPassByPtt=10, nbDaysFromNow=10, mostRecentPassages="true", displaySensor="false"):
if (type == "program"):
type = "<typ:programNumber>" + str(id) + "</typ:programNumber>"
else:
type = "<typ:platformId>" + str(id) + "</typ:platformId>"
request = (
'<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:typ="http://service.dataxmldistribution.argos.cls.fr/types">\n'
'<soap:Header/>\n'
'<soap:Body>\n'
'<typ:xmlRequest>\n'
'<typ:username>%s</typ:username>\n'
'<typ:password>%s</typ:password>\n'
'%s\n'
'<typ:nbPassByPtt>%d</typ:nbPassByPtt>\n'
'<typ:nbDaysFromNow>%d</typ:nbDaysFromNow>\n'
'<typ:mostRecentPassages>%s</typ:mostRecentPassages>\n'
'<typ:displayLocation>true</typ:displayLocation>\n'
'<typ:displayRawData>false</typ:displayRawData>\n'
'<typ:displaySensor>%s</typ:displaySensor>\n'
'</typ:xmlRequest>\n'
'</soap:Body>\n'
'</soap:Envelope>'
) % (username, password, type, nbPassByPtt, nbDaysFromNow, mostRecentPassages, displaySensor)
#print request
data = argosRequest(request)
data = cleanupXml(data)
return data
def getLocations(argos_xml):
root = ET.fromstring(argos_xml)
locations = []
for location in root.findall(".//location"):
#ET.dump(location)
location_date = location.find("locationDate").text
latitude = float(location.find("latitude").text)
longitude = float(location.find("longitude").text)
current_location = [location_date, latitude, longitude]
locations.append(current_location)
if (len(locations) == 0):
return None
else:
return locations
def get_current_location(username, password, platform_id):
argos_xml = getXml(username, password, platform_id)
locations = getLocations(argos_xml)
current_loc = sorted(locations).pop()
return current_loc
# new stuff
#!/usr/bin/python
#
# Example code derived from:
#
# https://dev.twitter.com/docs/auth/oauth/single-user-with-examples#python
# https://github.com/brosner/python-oauth2
#
import argos
import oauth2 as oauth
import json
import urllib
import ConfigParser
from os.path import expanduser
import argparse
def oauth_req(url, key, secret, http_method="GET", post_body=None, http_headers=None):
consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
token = oauth.Token(key=key, secret=secret)
client = oauth.Client(consumer, token)
resp, content = client.request(
url,
method=http_method,
body=post_body,
headers=http_headers,
force_auth_header=True
)
return content
def update_status(status, api_key, api_secret):
response_body = oauth_req(
'https://api.twitter.com/1.1/statuses/update.json',
api_key,
api_secret,
"POST",
status
)
def encode_status(status, lat, lon):
twitter_update = { 'status' : status, 'lat' : lat, 'long' : lon, 'display_coordinates' : 'true' }
return urllib.urlencode(twitter_update)
def get_last_location(platform_id):
location = []
date = None
lat = None
lon = None
section_name = "platform_" + str(platform_id)
if CONFIG.has_option(section_name, "date"):
date = CONFIG.get(section_name, "date")
if CONFIG.has_option(section_name, "lat"):
lat = CONFIG.getfloat(section_name, "lat")
if CONFIG.has_option(section_name, "lon"):
lon = CONFIG.getfloat(section_name, "lon")
location = [ date, lat, lon ]
return location
def get_platform_name(platform_id):
section_name = "platform_" + str(platform_id)
name = CONFIG.get(section_name, "name")
return name
def save_location(platform_id, date, lat, lon):
section_name = "platform_" + str(platform_id)
if not CONFIG.has_section(section_name):
CONFIG.add_section(section_name)
CONFIG.set(section_name, "date", date)
CONFIG.set(section_name, "lat", lat)
CONFIG.set(section_name, "lon", lon)
with open(CONFIG_FILE, 'wb') as configfile:
CONFIG.write(configfile)
def get_platforms():
argos_platforms = map(int, CONFIG.get("argos", "platforms").split(","))
return argos_platforms
# --------------------------------------------------------------------------#
HOME = expanduser("~")
CONFIG_FILE=HOME + "/etc/wsw.conf"
CONFIG = ConfigParser.ConfigParser()
CONFIG.read(CONFIG_FILE)
CONSUMER_KEY = CONFIG.get("twitter", "consumer_key")
CONSUMER_SECRET = CONFIG.get("twitter", "consumer_secret")
API_KEY = CONFIG.get("twitter", "api_key")
API_SECRET = CONFIG.get("twitter", "api_secret")
argos_username = CONFIG.get("argos", "argos_username")
argos_password = CONFIG.get("argos", "argos_password")
parser = argparse.ArgumentParser()
parser.add_argument('platform_id', type=int)
args = parser.parse_args()
platform_id = args.platform_id
platform_name = get_platform_name(platform_id)
last_loc = get_last_location(platform_id)
current_loc = argos.get_current_location(argos_username, argos_password, platform_id)
current_lat = current_loc[1]
current_lon = current_loc[2]
section_name = "platform_" + str(platform_id)
twitter_status = CONFIG.get(section_name, "twitter_status")
twitter_request = encode_status (twitter_status, current_lat, current_lon)
if (cmp(last_loc, current_loc) == 0):
print "[*] " + platform_name + " (" + str(platform_id) + "):\tno update"
else:
print "[*] " + platform_name + " (" + str(platform_id) + "):\tlocation has changed"
save_location(platform_id, current_loc[0], current_loc[1], current_loc[2])
print "[+] last loc:\t" + str(last_loc)
print "[+] current loc:\t" + str(current_loc)
print twitter_status
update_status (twitter_request, API_KEY, API_SECRET)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment