Merge branch 'main' of ssh://code.freedombone.net:2222/bashrc/epicyon

main
Bob Mottram 2021-06-08 19:05:26 +01:00
commit 1ae955a024
6 changed files with 518 additions and 323 deletions

132
city.py
View File

@ -126,10 +126,45 @@ def _getCityPulse(currTimeOfDay, decoySeed: int) -> (float, float):
return distanceFromCityCenter, angleRadians
def parseNogoString(nogoLine: str) -> []:
"""Parses a line from locations_nogo.txt and returns the polygon
"""
polygonStr = nogoLine.split(':', 1)[1]
if ';' in polygonStr:
pts = polygonStr.split(';')
else:
pts = polygonStr.split(',')
if len(pts) <= 4:
return []
polygon = []
for index in range(int(len(pts)/2)):
if index*2 + 1 >= len(pts):
break
longitudeStr = pts[index*2].strip()
latitudeStr = pts[index*2 + 1].strip()
if 'E' in latitudeStr or 'W' in latitudeStr:
longitudeStr = pts[index*2 + 1].strip()
latitudeStr = pts[index*2].strip()
if 'E' in longitudeStr:
longitudeStr = \
longitudeStr.replace('E', '')
longitude = float(longitudeStr)
elif 'W' in longitudeStr:
longitudeStr = \
longitudeStr.replace('W', '')
longitude = -float(longitudeStr)
else:
longitude = float(longitudeStr)
latitude = float(latitudeStr)
polygon.append([latitude, longitude])
return polygon
def spoofGeolocation(baseDir: str,
city: str, currTime, decoySeed: int,
citiesList: []) -> (float, float, str, str,
str, str, int):
citiesList: [],
nogoList: []) -> (float, float, str, str,
str, str, int):
"""Given a city and the current time spoofs the location
for an image
returns latitude, longitude, N/S, E/W,
@ -138,6 +173,11 @@ def spoofGeolocation(baseDir: str,
locationsFilename = baseDir + '/custom_locations.txt'
if not os.path.isfile(locationsFilename):
locationsFilename = baseDir + '/locations.txt'
nogoFilename = baseDir + '/custom_locations_nogo.txt'
if not os.path.isfile(nogoFilename):
nogoFilename = baseDir + '/locations_nogo.txt'
manCityRadius = 0.1
varianceAtLocation = 0.0004
default_latitude = 51.8744
@ -156,6 +196,19 @@ def spoofGeolocation(baseDir: str,
with open(locationsFilename, "r") as f:
cities = f.readlines()
nogo = []
if nogoList:
nogo = nogoList
else:
if os.path.isfile(nogoFilename):
with open(nogoFilename, "r") as f:
nogoList = f.readlines()
for line in nogoList:
if line.startswith(city + ':'):
polygon = parseNogoString(line)
if polygon:
nogo.append(polygon)
city = city.lower()
for cityName in cities:
if city in cityName.lower():
@ -183,22 +236,35 @@ def spoofGeolocation(baseDir: str,
datetime.timedelta(hours=approxTimeZone)
camMake, camModel, camSerialNumber = \
_getDecoyCamera(decoySeed)
# patterns of activity change in the city over time
(distanceFromCityCenter, angleRadians) = \
_getCityPulse(currTimeAdjusted, decoySeed)
# The city radius value is in longitude and the reference
# is Manchester. Adjust for the radius of the chosen city.
if areaKm2 > 1:
manRadius = math.sqrt(630 / math.pi)
radius = math.sqrt(areaKm2 / math.pi)
cityRadius = manCityRadius * manRadius / radius
else:
cityRadius = manCityRadius
# Get the position within the city, with some randomness added
latitude += \
distanceFromCityCenter * cityRadius * math.cos(angleRadians)
longitude += \
distanceFromCityCenter * cityRadius * math.sin(angleRadians)
validCoord = False
seedOffset = 0
while not validCoord:
# patterns of activity change in the city over time
(distanceFromCityCenter, angleRadians) = \
_getCityPulse(currTimeAdjusted, decoySeed + seedOffset)
# The city radius value is in longitude and the reference
# is Manchester. Adjust for the radius of the chosen city.
if areaKm2 > 1:
manRadius = math.sqrt(630 / math.pi)
radius = math.sqrt(areaKm2 / math.pi)
cityRadius = manCityRadius * manRadius / radius
else:
cityRadius = manCityRadius
# Get the position within the city, with some randomness added
latitude += \
distanceFromCityCenter * cityRadius * \
math.cos(angleRadians)
longitude += \
distanceFromCityCenter * cityRadius * \
math.sin(angleRadians)
longval = longitude
if longdirection == 'W':
longval = -longitude
validCoord = not pointInNogo(nogo, latitude, longval)
if not validCoord:
seedOffset += 1
if seedOffset > 100:
break
# add a small amount of variance around the location
fraction = randint(0, 100000) / 100000
distanceFromLocation = fraction * fraction * varianceAtLocation
@ -229,3 +295,33 @@ def getSpoofedCity(city: str, baseDir: str, nickname: str, domain: str) -> str:
with open(cityFilename, 'r') as fp:
city = fp.read().replace('\n', '')
return city
def _pointInPolygon(poly: [], x: float, y: float) -> bool:
"""Returns true if the given point is inside the given polygon
"""
n = len(poly)
inside = False
p2x = 0.0
p2y = 0.0
xints = 0.0
p1x, p1y = poly[0]
for i in range(n + 1):
p2x, p2y = poly[i % n]
if y > min(p1y, p2y):
if y <= max(p1y, p2y):
if x <= max(p1x, p2x):
if p1y != p2y:
xints = (y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x
if p1x == p2x or x <= xints:
inside = not inside
p1x, p1y = p2x, p2y
return inside
def pointInNogo(nogo: [], latitude: float, longitude: float) -> bool:
for polygon in nogo:
if _pointInPolygon(polygon, latitude, longitude):
return True
return False

File diff suppressed because one or more lines are too long

14
locations_nogo.txt 100644
View File

@ -0,0 +1,14 @@
NEW YORK, USA: 73.951W,40.879, 73.974W,40.83, 74.029W,40.756, 74.038W,40.713, 74.056W,40.713, 74.127W,40.647, 74.038W,40.629, 73.995W,40.667, 74.014W,40.676, 73.994W,40.702, 73.967W,40.699, 73.958W,40.729, 73.956W,40.745, 73.918W,40.781, 73.937W,40.793, 73.946W,40.782, 73.977W,40.738, 73.98W,40.713, 74.012W,40.705, 74.006W,40.752, 73.955W,40.824
NEW YORK, USA: 74.115W,40.663, 74.065W,40.602, 74.118W,40.555, 74.047W,40.516, 73.882W,40.547, 73.909W,40.618, 73.978W,40.579, 74.009W,40.602, 74.033W,40.61, 74.039W,40.623, 74.032W,40.641, 73.996W,40.665
LONDON, ENGLAND: 0.23888E,51.459, 0.1216E,51.5, 0.016E,51.479, 0.097W,51.502, 0.126W,51.482, 0.196W,51.457, 0.292W,51.465, 0.309W,51.49, 0.226W,51.495, 0.198W,51.47, 0.174W,51.488, 0.136W,51.489, 0.1189W,51.515, 0.038E,51.513, 0.0692E,51.51, 0.12833E,51.526, 0.3289E,51.475
LONDON, ENGLAND: 0.054W,51.535, 0.044W,51.53, 0.008W,51.55, 0.0429W,51.57, 0.038W,51.6, 0.0209W,51.603, 0.032W,51.613, 0.00191E,51.66, 0.024W,51.666, 0.0313W,51.659, 0.0639W,51.579, 0.059W,51.568, 0.0329W,51.552
BERLIN, GERMANY: 13.491E,52.469, 13.416E,52.512, 13.378E,52.517, 13.377E,52.51, 13.336E,52.508, 13.323E,52.518, 13.286E,52.518, 13.295E,52.533, 13.309E,52.522, 13.322E,52.526, 13.395E,52.524, 13.421E,52.516, 13.465E,52.499, 13.471E,52.502, 13.498E,52.487, 13.501E,52.479, 13.51E,52.46
ISTANBUL, TURKEY: 28.758E,41.117, 28.781E,41.001, 28.871E,40.978, 28.963E,41.013, 28.923E,41.057, 28.949E,41.071, 28.952E,41.047, 28.979E,41.031, 28.998E,41.045, 29.026E,41.052, 29.05073E,41.101, 29.059E,41.127, 29.028E,41.158, 29.095E,41.214, 29.135E,41.196, 29.084E,41.145, 29.107E,41.12, 29.074E,41.098, 29.068E,41.075, 29.057E,41.046, 29.014E,41.022, 29.033E,40.987, 29.103E,40.953, 29.147E,40.907, 29.186E,40.889, 29.232E,40.877, 29.199E,40.82, 28.609E,40.932
MADRID, SPAIN: 3.774W,40.397, 3.713W,40.415, 3.712W,40.422, 3.721W,40.432, 3.717W,40.435, 3.713W,40.463, 3.729W,40.46, 3.737W,40.475, 3.748W,40.472, 3.749W,40.477, 3.766W,40.472, 3.762W,40.458, 3.788W,40.442, 3.789W,40.427, 3.781W,40.43, 3.777W,40.426, 3.782W,40.419, 3.778W,40.407
MADRID, SPAIN: 3.688W,40.407, 3.678W,40.408, 3.675W,40.411, 3.679W,40.422, 3.688W,40.42, 3.689W,40.416
MADRID, SPAIN: 3.654W,40.412, 3.617W,40.405, 3.604W,40.411, 3.607W,40.419, 3.62W,40.415, 3.623W,40.419, 3.627W,40.419, 3.646W,40.427, 3.649W,40.419, 3.657W,40.422
LOS ANGELES, USA: 118.475W,34.146, 118.499W,34.024, 118.368W,33.832, 118.254W,33.769, 118.114W,33.772, 117.891W,33.62, 117.471W,33.333, 117.954W,32.915, 119.299W,33.779
SAN JOSE, USA: 121.988W,37.408, 121.924W,37.452, 121.951W,37.498, 121.992W,37.505, 122.056W,37.54, 122.077W,37.578, 122.098W,37.618, 122.131W,37.637, 122.189W,37.706, 122.227W,37.775, 122.279W,37.798, 122.315W,37.802, 122.291W,37.832, 122.309W,37.902, 122.382W,37.915, 122.368W,37.927, 122.514W,37.882, 122.473W,37.83, 122.481W,37.788, 122.394W,37.796, 122.384W,37.729, 122.4W,37.688, 122.382W,37.654, 122.406W,37.637, 122.392W,37.612, 122.356W,37.586, 122.332W,37.586, 122.275W,37.529, 122.228W,37.488, 122.181W,37.482, 122.134W,37.48, 122.128W,37.471, 122.122W,37.448, 122.095W,37.428, 122.07W,37.413, 122.036W,37.402, 122.035W,37.421
SAN FRANCISCO, USA: 118.475W,34.146, 118.499W,34.024, 118.368W,33.832, 118.254W,33.769, 118.114W,33.772, 117.891W,33.62, 117.471W,33.333, 117.954W,32.915, 119.299W,33.779
SAN JOSE, USA: 121.988W,37.408, 121.924W,37.452, 121.951W,37.498, 121.992W,37.505, 122.056W,37.54, 122.077W,37.578, 122.098W,37.618, 122.131W,37.637, 122.189W,37.706, 122.227W,37.775, 122.279W,37.798, 122.315W,37.802, 122.291W,37.832, 122.309W,37.902, 122.382W,37.915, 122.368W,37.927, 122.514W,37.882, 122.473W,37.83, 122.481W,37.788, 122.394W,37.796, 122.384W,37.729, 122.4W,37.688, 122.382W,37.654, 122.406W,37.637, 122.392W,37.612, 122.356W,37.586, 122.332W,37.586, 122.275W,37.529, 122.228W,37.488, 122.181W,37.482, 122.134W,37.48, 122.128W,37.471, 122.122W,37.448, 122.095W,37.428, 122.07W,37.413, 122.036W,37.402, 122.035W,37.421
OAKLAND, USA: 121.988W,37.408, 121.924W,37.452, 121.951W,37.498, 121.992W,37.505, 122.056W,37.54, 122.077W,37.578, 122.098W,37.618, 122.131W,37.637, 122.189W,37.706, 122.227W,37.775, 122.279W,37.798, 122.315W,37.802, 122.291W,37.832, 122.309W,37.902, 122.382W,37.915, 122.368W,37.927, 122.514W,37.882, 122.473W,37.83, 122.481W,37.788, 122.394W,37.796, 122.384W,37.729, 122.4W,37.688, 122.382W,37.654, 122.406W,37.637, 122.392W,37.612, 122.356W,37.586, 122.332W,37.586, 122.275W,37.529, 122.228W,37.488, 122.181W,37.482, 122.134W,37.48, 122.128W,37.471, 122.122W,37.448, 122.095W,37.428, 122.07W,37.413, 122.036W,37.402, 122.035W,37.421

View File

@ -87,7 +87,7 @@ def _spoofMetaData(baseDir: str, nickname: str, domain: str,
(latitude, longitude, latitudeRef, longitudeRef,
camMake, camModel, camSerialNumber) = \
spoofGeolocation(baseDir, spoofCity, currTimeAdjusted,
decoySeed, None)
decoySeed, None, None)
os.system('exiftool -artist="' + nickname + '" ' +
'-Make="' + camMake + '" ' +
'-Model="' + camModel + '" ' +

View File

@ -81,7 +81,9 @@ from like import likePost
from like import sendLikeViaServer
from announce import announcePublic
from announce import sendAnnounceViaServer
from city import parseNogoString
from city import spoofGeolocation
from city import pointInNogo
from media import getMediaPath
from media import getAttachmentMediaType
from delete import sendDeleteViaServer
@ -3605,19 +3607,45 @@ def testRemovePostInteractions() -> None:
def testSpoofGeolocation() -> None:
print('testSpoofGeolocation')
nogoLine = \
'NEW YORK, USA: 73.951W,40.879, 73.974W,40.83, ' + \
'74.029W,40.756, 74.038W,40.713, 74.056W,40.713, ' + \
'74.127W,40.647, 74.038W,40.629, 73.995W,40.667, ' + \
'74.014W,40.676, 73.994W,40.702, 73.967W,40.699, ' + \
'73.958W,40.729, 73.956W,40.745, 73.918W,40.781, ' + \
'73.937W,40.793, 73.946W,40.782, 73.977W,40.738, ' + \
'73.98W,40.713, 74.012W,40.705, 74.006W,40.752, ' + \
'73.955W,40.824'
polygon = parseNogoString(nogoLine)
assert len(polygon) > 0
assert polygon[0][1] == -73.951
assert polygon[0][0] == 40.879
citiesList = [
'NEW YORK, USA:40.7127281:W74.0060152:784',
'LOS ANGELES, USA:34.0536909:W118.242766:1214',
'HOUSTON, USA:29.6072:W95.1586:1553',
'MANCHESTER, ENGLAND:53.4794892:W2.2451148:630',
'BERLIN, GERMANY:52.5170365:13.3888599:891',
'ANKARA, TURKEY:39.93:32.85:24521'
'ANKARA, TURKEY:39.93:32.85:24521',
'LONDON, ENGLAND:51.5073219:W0.1276474:1738'
]
testSquare = [
[[0.03, 0.01], [0.02, 10], [10.01, 10.02], [10.03, 0.02]]
]
assert pointInNogo(testSquare, 5, 5)
assert pointInNogo(testSquare, 2, 3)
assert not pointInNogo(testSquare, 20, 5)
assert not pointInNogo(testSquare, 11, 6)
assert not pointInNogo(testSquare, 5, -5)
assert not pointInNogo(testSquare, 5, 11)
assert not pointInNogo(testSquare, -5, -5)
assert not pointInNogo(testSquare, -5, 5)
nogoList = []
currTime = datetime.datetime.utcnow()
decoySeed = 7634681
cityRadius = 0.1
coords = spoofGeolocation('', 'los angeles', currTime,
decoySeed, citiesList)
decoySeed, citiesList, nogoList)
assert coords[0] >= 34.0536909 - cityRadius
assert coords[0] <= 34.0536909 + cityRadius
assert coords[1] >= 118.242766 - cityRadius
@ -3627,8 +3655,9 @@ def testSpoofGeolocation() -> None:
assert len(coords[4]) > 4
assert len(coords[5]) > 4
assert coords[6] > 0
nogoList = []
coords = spoofGeolocation('', 'unknown', currTime,
decoySeed, citiesList)
decoySeed, citiesList, nogoList)
assert coords[0] >= 51.8744 - cityRadius
assert coords[0] <= 51.8744 + cityRadius
assert coords[1] >= 0.368333 - cityRadius
@ -3641,6 +3670,14 @@ def testSpoofGeolocation() -> None:
kmlStr = '<?xml version="1.0" encoding="UTF-8"?>\n'
kmlStr += '<kml xmlns="http://www.opengis.net/kml/2.2">\n'
kmlStr += '<Document>\n'
nogoLine2 = \
'NEW YORK, USA: 74.115W,40.663, 74.065W,40.602, ' + \
'74.118W,40.555, 74.047W,40.516, 73.882W,40.547, ' + \
'73.909W,40.618, 73.978W,40.579, 74.009W,40.602, ' + \
'74.033W,40.61, 74.039W,40.623, 74.032W,40.641, ' + \
'73.996W,40.665'
polygon2 = parseNogoString(nogoLine2)
nogoList = [polygon, polygon2]
for i in range(1000):
dayNumber = randint(10, 30)
hour = randint(1, 23)
@ -3651,7 +3688,45 @@ def testSpoofGeolocation() -> None:
" " + hourStr + ":14",
"%Y-%m-%d %H:%M")
coords = spoofGeolocation('', 'new york, usa', currTime,
decoySeed, citiesList)
decoySeed, citiesList, nogoList)
longitude = coords[1]
if coords[3] == 'W':
longitude = -coords[1]
kmlStr += '<Placemark id="' + str(i) + '">\n'
kmlStr += ' <name>' + str(i) + '</name>\n'
kmlStr += ' <Point>\n'
kmlStr += ' <coordinates>' + str(longitude) + ',' + \
str(coords[0]) + ',0</coordinates>\n'
kmlStr += ' </Point>\n'
kmlStr += '</Placemark>\n'
nogoLine = \
'LONDON, ENGLAND: 0.23888E,51.459, 0.1216E,51.5, ' + \
'0.016E,51.479, 0.097W,51.502, 0.126W,51.482, ' + \
'0.196W,51.457, 0.292W,51.465, 0.309W,51.49, ' + \
'0.226W,51.495, 0.198W,51.47, 0.174W,51.488, ' + \
'0.136W,51.489, 0.1189W,51.515, 0.038E,51.513, ' + \
'0.0692E,51.51, 0.12833E,51.526, 0.3289E,51.475'
polygon = parseNogoString(nogoLine)
nogoLine2 = \
'LONDON, ENGLAND: 0.054W,51.535, 0.044W,51.53, ' + \
'0.008W,51.55, 0.0429W,51.57, 0.038W,51.6, ' + \
'0.0209W,51.603, 0.032W,51.613, 0.00191E,51.66, ' + \
'0.024W,51.666, 0.0313W,51.659, 0.0639W,51.579, ' + \
'0.059W,51.568, 0.0329W,51.552'
polygon2 = parseNogoString(nogoLine2)
nogoList = [polygon, polygon2]
for i in range(1000):
dayNumber = randint(10, 30)
hour = randint(1, 23)
hourStr = str(hour)
if hour < 10:
hourStr = '0' + hourStr
currTime = datetime.datetime.strptime("2021-05-" + str(dayNumber) +
" " + hourStr + ":14",
"%Y-%m-%d %H:%M")
coords = spoofGeolocation('', 'london, england', currTime,
decoySeed, citiesList, nogoList)
longitude = coords[1]
if coords[3] == 'W':
longitude = -coords[1]

View File

@ -41,6 +41,10 @@ def getHashtagCategoriesFeed(baseDir: str,
rssStr += ' <title>' + categoryStr + '</title>\n'
listStr = ''
for hashtag in hashtagList:
if ':' in hashtag:
continue
if '&' in hashtag:
continue
listStr += hashtag + ' '
rssStr += ' <description>' + listStr.strip() + '</description>\n'
rssStr += ' <link/>\n'