summaryrefslogtreecommitdiffstats
path: root/Src/PyCatcher/src/cellIDDatabase.py
blob: d12ce600a58135be09209a36ad53c43fc82b5f3e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import re
import urllib2
from settings import Open_Cell_ID_Key
from struct import pack, unpack
from httplib import HTTP
import sqlite3
import os
from settings import Database_path

class CIDDatabases:
    NONE = 0
    GOOGLE = 1
    OPENCID = 2
    LOCAL = 3

class Translator:
    Country = {
        'Germany':'de'
    }

    Provider = {
        'T-Mobile':'01',
        'Vodafone':'02',
        'E-Plus':'03',
        'O2':'07'
    }

    MCC = {
        'Germany':262
    }

class CellIDDBStatus:
    CONFIRMED = 0
    APPROXIMATED = 1
    ERROR = 2
    NOT_LOOKED_UP = 3
    NOT_IN_DB = 4

class CellIDDatabase:


    def fetch_id_from_OpenCellID(self,cid, lac, country, provider):
        key_ocid = Open_Cell_ID_Key

        mcc = Translator.MCC[country]
        mnc = Translator.Provider[provider]
        
        url = 'http://www.opencellid.org/cell/get?key=%s&mnc=%s&mcc=%d&lac=%d&cellid=%d'%(key_ocid,mnc,mcc,lac,cid)
        response = urllib2.urlopen(url).read()

        status = (re.search(r'stat="(.+)"',response)).group(1)

        if status != 'ok':
            status = CellIDDBStatus.ERROR
            return

        match = re.search(r'lat="(\d+\.\d+)".*lon="(\d+\.\d+).*range="(\d+)"',response)
        latitude,longitude,range = match.group(1),match.group(2),match.group(3)

        if int(range) > 10000:
            status = CellIDDBStatus.APPROXIMATED
        else:
            status = CellIDDBStatus.CONFIRMED

        latitude = float(latitude)
        longitude = float(longitude)

        if latitude == 0 or longitude == 0:
            status = CellIDDBStatus.NOT_IN_DB

        return status, latitude, longitude


    def fetch_id_from_Google(self, cid, lac, country):
        latitude = 0
        longitude = 0
        device = "Motorola C123"
        country = Translator.Country[country]
        b_string = pack('>hqh2sh13sh5sh3sBiiihiiiiii',
            21, 0,
            len(country), country,
            len(device), device,
            len('1.3.1'), "1.3.1",
            len('Web'), "Web",
            27, 0, 0,
            3, 0, cid, lac,
            0, 0, 0, 0)

        http = HTTP('www.google.com', 80)
        http.putrequest('POST', '/glm/mmap')
        http.putheader('Content-Type', 'application/binary')
        http.putheader('Content-Length', str(len(b_string)))
        http.endheaders()
        http.send(b_string)
        code, msg, headers = http.getreply()
        try:
            bytes = http.file.read()
            (a, b,errorCode, latitude, longitude, c, d, e) = unpack(">hBiiiiih",bytes)
            latitude /= 1000000.0
            longitude /= 1000000.0
            status = CellIDDBStatus.CONFIRMED
        except:
            status = CellIDDBStatus.NOT_IN_DB

        return status, latitude, longitude

    def fetch_id_from_local(self, cid, database):
        database += '.db'
        path = os.path.join(Database_path, database)
        if os.path.exists(path):
            connection = sqlite3.connect(path)
        else:
            return CellIDDBStatus.ERROR,0,0
        if connection:
            cursor = connection.cursor()
        else:
            return CellIDDBStatus.ERROR,0,0

        sql = 'SELECT * FROM basestations WHERE cellid =%d'%cid
        cursor.execute(sql)
        try:
            result = (cursor.fetchall())[0]
        except:
            result = None

        if result:
            cursor.close()
            connection.close()
            return CellIDDBStatus.CONFIRMED,0,0
        else:
            cursor.close()
            connection.close()
            return CellIDDBStatus.NOT_IN_DB,0,0