Beispiel #1
0
#!/usr/bin/env python3

from configReader import ConfigReader
import sys
import os, os.path
from time import time
from math import floor
import hashlib
import random
import requests

configReader = ConfigReader(name = "clientConfig.txt")
path = sys.argv[1]
keys = configReader.getKeys()
endpoint = keys['endpoint']
username = keys['username']
password = keys['password']
finalLocation = keys['finalLocation']

print("Uploading to %s" % (endpoint))
r = requests.post(endpoint, 
    auth = (username, password), 
    files = {'file': open(path, 'rb')})

print(r.status_code)

if r.status_code == 200:
    urlPath = os.path.join(finalLocation, r.text)
    print(urlPath)
    os.system("echo '%s'|pbcopy" % urlPath)
else:
Beispiel #2
0
	def GET(self,**kwargs):
		configReader = ConfigReader()
		configReader.readKeys()
		keys = configReader.getKeys()
		region = 'USA-county'
		year = '2012'
		if 'region' in kwargs:
			if self.validRegion(kwargs['region']):
				region = kwargs['region']
		elif 'region' in keys:
			if self.validRegion(keys['region']):
				region=keys['region']
		if 'year' in kwargs:
			if self.validYear(int(kwargs['year'])):
				year = kwargs['year']
		elif 'year' in keys:
			if self.validYear(int(keys['year'])):
				year = keys['year']
		areas = {}
		#Read election data
		if 'county' in region:
			files = glob(staticPath+'purple/*'+year+'.txt')
			for fileName in files:
				f = open(fileName,'r')
				areas.update(self.readReigion(f))
				f.close()
		else:
			f = open(staticPath+'purple/'+region+'2012.txt')
			areas.update(self.readReigion(f))
		f = open(staticPath+'purple/'+region+'.txt')
		coords = [{'name':'','color':None,'coords':[]}]
		reading = False
		#Read the region file
		for line in f:
			if not reading:
				#A fix for some file inconsitencies in LA
				line = line.rstrip().lower().replace(' parish','')
				if line in areas:
					coords[-1]['name']=line
					l = areas[line]
					# Baisc Red Green Blue Map
					# if (max(l)==l[0]):
					# 	coords[-1]['color']='blue'
					# elif (max(l)==l[1]):
					# 	coords[-1]['color']='red'
					# elif (max(l)==l[2]):
					# 	coords[-1]['color']='green'
					r = float(l[0])/sum(l)*255
					g = float(l[2])/sum(l)*255
					b = float(l[1])/sum(l)*255
					
					coords[-1]['color'] = '#%02X%02X%02X' % (r,g,b)					
					reading = True
			else:
				if line[0]!='\n':
					coords[-1]['coords'].append([float(item) for item in line.split() if len(line.split())==2])
				else:
					reading = False
					coords.append({'name':'','color':None,'coords':[]})
		f.close()
		#Fix there being elements that are len == 0. String parsing can be a pain.
		c = []
		for dictionary in coords:
			l = []
			for item in dictionary['coords']:
				if len(item)==2:
					l.append(item)
			if len(l)!=0:
				dictionary['coords']=l
				c.append(dictionary)
		coords = list(c)
		lowest = min([item for dictionary in coords for c in dictionary['coords'] for item in c])*-1
		#Convert them to values above 0, round them, and multiply to make it bigger
		for dictionary in coords:
			dictionary['coords'] = [[(item+lowest)*20 for item in c]for c in dictionary['coords']]
		#get the highest y coordinate
		highestY = min([c[1] for dictionary in coords for c in dictionary['coords']])
		#We need to reflect it, since we're drawing in quadrant IV
		for dictionary in coords:
			dictionary['coords'] = [[c[0], highestY-(c[1]-highestY)] for c in dictionary['coords']]
		
		#Now we're gonna move it up so you can actually see it
		#Find the newest highest y coordinate
		highestY = min([c[1] for dictionary in coords for c in dictionary['coords']])
		for dictionary in coords:
			dictionary['coords'] = [[c[0],c[1]-highestY]for c in dictionary['coords']]
		print 'sending to client'
		return json.dumps({'coords':coords})