In [19]:
import requests as r
import json
import time
import xlsxwriter

Fahrtenbuch aus MOVES App

In [20]:
detail = 1 # the lower, the more precise the location is geoencoded
fromdate = '20140201' # date in yyyyMMdd
todate = '20140228' # date in yyyyMMdd
In [21]:
# Source: https://gist.github.com/bradmontgomery/5397472
def getadress(latitude,longitude):
    # grab some lat/long coords from wherever. For this example,
    # I just opened a javascript console in the browser and ran:
    #
    # navigator.geolocation.getCurrentPosition(function(p) {
    #   console.log(p);
    # })
    #
    #latitude = 35.1330343
    #longitude = -90.0625056
 
    # Did the geocoding request comes from a device with a
    # location sensor? Must be either true or false.
    sensor = 'true'
 
    # Hit Google's reverse geocoder directly
    # NOTE: I *think* their terms state that you're supposed to
    # use google maps if you use their api for anything.
    base = "http://maps.googleapis.com/maps/api/geocode/json?"
    params = "latlng={lat},{lon}&sensor={sen}".format(
        lat=latitude,
        lon=longitude,
        sen=sensor
    )
    url = "{base}{params}".format(base=base, params=params)

    response = r.get(url)
    
    if response.status_code==200:
        pass
    else:
        print('Error: \'%s\'' % response.json()['error'])
    
    return response.json()['results'][detail]['formatted_address']
In [22]:
url = 'https://api.moves-app.com/api/1.1' # Moves API
params=  {'access_token': '********************', # your Access Token here
          'trackPoints': 'false',
          'from': fromdate,
          'to': todate}
            #'pastDays': '10',
timeline = r.get(url+'/user/storyline/daily', params=params)

if timeline.status_code==200:
    print('Received Data from Moves API.')
else:
    print('Error: \'%s\'' % timeline.json()['error'])
Received Data from Moves API.
In [23]:
begin = time.strptime(timeline.json()[0]['date'], '%Y%m%d')
ende = time.strptime(timeline.json()[-1]['date'], '%Y%m%d')
print('Zeitraum: %s - %s' % (time.strftime("%A, %d %b %Y", begin), time.strftime("%A, %d %b %Y", ende)))
Zeitraum: Saturday, 01 Feb 2014 - Friday, 28 Feb 2014

Create Excel File

In [24]:
# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('Fahrtenbuch-%s.xlsx' % time.strftime("%Y", begin))
worksheet = workbook.add_worksheet(time.strftime("%b-%Y", begin))
# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})

# Text with formatting.
worksheet.write(0,0, 'Fahrtenbuch', bold)
worksheet.write(2,0, 'Von:')
worksheet.write(2,1, '%s' % time.strftime("%A, %d %b %Y", begin))
worksheet.write(3,0, 'Bis:')
worksheet.write(3,1, '%s' % time.strftime("%A, %d %b %Y", ende))
Out[24]:
0

Crawl the Data

