Created
May 13, 2020 01:06
-
-
Save tuananhle7/28f0e57ed12cba544f0f90dc085ad78b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Download data from RescueTime. | |
Inspired by https://gist.github.com/veekas/2fe3cf30fe6375f5d121c6372a550000 | |
Also see https://www.rescuetime.com/anapi/setup/documentation | |
""" | |
import urllib.parse | |
import urllib.request | |
import requests | |
from tqdm import tqdm | |
def download(date_start, date_end, api_key): | |
"""Download data in an interval (inclusive). | |
Format: YYYY-MM-DD | |
Saves to {date_start}-{date_end}.csv | |
""" | |
params = urllib.parse.urlencode( | |
{ | |
"key": api_key, | |
"perspective": "interval", | |
"resolution_time": "hour", | |
"format": "csv", | |
"restrict_begin": date_start, | |
"restrict_end": date_end, | |
} | |
) | |
url = f"https://www.rescuetime.com/anapi/data?{params}" | |
print(f"Downloading for interval {date_start} to {date_end} from\n{url}") | |
save_path = f"{date_start}-{date_end}.csv" | |
# Copied from | |
# https://stackoverflow.com/questions/37573483/progress-bar-while-download-file-over-http-with-requests | |
# Streaming, so we can iterate over the response. | |
r = requests.get(url, stream=True) | |
# Total size in bytes. | |
total_size = int(r.headers.get("content-length", 0)) | |
block_size = 1024 # 1 Kibibyte | |
progress = tqdm(total=total_size, unit="iB", unit_scale=True) | |
with open(save_path, "wb") as f: | |
for data in r.iter_content(block_size): | |
progress.update(len(data)) | |
f.write(data) | |
progress.close() | |
if total_size != 0 and progress.n != total_size: | |
print("ERROR, something went wrong") | |
else: | |
print(f"Data saved to {save_path}") | |
def get_year_month_start(year_end, month_end): | |
if month_end - 3 <= 0: | |
year_start = year_end - 1 | |
else: | |
year_start = year_end | |
month_start = (month_end - 3) % 12 | |
if month_start == 0: | |
month_start = 12 | |
return year_start, month_start | |
def main(): | |
# get from https://www.rescuetime.com/anapi/manage | |
api_key = "FILL ME" | |
year_end = 2020 | |
month_end = 5 | |
day = 11 | |
for _ in range(20): | |
year_start, month_start = get_year_month_start(year_end, month_end) | |
download( | |
f"{year_start}-{month_start:02}-{day + 1:02}", | |
f"{year_end}-{month_end:02}-{day:02}", | |
api_key, | |
) | |
year_end, month_end = year_start, month_start | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment