1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | #
|
---|
4 | ###########################################
|
---|
5 | #
|
---|
6 | # Script for importing DroidStumbler .csv files
|
---|
7 | #
|
---|
8 | # In theory, only the -f option is needed, but for overview's sake, please use the others aswell.
|
---|
9 | # -f = location of the .netxml, e.g. '/home/test.csv'
|
---|
10 | # -m = name of the dataset, e.g. 'Walk in park' or 'Trip with boat'
|
---|
11 | # -g = your name
|
---|
12 | # -e = your email address
|
---|
13 | #
|
---|
14 | # (Run from project root)
|
---|
15 | # ./manage.py import_droidstumbler -f <file location> -m <dataset name> -g <username> -e <email>
|
---|
16 | #
|
---|
17 | # Make sure the variables in this script match the column numbers in your file e.g.;
|
---|
18 | # Lat is read from the first column [0], if the lat in your file is in the 4th column, change
|
---|
19 | # 'lat = row[0]' to 'lat = row[3]'.
|
---|
20 | # Also, take note of the 'replace()' and 'strip()' functions. These should probably be edited aswell.
|
---|
21 | #
|
---|
22 | # Dennis Wagenaar
|
---|
23 | # d.wagenaar@gmail.com
|
---|
24 | #
|
---|
25 | ###########################################
|
---|
26 |
|
---|
27 | from django.core.management import setup_environ
|
---|
28 | from django.core.management.base import BaseCommand, CommandError
|
---|
29 | from optparse import OptionParser, make_option
|
---|
30 | from gheat.models import *
|
---|
31 | import csv
|
---|
32 | import datetime
|
---|
33 | import os
|
---|
34 | import sys
|
---|
35 |
|
---|
36 | def import_droidstumbler(location, meetrondje, gebruiker, email):
|
---|
37 |
|
---|
38 | g, created = Gebruiker.objects.get_or_create(naam=gebruiker , email=email)
|
---|
39 | a, created = Apparatuur.objects.get_or_create(antenne='buildin' , kaart='mobilePhone')
|
---|
40 | mr = MeetRondje.objects.create(datum=datetime.datetime.now() , naam=meetrondje , gebruiker=g , apparatuur=a)
|
---|
41 |
|
---|
42 | csvfile = csv.reader(open(location, 'rb'), delimiter=',')
|
---|
43 | for row in csvfile:
|
---|
44 | epoch, msg_type, lat, lon, accuracy, ssid, bssid, level, frequency, capabilities, empty = row
|
---|
45 | if msg_type == "data" and lat and lon:
|
---|
46 | ap, created = Accespoint.objects.get_or_create(mac=bssid, ssid=ssid, encryptie=capabilities)
|
---|
47 | if created:
|
---|
48 | print "# INFO: New AccessPoint %s (%s)" % (ap.ssid, ap.mac)
|
---|
49 | m = Meting.objects.create(meetrondje=mr, accespoint=ap, latitude=lat, longitude=lon, signaal=(100 + int(level)))
|
---|
50 | print "# INFO: New Meeting: %s" % m
|
---|
51 |
|
---|
52 | class Command(BaseCommand):
|
---|
53 | args = '<csvfile>'
|
---|
54 | option_list = BaseCommand.option_list + (
|
---|
55 | make_option('-m', '--meetrondje', dest='meetrondje', default='rondje'),
|
---|
56 | make_option('-g', '--gebruiker', dest='gebruiker', default='username'),
|
---|
57 | make_option('-e', '--email', dest='email', default='foo@bar.org'),
|
---|
58 | )
|
---|
59 |
|
---|
60 | def handle(self, *args, **options):
|
---|
61 | try:
|
---|
62 | (csv_file,) = args
|
---|
63 | except ValueError:
|
---|
64 | self.print_help(sys.argv[0],sys.argv[1])
|
---|
65 | raise CommandError("Not all arguments are provided")
|
---|
66 | if not os.path.isfile(csv_file):
|
---|
67 | raise CommandError("csv file '%s' does not exists" % csv_file)
|
---|
68 | import_droidstumbler(csv_file,options['meetrondje'],options['gebruiker'],options['email'])
|
---|