Download scraper-scripts/GetRainfallData.py from CashlessConsumer/chennai-floods-raw: direct link, hf CLI and curl.
- Browser
- Download file 2.51 kB
-
https://huggingface.co/datasets/CashlessConsumer/chennai-floods-raw/resolve/main/scraper-scripts/GetRainfallData.py
- Command line
-
hf download hf://datasets/CashlessConsumer/chennai-floods-raw/scraper-scripts/GetRainfallData.py
-
curl -L -o GetRainfallData.py https://huggingface.co/datasets/CashlessConsumer/chennai-floods-raw/resolve/main/scraper-scripts/GetRainfallData.py
2.51 kB
| import os | |
| import pandas as pd | |
| import requests | |
| import json | |
| from datetime import datetime, timedelta | |
| def get_rainfall_day(date_time, location): | |
| # Replace this with your logic to fetch rainfall data for a specific date and time in the given location | |
| print(f"Fetching rainfall data for {date_time} in {location}") | |
| def loop_through_dates(start_date, end_date, location): | |
| date_format = "%Y-%m-%d %H-%M-%S" # Define your date format | |
| current_date = datetime.strptime(start_date, date_format) | |
| end_date = datetime.strptime(end_date, date_format) | |
| while current_date <= end_date: | |
| # Invoke the method for each date | |
| get_rainfall_day(current_date.strftime(date_format), location) | |
| current_date += timedelta(days=1) # Increment date by 1 day | |
| def get_rainfall_day(date,filename): | |
| url = "https://www.chennaifloodsdss.in/Master/GetRainfallBasedondistrict" | |
| params = {"district":filename, "datetime": date} | |
| response = requests.request("GET", url, params=params) | |
| data = json.loads(json.loads(response.text)) | |
| if data not in ("", [], None): | |
| with open("data/" + filename + "_" + date + ".json", 'w') as file: | |
| json.dump(data, file, indent=4) | |
| def merge_data(): | |
| # Folder containing JSON files | |
| folder_path = './data' | |
| # List to store individual DataFrames | |
| data_frames = [] | |
| # Loop through each file in the folder | |
| for filename in os.listdir(folder_path): | |
| if filename.endswith('.json'): | |
| file_path = os.path.join(folder_path, filename) | |
| # Read JSON file into a DataFrame | |
| with open(file_path, 'r') as file: | |
| json_content = pd.read_json(file) | |
| data_frames.append(json_content) | |
| # Concatenate all DataFrames into a single DataFrame | |
| combined_df = pd.concat(data_frames, ignore_index=True) | |
| # Export combined DataFrame to a single JSON file | |
| combined_json_file = 'combined_data.json' | |
| combined_df.to_json(combined_json_file, orient='records') | |
| print(f"Combined data exported to '{combined_json_file}'") | |
| def main(): | |
| # start_date = "2021-11-03 09-30-00" | |
| # end_date = "2023-12-12 09-30-00" | |
| start_date = "2022-11-03 09-30-00" | |
| end_date = "2023-11-03 09-30-00" | |
| # locations = ["Kancheepuram", "Ranipet", "Chengalpattu", "Tiruvallur"] | |
| # location = "Chennai" | |
| # for location in locations: | |
| # loop_through_dates(start_date, end_date, location) | |
| merge_data() | |
| if __name__ == "__main__": | |
| main() |