Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Heaton authored and Robert Heaton committed Jul 8, 2018
0 parents commit cd5e336
Show file tree
Hide file tree
Showing 11 changed files with 643 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
output/*
vendor/*
*.pyc
.DS_Store
4 changes: 4 additions & 0 deletions config.py
@@ -0,0 +1,4 @@
ATTACKER_FB_EMAIL = "EMAIL"
ATTACKER_FB_PASSWORD = "PASSWORD :/"
TARGET_FB_EMAIL = "EMAIL"
TARGET_FB_PASSWORD = "PASSWORD :/"
23 changes: 23 additions & 0 deletions convert.py
@@ -0,0 +1,23 @@
from utils import _distance

# [200~40.59.36 73.22.36 73.21.40[201~
h1 = 73
m1 = 22
s1 = 36

h2 = 73
m2 = 21
s2 = 40

hh = 40
mm = 59
ss = 36


def conv(h, m, s):
return h + m/60.0 + s/3600.0

l1 = (conv(hh, mm, ss), conv(h1, m1, s1))
l2 = (conv(hh, mm, ss), conv(h2, m2, s2))

print _distance(l1, l2)
44 changes: 44 additions & 0 deletions fake.py
@@ -0,0 +1,44 @@
from interfaces import *
import utils

class FakeService:

def __init__(self):
self.locations = {}

def move(self, user_id, location):
self.locations[user_id] = location

def ping(self, pinger_id, pingee_id):
pinger_loc = self.location(pinger_id)
pingee_loc = self.location(pingee_id)

distance = utils._distance(pinger_loc, pingee_loc)

return round(distance)

def location(self, user_id):
loc = self.locations[user_id]
return utils._round_location(loc)

class FakeUser(User):

def __init__(self, user_id, initial_location, service):
self._user_id = user_id
self.service = service

self.move(initial_location)

def user_id(self):
return self._user_id

def move(self, co_ords):
self.service.move(self.user_id(), co_ords)

def ping(self, pingee_id):
return self.service.ping(self.user_id(), pingee_id)

def location(self):
return self.service.location(self.user_id())


54 changes: 54 additions & 0 deletions interfaces.py
@@ -0,0 +1,54 @@
from utils import *
from pprint import pprint
import random

class User:

def move(self, co_ords):
pass

def ping(self, pingee_id):
pass

class Attack:

def run(self):
pass

class AttackOutput:

def save_kml(self):
pass

def location_estimate(self):
pass

class Property:

def name(self):
pass

def description(self):
pass

def multi_evaluate(self, user1, user2, n_evals, seed=None):
if seed is None:
seed = random.randint(0, 9999)
random.seed(seed)
print("Seed = %d" % seed)
print("Running %s" % self.__class__.__name__)
results = []
for _ in range(0, n_evals):
inputs = self.generate_inputs()
pprint(inputs)
ret = self.evaluate(user1, user2, inputs)
# pprint(ret)
results.append(ret)
return results

def generate_inputs(self):
pass

def evaluate(self, user1, user2, inputs):
pass

62 changes: 62 additions & 0 deletions kml.py
@@ -0,0 +1,62 @@
import os
import sys
import simplekml
from polycircles import polycircles
import time
import hashlib

M_IN_MILE = 1609.34

parent_folder = os.path.dirname(os.path.realpath(sys.argv[0])) + "/"


def points(points, filepath=None):
kml_obj = simplekml.Kml()

for p in points:
label = p[0]
point = p[1]
p = kml_obj.newpoint(
# name = label,
coords=[(point[1], point[0])])
# TODO: this is silly
hex_color = hashlib.md5(label).hexdigest()[6:14]
p.style.iconstyle.color = simplekml.Color.hex(hex_color)

if filepath is None:
filepath = str(time.time())

kml_obj.save(parent_folder + "output/" + filepath)

def save_kml(markers, filepath=None, extra_locations={}):
if filepath is None:
filepath = str(time.time())

kml_obj = simplekml.Kml()
for marker in markers:
centre_lat = marker.location[0]
centre_lon = marker.location[1]

outer_polycircle = polycircles.Polycircle(
latitude=centre_lat,
longitude=centre_lon,
radius=(marker.distance + (marker.precision / 2.0))*M_IN_MILE,
number_of_vertices=50)
inner_polycircle = polycircles.Polycircle(
latitude=centre_lat,
longitude=centre_lon,
radius=(marker.distance - (marker.precision / 2.0))*M_IN_MILE,
number_of_vertices=50)

pol = kml_obj.newpolygon(
name="%.6f, %.6f | %.3f" % (centre_lat, centre_lon, marker.distance),
outerboundaryis=outer_polycircle.to_kml(),
innerboundaryis=inner_polycircle.to_kml())
pol.style.polystyle.color = simplekml.Color.changealphaint(
100, simplekml.Color.red)

for key, loc in extra_locations.iteritems():
kml_obj.newpoint(
name=key,
coords=[(loc[1], loc[0])])
kml_obj.save(parent_folder + "output/" + filepath)
5 changes: 5 additions & 0 deletions requirements.txt
@@ -0,0 +1,5 @@
pynder
robobrowser
simplekml
polycircles
html5lib
163 changes: 163 additions & 0 deletions run.py
@@ -0,0 +1,163 @@
import time
import setup
import config
import kml
import random
import math
from pprint import pprint

from tinder import TinderUser
from happn import HappnClient, HappnUser
from fake import *
from utils import *
from interfaces import *

import utils


class DistanceMarkerPatternsParallel(Property):

INITIAL_LOCATION_DELTA = 0.010

def generate_inputs(self):
return {
'location': (37.749, -122.372),
'distance_marker_direction': (1, 0),
'initial_location_direction': (1, 0)
}

def evaluate(self, attacker, target, inputs):
loc = inputs['location']
distance_marker_direction = inputs['distance_marker_direction']
initial_location_direction = inputs['initial_location_direction']

n_markers = 6

attacker.move(loc)
target.move(loc)
print attacker.location()
print target.location()
print attacker.ping(target.user_id())

points = []
points.append(("TARGET", target.location()))

def run_f(direction, i):
return utils._distance_markers(attacker, target, n_markers, direction)

def process_f(distance_marker_set, i):
for m in distance_marker_set.distance_markers:
key = "%.1f" % m.distance
points.append((key, m.location))
print utils._distance(target.location(), m.location)
kml.points(points, "%s-%d" % (time.time(), i))

stream = _axis_pair_test_case_stream(loc, self.INITIAL_LOCATION_DELTA, 6)

pprint([(tc.attacker_loc, tc.target_loc, tc.direction) for tc in stream])

_run_over_stream(attacker, target, run_f, process_f, stream)

kml.points(points)

class TestCase:

def __init__(self, attacker_loc, target_loc, direction):
"""
Attacker starts at attacker_loc
Target starts at target_loc
Attacker shuffles in direction
"""
self.attacker_loc = attacker_loc
self.target_loc = target_loc
self.direction = direction

def _linear_test_case_stream(centre, line_direction, delta, amp, test_case_direction, target_loc=None):
"""
Return a line of test cases.
"""
def mk(loc):
if target_loc:
tl = target_loc
else:
tl = loc
return TestCase(loc, tl, test_case_direction)

return map(mk, _linear_location_stream(centre, line_direction, delta, amp))

def _linear_location_stream(centre, line_direction, delta, amp):
"""
Return a line of locations either side of given centre.
"""
return [(
centre[0] + line_direction[0] * n * delta,
centre[1] + line_direction[1] * n * delta
) for n in range(-amp, amp+1)]

def _square_test_case_stream(centre, delta, amp):
"""
Return a single square of test cases.
"""
dirs = [(-1,0), (1,0), (0,-1), (0,1)]
stream = []
for d in dirs:
line_dir = (1-abs(d[0]), 1-abs(d[1]))
stream += _linear_test_case_stream(centre, line_dir, delta, amp, d, centre)
return stream

def _axis_pair_test_case_stream(centre, delta, amp):
"""
Return an axis of test cases that rake across the given centre
"""
c1 = (centre[0] - delta*amp, centre[1])
c2 = (centre[0], centre[1] - delta*amp)

line_dir1 = (0, 1)
test_case_dir1 = (1, 0)
line_dir2 = (1, 0)
test_case_dir2 = (0, 1)

return _linear_test_case_stream(c1, line_dir1, delta, amp, test_case_dir1, centre) + _linear_test_case_stream(c2, line_dir2, delta, amp, test_case_dir2, centre)



def _run_over_stream(attacker, target, run_f, process_f, test_case_stream):
rets = []
for i, tc in enumerate(test_case_stream):
attacker.move(tc.attacker_loc)
target.move(tc.target_loc)
ret = run_f(tc.direction, i)
processed = process_f(ret, i)
rets.append(processed)

return rets


live = True
attacker = None
target = None
seed = 1234

if live:
attacker_session = setup.setup(
config.ATTACKER_FB_EMAIL,
config.ATTACKER_FB_PASSWORD,
)
target_session = setup.setup(
config.TARGET_FB_EMAIL,
config.TARGET_FB_PASSWORD,
)
attacker = TinderUser(attacker_session)
target = TinderUser(target_session)
else:
service = FakeService()
attacker = FakeUser(1, (0,0), service)
target = FakeUser(2, (3,5), service)

print "Attacker ID: %s" % attacker.user_id()
print "Target ID: %s" % target.user_id()

print(attacker.location())
prop = DistanceMarkerPatternsParallel()
o = prop.multi_evaluate(attacker, target, 1, seed)
exit(0)

0 comments on commit cd5e336

Please sign in to comment.