Commit bf20b34d authored by ernesst's avatar ernesst
Browse files

Upload New File

parent 342f9f90
#!/usr/bin/env python3
import time
import subprocess
import re
import json
import time
import sys
import json
import requests
import os
#from subprocess import Popen, PIPE, STDOUT
i = 0
# Version 0.1
def read_gps():
global latitude
global longitude
global accuracy
latitude = str("")
longitude = str("")
accuracy = str("")
cmd = ['sudo test_gps']
p = subprocess.Popen(cmd, stdout = subprocess.PIPE,
stderr = subprocess.STDOUT, shell = True)
for line in p.stdout:
line = line.decode('utf-8')
#print(line)
if re.search("^latitude", line):
latitude = line.split()
latitude = latitude[1]
if re.search("^longtide", line): #bug in test_gps
longitude = line.split()
longitude = longitude[1]
if re.search("^accuracy", line): #bug in test_gps
accuracy = line.split()
accuracy= accuracy[1]
if accuracy != "":
#print(line + "=> accuracy : " + accuracy)
#print(line + "=> longitude : " + longitude)
#print(line + "=> latitude : " + latitude)
return accuracy,longitude,latitude
def read_wifi(lat, lon, acc):
cmdstr = ['sudo iwlist wlan0 scanning']
proc = subprocess.Popen(cmdstr, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, stderr = proc.communicate()
stdout = stdout.decode('utf-8')
Scan_list = []
Scan_list = re.split("\n Cell ", stdout)
Scan_list.pop(0)
wifi_host = []
json_item = {}
json_item = {"timestamp" : int(round(time.time() * 1000)),"position":{}, "wifiAccessPoints" : []}
position = {"latitude": lat, "longitude" :lon, "accuracy": acc}
json_item["position"] = position
age = ''
radio_type = ''
Frequency = ''
channe = ''
mac = ''
for router in Scan_list:
wifi_host = router.split("\n ")
#print("****************************")
for row in wifi_host:
row = row.strip()
#if re.search("^Frequency:", row):
#Frequency = row.split()
#Frequency = Frequency[0].split(":")
#Frequency = Frequency[1]
#Frequency = Frequency
#print(Frequency)
#print(row + " => " + Frequency)
#item[row.frequency] = Frequency
if re.search("^Channel", row):
channel = row.split(":")
channel = channel[1]
#print(row+ " => " + channel )
if re.search("- Address:", row):
mac = row.split()
mac = mac[3]
#print(row+ " => " + mac)
if re.search("^IE: IEEE", row):
radio_type = row.split()
radio_type = radio_type[2]
radio_type = radio_type.split("/")
#print(radio_type)
radio_type = radio_type[0]
#print(row + " => " + radio_type)
if re.search("^Extra: Last beacon:", row):
age = row.split()
age = age[3]
age = age[:-2]
#print(row + " => " + age)
json_item['wifiAccessPoints'].append({"frequency":Frequency, "channel":channel, "macAddress": mac, "age": age, "radioType": radio_type})
print("Wifi networks discovered : " + str(len(json_item['wifiAccessPoints'])))
data = {}
data["items"] = []
try:
with open('data.txt', "r") as masterjf:
try:
data = json.load(masterjf)
data["items"].append(json_item)
except:
data["items"].append(json_item)
except:
data["items"].append(json_item)
#print(data)
with open('data.txt', 'w+') as outfile:
json.dump(data, outfile)
try:
while True:
print("Scan No : " + str(i))
read_gps()
print("lat : " + latitude + " lon : " + longitude)
read_wifi(latitude,longitude,accuracy)
time.sleep(4)
i = i + 1
except KeyboardInterrupt:
print("\n \nUploading the information to https://location.services.mozilla.com, please wait.")
url = 'https://location.services.mozilla.com/v2/geosubmit'
with open('data.txt') as json_file:
payload = json.load(json_file)
headers = {'content-type': 'application/json'}
try:
response = requests.post(url, data=json.dumps(payload), headers=headers)
if response.status_code == 200:
print("***************************")
print("Data uploaded successfully")
print("***************************")
os.remove("data.txt")
else:
print("Sorry an issue happen during the upload, the collect is not lost see data.txt")
time.sleep(2)
print("\n***************************")
print("\nThanks for the contribution")
except:
print("\nSorry an issue happen during the upload, the collect is not lost see data.txt")
sys.exit()
#dbus-send --system --print-reply=literal --type=method_call --dest=org.ofono /ril_0 org.ofono.NetworkRegistration.GetProperties
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