In [25]:
fahrtenbuchdata=[]
In [26]:
for day in range(len(timeline.json())): # Tage durchgehen
    
    # Datum
    date = time.strptime(timeline.json()[day]['date'], '%Y%m%d')
    print('%s' % (time.strftime("%A, %d %b %Y", date)))
    print(20*'=')
    
    fahrtenbuchdata.append('%s' % (time.strftime("%A, %d %b %Y", date)))
    
    transport=False
    for i in range(len(timeline.json()[day]['segments'])): # Segmente durchgehen
        
        try: # Ort
            place = timeline.json()[day]['segments'][i]['place']
            lat = timeline.json()[day]['segments'][i]['place']['location']['lat']
            lon = timeline.json()[day]['segments'][i]['place']['location']['lon']
            location= getadress(lat, lon)
            if timeline.json()[day]['segments'][i]['place'].has_key('name'):
                name = timeline.json()[day]['segments'][i]['place']['name']
                
                if transport:
                    #print('%s (%s)\n' % (location, name))
                    print('%s\n' % (location))
                    fahrtenbuchdata.append('%s' % (location))
                    fahrtenbuchdata.append('')
                    transport=False
            else:
                if transport:
                    print('%s\n' % (location))
                    fahrtenbuchdata.append('%s' % (location))
                    fahrtenbuchdata.append('')
                    transport=False
                pass
        except: # an sonsten ist es eine Verbindung
            try:
                #print '.'
                for act in range(len(timeline.json()[day]['segments'][i]['activities'])):

                    # nur Transport nutzen
                    if timeline.json()[day]['segments'][i]['activities'][act]['activity']=='transport':
                        starttime = time.strptime(timeline.json()[day]['segments'][i]['activities'][act]['startTime'], "%Y%m%dT%H%M%S+%f")
                        distance= float(timeline.json()[day]['segments'][i]['activities'][act]['distance'])
                        endtime = time.strptime(timeline.json()[day]['segments'][i]['activities'][act]['endTime'], "%Y%m%dT%H%M%S+%f")
                        
                        #print('%s (%s)' % (location, name))
                        print('%s' % (location))
                        fahrtenbuchdata.append('%s' % (location))
                        route = '%.1fkm (%s - %s)' % (distance/1000.0, time.strftime("%H:%MUhr", starttime), time.strftime("%H:%MUhr", endtime))
                        print(route)
                        fahrtenbuchdata.append(route)
                        
                        transport=True
                    else:
                        pass
                #print '.'
            except: # oder was anderes
                pass

    print('\n')
    fahrtenbuchdata.append('')
Saturday, 01 Feb 2014
====================
Äußere Neustadt, Dresden, Germany
6.5km (03:45Uhr - 03:56Uhr)
Striesen, Dresden, Germany

Striesen, Dresden, Germany
7.9km (10:34Uhr - 10:52Uhr)
Plauen, Dresden, Germany

Plauen, Dresden, Germany
3.6km (11:20Uhr - 11:34Uhr)
Seevorstadt West, Dresden, Germany

Seevorstadt West, Dresden, Germany
5.6km (14:33Uhr - 14:54Uhr)
Striesen, Dresden, Germany

Striesen, Dresden, Germany
4.5km (19:07Uhr - 19:20Uhr)
Altmarkt, 01067 Dresden, Germany

Innere Altstadt, Dresden, Germany
5.7km (22:37Uhr - 22:48Uhr)
Striesen, Dresden, Germany



Sunday, 02 Feb 2014
====================
Striesen, Dresden, Germany
8.1km (09:48Uhr - 10:02Uhr)
Äußere Neustadt, Dresden, Germany

Äußere Neustadt, Dresden, Germany
7.7km (12:21Uhr - 12:38Uhr)
Striesen, Dresden, Germany



Monday, 03 Feb 2014
====================


Tuesday, 04 Feb 2014
====================


Wednesday, 05 Feb 2014
====================
Neugruna, Dresden, Germany
7.7km (15:36Uhr - 16:00Uhr)
Plauen, Dresden, Germany

Plauen, Dresden, Germany
0.6km (19:36Uhr - 19:38Uhr)
Plauen, Dresden, Germany
6.8km (19:39Uhr - 19:54Uhr)
Gruna, Dresden, Germany

Gruna, Dresden, Germany
1.2km (19:58Uhr - 20:01Uhr)
Striesen, Dresden, Germany



Thursday, 06 Feb 2014
====================
Striesen, Dresden, Germany
4.8km (16:01Uhr - 16:18Uhr)
Seevorstadt West, Dresden, Germany

Seevorstadt West, Dresden, Germany
6.2km (17:31Uhr - 17:50Uhr)
Mickten, Dresden, Germany

Mickten, Dresden, Germany
11.4km (22:58Uhr - 23:19Uhr)
Striesen, Dresden, Germany



Friday, 07 Feb 2014
====================
Striesen, Dresden, Germany
5.3km (09:27Uhr - 09:42Uhr)
Südvorstadt, Dresden, Germany

Südvorstadt, Dresden, Germany
5.5km (13:00Uhr - 13:12Uhr)
Striesen, Dresden, Germany

