Download scraper-scripts/CheckReport.py from CashlessConsumer/chennai-floods-raw: direct link, hf CLI and curl.
- Browser
- Download file 5.35 kB
-
https://huggingface.co/datasets/CashlessConsumer/chennai-floods-raw/resolve/main/scraper-scripts/CheckReport.py
- Command line
-
hf download hf://datasets/CashlessConsumer/chennai-floods-raw/scraper-scripts/CheckReport.py
-
curl -L -o CheckReport.py https://huggingface.co/datasets/CashlessConsumer/chennai-floods-raw/resolve/main/scraper-scripts/CheckReport.py
5.35 kB
| import requests | |
| import os | |
| from datetime import datetime | |
| from datetime import datetime, timedelta | |
| def generate_filenames_for_hour_range(date, start_hour, end_hour): | |
| filenames = [] | |
| microseconds_in_hour = 3600 * 1000000 # Total microseconds in an hour | |
| current_datetime = date.replace(hour=0, minute=0, second=0, microsecond=0) | |
| for hour in range(start_hour, end_hour + 1): | |
| #current_datetime = date.replace(hour=hour, minute=0, second=0, microsecond=0) | |
| for microsec in range(microseconds_in_hour): | |
| # Create datetime object with the specific microsecond within the hour | |
| #current_datetime = date.replace(hour=hour, minute=0, second=0, microsecond=microsec) | |
| current_datetime = date.replace(microsecond=microsec + (hour * 60 * 60 * 1000000)) | |
| # Formatting the date according to the required pattern | |
| formatted_date = current_datetime.strftime("%d%m%Y%H%M%S%f")[:-3] # Truncate microseconds to 3 digits | |
| date_str = current_datetime.strftime("%d-%m-%Y") | |
| # Generating the filename and adding it to the list | |
| filename = f"{formatted_date}Bulletin_1.0_Observed Rainfall_{date_str}.pdf" | |
| filenames.append(filename) | |
| return filenames | |
| # Rest of the code remains unchanged, using generate_filenames_for_hour_range in download_for_day | |
| def download_for_day_and_hour_range(date, start_hour, end_hour, url): | |
| try: | |
| filenames_for_hour_range = generate_filenames_for_hour_range(date, start_hour, end_hour) | |
| for filename in filenames_for_hour_range: | |
| print(filename) | |
| if check_and_download(url, filename): | |
| print(f"Download successful for {filename}") | |
| break # Break the loop if file is downloaded successfully | |
| else: | |
| print("Download failed for all filenames") | |
| except Exception as e: | |
| print(f"Error during download: {e}") | |
| def generate_filenames_for_day(date): | |
| filenames = [] | |
| milliseconds_in_day = 1000000 # Total microseconds in a day | |
| current_datetime = date.replace(hour=0, minute=0, second=0, microsecond=0) | |
| for millisec in range(milliseconds_in_day): | |
| # Create datetime object with the specific microsecond within the day | |
| current_datetime = date.replace(microsecond=millisec) | |
| # Formatting the date according to the required pattern | |
| formatted_date = current_datetime.strftime("%d%m%Y%H%M%S%f")[:-3] # Truncate microseconds to 3 digits | |
| date_str = current_datetime.strftime("%d-%m-%Y") | |
| # Generating the filename and adding it to the list | |
| filename = f"{formatted_date}Bulletin_1.0_Observed Rainfall_{date_str}.pdf" | |
| filenames.append(filename) | |
| return filenames | |
| def check_and_download(url, filename): | |
| """ | |
| Checks if the URL returns a file and downloads it to the specified filename. | |
| Args: | |
| url: The URL to check. | |
| filename: The name of the file to save the downloaded content to. | |
| Returns: | |
| True if the file was downloaded, False otherwise. | |
| """ | |
| try: | |
| # Send a HEAD request to get the headers without downloading the content | |
| url = url + "?" + filename | |
| response = requests.head(url) #, headers=headers) | |
| if response.status_code == 200: | |
| # Get the file size | |
| file_size = int(response.headers.get("Content-Length", 0)) | |
| if file_size > 0: | |
| print(f"File size is : {file_size}") | |
| # Download the file and save it | |
| with open(filename, "wb") as f: | |
| response = requests.get(url, stream=True) | |
| for chunk in response.iter_content(chunk_size=1024): | |
| f.write(chunk) | |
| print(f"Downloaded file: {filename}") | |
| return True | |
| else: | |
| print(f"File size is zero: {url}") | |
| else: | |
| print(f"Error accessing URL: {url} ({response.status_code})") | |
| except Exception as e: | |
| print(f"Error downloading file: {e}") | |
| return False | |
| def download_for_day(date, url): | |
| try: | |
| filenames_for_day = list(set(generate_filenames_for_day(date))) | |
| for filename in filenames_for_day: | |
| print(filename) | |
| # Download file using generated filename and provided URL | |
| if check_and_download(url, filename): | |
| print(f"Download successful for {filename}") | |
| break # Break the loop if file is downloaded successfully | |
| else: | |
| print("Download failed for all filenames") | |
| except Exception as e: | |
| print(f"Error during download: {e}") | |
| # Example usage with a date, start hour, end hour, and the URL | |
| given_date = datetime(2023, 12, 3) # Replace with your specific date | |
| given_start_hour = 8 # Replace with your specific start hour | |
| given_end_hour = 12 # Replace with your specific end hour | |
| url_to_download = "https://www.chennaifloodsdss.in/HomePage/DownloadImagesFromAlert/0" # Replace with the actual URL | |
| download_for_day_and_hour_range(given_date, given_start_hour, given_end_hour, url_to_download) | |
| # # Example usage with a date and the URL | |
| # given_date = datetime(2023, 12, 3) # Replace with your specific date | |
| # url_to_download = "https://www.chennaifloodsdss.in/HomePage/DownloadImagesFromAlert/0" # Replace with the actual URL | |
| # download_for_day(given_date, url_to_download) |