Created
March 14, 2021 20:57
-
-
Save gandroz/5324acf9d2125167609217dbe748841b to your computer and use it in GitHub Desktop.
IP sender script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pickle | |
import os.path | |
import requests | |
from googleapiclient.discovery import build | |
from google_auth_oauthlib.flow import InstalledAppFlow | |
from google.auth.transport.requests import Request | |
from email.mime.text import MIMEText | |
import base64 | |
# If modifying these scopes, delete the file token.pickle. | |
SCOPES = ['https://www.googleapis.com/auth/gmail.send'] | |
PATH_TO_IP_FILE = ".....txt" | |
CREDS = "....json" | |
TOKEN = ".....pickle" | |
EMAIL = "[email protected]" | |
def check_ip(): | |
r = requests.get("https://api.myip.com/") | |
current_ip = r.json()['ip'] | |
message = f"{current_ip}" | |
with open(PATH_TO_IP_FILE, "r") as f: | |
stored_ip = f.readline().strip() | |
if stored_ip != current_ip: | |
print(f"Change IP to {current_ip}") | |
with open(PATH_TO_IP_FILE, "w") as f: | |
f.write(current_ip) | |
return current_ip | |
else: | |
print(f"Same IP {current_ip}") | |
return None | |
def create_message(message_text): | |
"""Create a message for an email. | |
Args: | |
message_text: The text of the email message. | |
Returns: | |
An object containing a base64url encoded email object. | |
""" | |
message = MIMEText(message_text) | |
message['to'] = EMAIL | |
message['from'] = EMAIL | |
message['subject'] = "IP" | |
encoded_message = base64.urlsafe_b64encode(message.as_bytes()) | |
return {'raw': encoded_message.decode()} | |
def send_email(new_ip): | |
"""Shows basic usage of the Gmail API. | |
Lists the user's Gmail labels. | |
""" | |
creds = None | |
# The file token.pickle stores the user's access and refresh tokens, and is | |
# created automatically when the authorization flow completes for the first | |
# time. | |
if os.path.exists(TOKEN): | |
with open(TOKEN, 'rb') as token: | |
creds = pickle.load(token) | |
# If there are no (valid) credentials available, let the user log in. | |
if not creds or not creds.valid: | |
if creds and creds.expired and creds.refresh_token: | |
creds.refresh(Request()) | |
else: | |
flow = InstalledAppFlow.from_client_secrets_file(CREDS, SCOPES) | |
creds = flow.run_local_server(port=0) | |
# Save the credentials for the next run | |
with open(TOKEN, 'wb') as token: | |
pickle.dump(creds, token) | |
service = build('gmail', 'v1', credentials=creds) | |
try: | |
message = create_message(new_ip) | |
message = service.users().messages().send(userId='me', body=message).execute() | |
except Exception as error: | |
print(f'An error occurred: {error}') | |
if __name__ == '__main__': | |
current_ip = check_ip() | |
if current_ip is not None: | |
send_email(current_ip) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment