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

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

Allow import (default) .gz files as well.

  • Property svn:executable set to *
File size: 2.5 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Script for importing DroidStumbler .csv files
5#
6# Original by Dennis Wagenaar <d.wagenaar@gmail.com>
7#
8# Rick van der Zwet <info@rickvanderzwet.nl>
9#
10from django.core.management import setup_environ
11from django.core.management.base import BaseCommand, CommandError
12from optparse import OptionParser, make_option
13from gheat.models import *
14import csv
15import datetime
16import gzip
17import os
18import sys
19
20def import_droidstumbler(location, meetrondje, gebruiker, email):
21
22 g, created = Gebruiker.objects.get_or_create(naam=gebruiker , email=email)
23 a, created = Apparatuur.objects.get_or_create(antenne='buildin' , kaart='mobilePhone')
24 mr = MeetRondje.objects.create(datum=datetime.datetime.now() , naam=meetrondje , gebruiker=g , apparatuur=a)
25
26 if location.endswith('.gz'):
27 fh = gzip.open(location,'rb')
28 else:
29 fh = open(location,'rb')
30 csvfile = csv.reader(fh, delimiter=',')
31 count = 0
32 ap_cache = {}
33 print "#INFO: Importing points"
34 for row in csvfile:
35 try:
36 epoch, msg_type, lat, lon, accuracy, ssid, bssid, level, frequency, capabilities = row
37 except ValueError:
38 print "# WARNING: Unable to parse line:%i '%s'" % (csvfile.line_num, row)
39 continue
40 if msg_type == "data" and lat and lon:
41 if not ap_cache.has_key(bssid):
42 ap_cache[bssid], created = Accespoint.objects.get_or_create(mac=bssid, ssid=ssid, encryptie=capabilities)
43
44 m = Meting.objects.create(meetrondje=mr, accespoint=ap_cache[bssid], latitude=lat, longitude=lon, signaal=(100 + int(level)))
45 # Give some feedback to the user
46 count += 1
47 if (count % 1000) == 0:
48 sys.stdout.write(str(count))
49 elif (count % 100) == 0:
50 sys.stdout.write(".")
51 sys.stdout.flush()
52
53 sys.stdout.write("%s\n" % count)
54 print "#INFO: All done, goodbye"
55
56
57class Command(BaseCommand):
58 args = '<csvfile>[.gz]'
59 option_list = BaseCommand.option_list + (
60 make_option('-m', '--meetrondje', dest='meetrondje', default='rondje'),
61 make_option('-g', '--gebruiker', dest='gebruiker', default='username'),
62 make_option('-e', '--email', dest='email', default='foo@bar.org'),
63 )
64
65 def handle(self, *args, **options):
66 try:
67 (csv_file,) = args
68 except ValueError:
69 self.print_help(sys.argv[0],sys.argv[1])
70 raise CommandError("Not all arguments are provided")
71 if not os.path.isfile(csv_file):
72 raise CommandError("csv file '%s' does not exists" % csv_file)
73 import_droidstumbler(csv_file,options['meetrondje'],options['gebruiker'],options['email'])
Note: See TracBrowser for help on using the repository browser.