source: src/django_gheat/gheat/management/commands/import_droidstumbler.py@ 9163

Last change on this file since 9163 was 9163, checked in by rick, 14 years ago

I guess the python documentation is right about this one :-)

"Lots of people want their programs to have “required options”. Think about it.
If it’s required, then it’s not optional! If there is a piece of information
that your program absolutely requires in order to run successfully, that’s what
positional arguments are for." - http://docs.python.org/library/optparse.html

Fixed.

  • Property svn:executable set to *
File size: 2.8 KB
Line 
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
27from django.core.management import setup_environ
28from django.core.management.base import BaseCommand, CommandError
29from optparse import OptionParser, make_option
30from gheat.models import *
31import csv
32import datetime
33import os
34import sys
35
36def 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
52class 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'])
Note: See TracBrowser for help on using the repository browser.