Skip to content

Instantly share code, notes, and snippets.

@d0rb
Created March 20, 2024 10:12
Show Gist options
  • Save d0rb/ed96de8bffc56f7cf40fb8cc06c4c07a to your computer and use it in GitHub Desktop.
Save d0rb/ed96de8bffc56f7cf40fb8cc06c4c07a to your computer and use it in GitHub Desktop.
The check_gmail.py script is designed to determine the validity of a provided email address by checking if it has a public Google Calendar associated with it. The script achieves this by sending an HTTP request to the corresponding Google Calendar URL and analyzing the response. Based on the response received, the script provides insight into wh…
import sys
import requests
from bs4 import BeautifulSoup
def check_calendar_url(email):
url = f"https://calendar.google.com/calendar/ical/{email}/public/basic.ics"
try:
response = requests.get(url)
if response.status_code == 200:
print("The email address is valid!")
elif response.status_code == 404:
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.title.string if soup.title else ""
if title == "Error 404 (Bad Request)!!1":
print("The email address is valid but the calendar is not public.")
else:
print("The email address is invalid and does not exist.")
else:
print(f"Failed to fetch calendar URL. Status code: {response.status_code}")
except requests.RequestException as e:
print(f"Error fetching calendar URL: {e}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py <email>")
sys.exit(1)
email = sys.argv[1]
check_calendar_url(email)
@ranlo
Copy link

ranlo commented Mar 20, 2024

another difference - if the account exists (regardless of whether the calendar is public or private), you are going to get
x-frame-options: SAMEORIGIN
in the response headers

@d0rb
Copy link
Author

d0rb commented Mar 20, 2024

another difference - if the account exists (regardless of whether the calendar is public or private), you are going to get x-frame-options: SAMEORIGIN in the response headers

Nice , here's the updated script ( it's much better checking the headers then the title I guess )


import sys
import requests

def check_calendar_url(email):
    url = f"https://calendar.google.com/calendar/ical/{email}/public/basic.ics"
    try:
        response = requests.head(url)  # Send a HEAD request to fetch only the headers
        x_frame_options = response.headers.get('X-Frame-Options', '').upper()
        if x_frame_options == 'SAMEORIGIN':
            print("The email address is valid!")
        elif not x_frame_options:
            print("The email address is invalid and does not exist.")
        else:
            print("The email address is valid but the calendar is not public.")
    except requests.RequestException as e:
        print(f"Error fetching calendar URL: {e}")

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: python script.py <email>")
        sys.exit(1)
    email = sys.argv[1]
    check_calendar_url(email)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment