Skip to content

Instantly share code, notes, and snippets.

@mariopesch
Created January 3, 2023 15:54
Show Gist options
  • Save mariopesch/a74f4132adb733257882eb2306cd1b9e to your computer and use it in GitHub Desktop.
Save mariopesch/a74f4132adb733257882eb2306cd1b9e to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
openSenseMap Archive Downloader
set Name, BoxID, SensorID and Start Date and Number of Days
"""
import os
import glob
import pandas as pd
import requests
import datetime
#Values to set
date = "2023-01-02"
boxID = "5e98843845f937001cf26c6d"
name = "Nienberge_Garten"
sensorID = "5f0ed1eb987fd4001b9939e3"
#download csv files from archive.opensensemap.org
#save them in the folder "files"
#combine them into one csv file
#set working directory
os.chdir("files")
# initializing date, set start Date
test_date = datetime.datetime.strptime("01-12-2022", "%d-%m-%Y")
# number of days
numberOfDays = 20
date_generated = pd.date_range(test_date, periods=numberOfDays)
print(date_generated.strftime("%d-%m-%Y"))
for date in date_generated:
date = date.strftime("%Y-%m-%d")
url = "https://archive.opensensemap.org/"+date+"/"+boxID+"-"+name+"/"+sensorID+"-"+date+".csv"
with requests.get(url, stream=True) as response:
response.raise_for_status()
with open(sensorID+"-"+date+".csv", "wb") as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
file.flush()
print("Done Downloading")
#find all csv files in the folder
#use glob pattern matching -> extension = 'csv'
#save result in list -> all_filenames
extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
#print(all_filenames)
#combine all files in the list
combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames ])
#export to csv
startDate = date_generated[0].strftime("%d-%m-%Y")
endDate = date_generated[numberOfDays-1].strftime("%d-%m-%Y")
combined_csv.to_csv(sensorID+"-"+name+"-"+startDate+"-"+endDate+".csv", index=False, encoding='utf-8-sig')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment