It supports thepiratebay, nyaa, 1337x, libgen, limetorrents and rarbg

  • rumba@lemmy.zip
    link
    fedilink
    English
    arrow-up
    0
    ·
    2 days ago

    So, you don’t know Python at all AND you don’t know Bash, but you feel compelled to talk about how one is so much better than the other?

    1. Python has a lot of compatibility problems, you have to roll the env system and do requirements, and if your distro doesn’t have those packages available, you can’t run the script. Then you can’t always run python 2 code in python 3.

    2. Bash has error handling and try/catch.

    3. Cross platfoming a shell script to run in ba, bash in linux, wsl and mac is fairly straight forward. Worst case, if you don’t have a specific flavor of bash installed, you can always install it. You rely on the posix compliance of the OS binaries to get the job done.

    -------Bash------
    
    #!/usr/bin/env bash
    
    API_URL="https://api.example.com/data"
    
    CONNECT_TIMEOUT=5   
    MAX_TIME=10         
    
    response=$(
      curl \
        --silent \
        --write-out "\n%{http_code}" \
        --connect-timeout "$CONNECT_TIMEOUT" \
        --max-time "$MAX_TIME" \
        "$API_URL"
    )
    
    http_body=$(echo "$response" | sed '$d')        
    http_code=$(echo "$response" | tail -n1)
    
    curl_exit_code=$?
    
    if [ $curl_exit_code -ne 0 ]; then
      echo "Error: Failed to connect or timed out (curl exit code $curl_exit_code)."
      exit 1
    fi
    
    if [ -z "$http_code" ]; then
      echo "Error: No HTTP status code received. The request may have timed out or failed."
      exit 1
    fi
    
    echo "HTTP status code: $http_code"
    
    if [ "$http_code" -eq 200 ]; then
      echo "Request successful! Parsing JSON response..."
      echo "$http_body" | jq
    else
      echo "Request failed with status code $http_code. Response body:"
      echo "$http_body"
      exit 1
    fi
    
    exit 0
    
    
    -------------Python-------------
    #!/usr/bin/env python3
    
    import requests
    import sys
    
    def main():
        # API endpoint
        API_URL = "https://api.example.com/data"
    
        # Timeout (in seconds)
        TIMEOUT = 5
    
        try:
            # Make a GET request with a timeout
            response = requests.get(API_URL, timeout=TIMEOUT)
    
            # Check the HTTP status code
            if response.status_code == 200:
                # Parse the JSON response
                try:
                    data = response.json()
                    print("Request successful. Here is the parsed JSON data:")
                    print(data)
                except ValueError as e:
                    print("Error: Could not parse JSON response.", e)
                    sys.exit(1)
            else:
                # Handle non-200 status codes
                print(f"Request failed with status code {response.status_code}.")
                print("Response body:", response.text)
                sys.exit(1)
    
        except requests.exceptions.Timeout:
            print(f"Error: The request timed out after {TIMEOUT} seconds.")
            sys.exit(1)
        except requests.exceptions.RequestException as e:
            # Catch all other requests-related errors (e.g., connection error)
            print(f"Error: An error occurred while making the request: {e}")
            sys.exit(1)
    
    if __name__ == "__main__":
        main()