Seidnitz, Dresden, Germany
0.6km (16:48Uhr - 16:49Uhr)
Seidnitz, Dresden, Germany
0.7km (16:50Uhr - 16:52Uhr)
Neugruna, Dresden, Germany



Saturday, 08 Feb 2014
====================
Striesen, Dresden, Germany
112.2km (09:23Uhr - 10:34Uhr)
Annahof, 03099 Kolkwitz, Germany

Annahof, 03099 Kolkwitz, Germany
7.9km (10:44Uhr - 10:58Uhr)
Gallinchen, Cottbus, Germany

Gallinchen, Cottbus, Germany
7.9km (13:21Uhr - 13:34Uhr)
03046 Cottbus, Germany

03046 Cottbus, Germany
114.1km (17:40Uhr - 19:04Uhr)
Striesen, Dresden, Germany

Striesen, Dresden, Germany
1.6km (19:16Uhr - 19:26Uhr)
Striesen, Dresden, Germany



Sunday, 09 Feb 2014
====================
Striesen, Dresden, Germany
6.6km (09:45Uhr - 09:58Uhr)
Kleinpestitz, Dresden, Germany

Kleinpestitz, Dresden, Germany
2.2km (13:46Uhr - 13:52Uhr)
Plauen, Dresden, Germany

Plauen, Dresden, Germany
8.3km (18:52Uhr - 19:08Uhr)
Striesen, Dresden, Germany



Monday, 10 Feb 2014
====================


Tuesday, 11 Feb 2014
====================


Wednesday, 12 Feb 2014
====================


Thursday, 13 Feb 2014
====================


Friday, 14 Feb 2014
====================
Striesen, Dresden, Germany
2.0km (08:05Uhr - 08:10Uhr)
Seidnitz, Dresden, Germany

Seidnitz, Dresden, Germany
8.8km (08:15Uhr - 08:37Uhr)
Südvorstadt, Dresden, Germany

Südvorstadt, Dresden, Germany
6.3km (08:46Uhr - 09:03Uhr)
01239 Dresden, Germany

01239 Dresden, Germany
7.2km (09:26Uhr - 09:52Uhr)
Striesen, Dresden, Germany



Saturday, 15 Feb 2014
====================
Striesen, Dresden, Germany
6.2km (09:47Uhr - 09:59Uhr)
Innere Neustadt, Dresden, Germany

Innere Neustadt, Dresden, Germany
6.1km (10:54Uhr - 11:08Uhr)
Striesen, Dresden, Germany

Striesen, Dresden, Germany
7.9km (15:07Uhr - 15:22Uhr)
Innere Neustadt, Dresden, Germany

Innere Neustadt, Dresden, Germany
2.9km (16:42Uhr - 16:49Uhr)
Johannstadt, Dresden, Germany

Johannstadt, Dresden, Germany
4.1km (18:24Uhr - 18:34Uhr)
Striesen, Dresden, Germany



Sunday, 16 Feb 2014
====================
Striesen, Dresden, Germany
9.4km (15:19Uhr - 15:43Uhr)
Weißer Hirsch, Dresden, Germany

Weißer Hirsch, Dresden, Germany
5.7km (16:54Uhr - 17:10Uhr)
01309 Dresden, Germany

01309 Dresden, Germany
3.4km (17:43Uhr - 17:49Uhr)
Johannstadt, Dresden, Germany

Johannstadt, Dresden, Germany
4.0km (17:59Uhr - 18:08Uhr)
Striesen, Dresden, Germany



Monday, 17 Feb 2014
====================


Tuesday, 18 Feb 2014
====================


Wednesday, 19 Feb 2014
====================


Thursday, 20 Feb 2014
====================


Friday, 21 Feb 2014
====================
Striesen, Dresden, Germany
2.2km (08:20Uhr - 08:26Uhr)
Seidnitz, Dresden, Germany

