This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuploaddata.py
66 lines (53 loc) · 2.74 KB
/
uploaddata.py
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
################################################################################################################
# Python3 script to run on the Raspberry Pi upload the latest data to SleepHQ / Dropbox #
# This is a work in progress! #
# v0.3 #
# Written by Erik Reynolds (https://github.com/grumpymaker/sleephq-pi) #
################################################################################################################
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
# Path to the latest data file
dataFilePath = "/home/erik/sleephq-backup/current.zip"
# My SleepHQ Credentials
sleepUsername = ""
sleepPassword = ""
driver = webdriver.Chrome()
driver.get('https://sleephq.com/users/sign_in')
time.sleep(5) # Let the user actually see something!
# Check to see if we're on the login page, else we're already logged in
if driver.current_url == 'https://sleephq.com/users/sign_in':
print("Logging in...")
username_input = driver.find_element(By.ID, 'user_email')
password_input = driver.find_element(By.ID, 'user_password')
submit_button = driver.find_element(By.TAG_NAME, 'button')
username_input.send_keys(sleepUsername)
time.sleep(2)
password_input.send_keys(sleepPassword)
time.sleep(2)
submit_button.click()
time.sleep(5) # give it a second to login and redirect
# We should be logged in now and on the dashboard page, grab the URL to check (and extract the teams ID)
dashboardURL = driver.current_url
print("Dashboard URL: " + dashboardURL)
# Check the dashboard URL to see if it matches the expected format (https://sleephq.com/account/teams/123456)
if not dashboardURL.startswith('https://sleephq.com/account/teams/'):
print("ERROR: Unexpected URL format, expected https://sleephq.com/account/teams/123456")
driver.quit()
exit()
# Extract the teams ID from the end of the URL
teamID = dashboardURL.split('/')[-1]
print("teams ID: " + teamID)
# Now we can go to the upload page
driver.get('https://sleephq.com/account/teams/' + teamID + '/imports')
fileUploadField = driver.find_element(By.XPATH, '//input[@type="file" and @class="dz-hidden-input"]')
fileUploadField.send_keys(dataFilePath)
time.sleep(5) # give it a few to grab the file
print("Uploading the datafile...")
beginUploadButton = driver.find_element(By.ID, 'start-upload-button')
beginUploadButton.click()
# Give it 60 seconds to upload the file before quitting
time.sleep(60)
print("Done! Closing the browser...")
driver.quit()