Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export from TeslaFi #563

Closed
roopesh opened this issue Apr 7, 2020 · 2 comments
Closed

Export from TeslaFi #563

roopesh opened this issue Apr 7, 2020 · 2 comments
Labels

Comments

@roopesh
Copy link

roopesh commented Apr 7, 2020

I am going to give TeslaMate a try ... just based on it's Home Assistant integration. However, I found myself wanting to import all our TeslaFi data.

I have a ton of TeslaFi data for two cars. Rather than dealing with that UI, I spent way more time creating a python script to export the data.

BTW I wrote this for my needs and it worked. YMMV, I didn't try to add error handling or anything fancy.

Requirements: python3, lxml, requests

Instructions:

  1. Copy all of the below into VSCode or something that respects indents
  2. Change the username and password to your teslafi login credentials
  3. Edit the years and months of data you want to export
  4. Save this file (e.g. teslafi_data_export.py) to the folder you want your exported files to live
  5. Run the file (e.g. python3 teslafi_data_export.py)
import requests
from lxml.html import fromstring, document_fromstring, tostring

username = '<your teslafi username>'
password = '<your teslafi password>'
years = [2016, 2017, 2018, 2019, 2020] # array of years you want to export
months = [1,2,3,4,5,6,7,8,9,10,11,12] # I assume all the months, up to you
cookie = ''
def login():
  url = "https://teslafi.com/userlogin.php"
  response = requests.request("GET", url, headers={}, data={})
  
  #print(response.cookies.keys())
  cookies = ""
  for key in response.cookies.keys():
    this_cookie = key + "=" + response.cookies.get(key)
    if cookies == "":
      cookies = this_cookie
    else:
      cookies += "; " + this_cookie
    #print (cookies)
  token = fromstring(response.text).forms[0].fields['token']
  #print("token: " + token)
  #print("cookies: " + cookies)
  global cookie
  cookie = cookies
  payload = {'username': username,'password': password,'remember': '1','submit': 'Login','token': token}
  headers = {"Cookie": cookies}
  l = requests.request("POST", url, headers=headers, data=payload)
  #print(l.status_code)
  return True

def getdata(m,y):
  url = "https://teslafi.com/exportMonth.php"
  headers = {'Content-Type': 'application/x-www-form-urlencoded','Cookie': cookie}
  response = requests.request("POST", url, headers=headers, data=pl(m,y))
  return response

def savefile(response, m, y):
  file = open(fname(m,y), "wb")
  file.write(response.text.encode('utf-8'))
  file.close()
  return 

def fname(m,y):
  return("TeslaFi" + str(m) + str(y) + ".csv")

def pl(m,y):
  url = 'https://teslafi.com/export2.php'
  response = requests.request("GET", url, headers={"Cookie": cookie})
  magic = fromstring(response.text).forms[0].fields['__csrf_magic']
  return('__csrf_magic=' + magic + '&Month=' + str(m) + '&Year=' + str(y))

def go():
  login()
  for year in years:
    for month in months:
      print('Processing: ' + str(month) + '/' + str(year))
      d = getdata(month, year)
      savefile(d, month, year)
      print('Saved: ' + fname(month, year))

go()

@roopesh roopesh added the kind:idea Idea for new feature or some form of enhancement label Apr 7, 2020
@adriankumpf adriankumpf added guide and removed kind:idea Idea for new feature or some form of enhancement labels Apr 7, 2020
@stale
Copy link

stale bot commented Jun 6, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Stale label Jun 6, 2020
@adriankumpf adriankumpf removed the Stale label Jun 8, 2020
@stale
Copy link

stale bot commented Aug 7, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Stale label Aug 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants