|
| 1 | +import logging |
| 2 | +import requests |
| 3 | +import pandas as pd |
| 4 | +from tabulate import tabulate |
| 5 | + |
| 6 | +# Configure logging |
| 7 | +logging.basicConfig(level=logging.INFO, |
| 8 | + format="%(asctime)s [%(levelname)s] %(message)s", |
| 9 | + datefmt="%Y-%m-%d %H:%M:%S") |
| 10 | + |
| 11 | +def download_json_file(url): |
| 12 | + try: |
| 13 | + response = requests.get(url, verify=False) |
| 14 | + response.raise_for_status() |
| 15 | + except requests.exceptions.RequestException as err: |
| 16 | + logging.error(f"HTTP request failed: {err}") |
| 17 | + return None |
| 18 | + |
| 19 | + try: |
| 20 | + data = response.json() |
| 21 | + except ValueError: |
| 22 | + logging.error("Failed to parse JSON") |
| 23 | + return None |
| 24 | + |
| 25 | + return data |
| 26 | + |
| 27 | +def process_data(data): |
| 28 | + # Extract the 'prefixes' and 'ipv6_prefixes' into separate dataframes |
| 29 | + prefixes_df = pd.DataFrame(data['prefixes']) |
| 30 | + ipv6_prefixes_df = pd.DataFrame(data['ipv6_prefixes']) |
| 31 | + |
| 32 | + # Combine both into a single DataFrame |
| 33 | + combined_df = pd.concat([prefixes_df, ipv6_prefixes_df]) |
| 34 | + |
| 35 | + return combined_df |
| 36 | + |
| 37 | +def filter_and_sort(df): |
| 38 | + # Filter rows in the London region |
| 39 | + london_df = df[df['region'] == 'eu-west-2'] |
| 40 | + |
| 41 | + # Filter rows with the AMAZON service |
| 42 | + amazon_df = london_df[london_df['service'] == 'AMAZON'] |
| 43 | + |
| 44 | + # Sort by the column 'ip_prefix' |
| 45 | + amazon_df.sort_values(by=['ip_prefix'], inplace=True) |
| 46 | + |
| 47 | + # Reset the index |
| 48 | + amazon_df.reset_index(drop=True, inplace=True) |
| 49 | + |
| 50 | + return amazon_df |
| 51 | + |
| 52 | +def main(): |
| 53 | + url = "https://ip-ranges.amazonaws.com/ip-ranges.json" |
| 54 | + logging.info(f"Downloading JSON file from {url}") |
| 55 | + data = download_json_file(url) |
| 56 | + |
| 57 | + if data is None: |
| 58 | + logging.error("Failed to download or parse JSON file") |
| 59 | + return |
| 60 | + |
| 61 | + logging.info("Processing data") |
| 62 | + df = process_data(data) |
| 63 | + |
| 64 | + logging.info("Filtering and sorting data") |
| 65 | + final_df = filter_and_sort(df) |
| 66 | + |
| 67 | + # Print the dataframe in a nice format |
| 68 | + print(tabulate(final_df, headers='keys', tablefmt='psql')) |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + main() |
0 commit comments