Seidnitz, Dresden, Germany
8.2km (08:33Uhr - 08:51Uhr)
Seidnitz, Dresden, Germany
2.2km (08:56Uhr - 09:03Uhr)
Seevorstadt West, Dresden, Germany

Wilsdruffer Vorstadt, Dresden, Germany
6.7km (11:57Uhr - 12:16Uhr)
Striesen, Dresden, Germany

Striesen, Dresden, Germany
1.9km (16:19Uhr - 16:23Uhr)
Seidnitz, Dresden, Germany

Seidnitz, Dresden, Germany
10.4km (16:32Uhr - 16:54Uhr)
Plauen, Dresden, Germany

Plauen, Dresden, Germany
1.5km (18:24Uhr - 18:27Uhr)
Plauen, Dresden, Germany

Plauen, Dresden, Germany
7.6km (18:43Uhr - 18:57Uhr)
Striesen, Dresden, Germany



Saturday, 22 Feb 2014
====================
Striesen, Dresden, Germany
11.0km (10:27Uhr - 10:48Uhr)
Radebeul, Germany

Radebeul, Germany
50.8km (11:05Uhr - 12:57Uhr)
Striesen, Dresden, Germany

Striesen, Dresden, Germany
2.2km (13:03Uhr - 13:09Uhr)
Seidnitz, Dresden, Germany

Seidnitz, Dresden, Germany
3.5km (13:18Uhr - 13:29Uhr)
01239 Dresden, Germany

01239 Dresden, Germany
5.8km (13:49Uhr - 14:04Uhr)
Striesen, Dresden, Germany



Sunday, 23 Feb 2014
====================
Striesen, Dresden, Germany
16.9km (09:51Uhr - 10:17Uhr)
Kesselsdorf, Germany

Kesselsdorf, Germany
23.7km (10:29Uhr - 10:48Uhr)
Kesselsdorf, Germany

Kesselsdorf, Germany
149.5km (13:16Uhr - 14:35Uhr)
03046 Cottbus, Germany



Monday, 24 Feb 2014
====================
03046 Cottbus, Germany
10.6km (07:55Uhr - 08:24Uhr)
03046 Cottbus, Germany

03046 Cottbus, Germany
5.1km (09:30Uhr - 09:44Uhr)
Cottbus, Betriebshof Schmellwitz, Straßenbahnbetriebshof Cottbus, 03044 Cottbus, Germany

Cottbus, Betriebshof Schmellwitz, Straßenbahnbetriebshof Cottbus, 03044 Cottbus, Germany
122.1km (11:02Uhr - 12:38Uhr)
Argenauer Straße 18B, 12555 Berlin, Germany

Argenauer Straße 18B, 12555 Berlin, Germany
128.1km (17:16Uhr - 18:31Uhr)
03046 Cottbus, Germany

03046 Cottbus, Germany
149.3km (19:32Uhr - 20:53Uhr)
Striesen, Dresden, Germany



Tuesday, 25 Feb 2014
====================
Striesen, Dresden, Germany
5.4km (19:33Uhr - 19:41Uhr)
Südvorstadt, Dresden, Germany

Südvorstadt, Dresden, Germany
5.2km (20:50Uhr - 20:58Uhr)
Striesen, Dresden, Germany



Wednesday, 26 Feb 2014
====================
Seidnitz, Dresden, Germany
1.6km (17:19Uhr - 17:32Uhr)
Striesen, Dresden, Germany

Striesen, Dresden, Germany
5.9km (19:20Uhr - 19:33Uhr)
Südvorstadt, Dresden, Germany

Südvorstadt, Dresden, Germany
6.3km (19:46Uhr - 20:01Uhr)
Striesen, Dresden, Germany



Thursday, 27 Feb 2014
====================


Friday, 28 Feb 2014
====================


Save to Excel File

In [27]:
for i in range(len(fahrtenbuchdata)):
    worksheet.write(i+5, 0, fahrtenbuchdata[i])
In [28]:
print('Erledigt. Nun beten und hoffen, dass das Finanzamt es akzeptiert.')
Erledigt. Nun beten und hoffen, dass das Finanzamt es akzeptiert.