Skip to content

Instantly share code, notes, and snippets.

@Haolicopter
Created June 15, 2017 21:37
Show Gist options
  • Save Haolicopter/63120de792d4c79aead7d3e98433106e to your computer and use it in GitHub Desktop.
Save Haolicopter/63120de792d4c79aead7d3e98433106e to your computer and use it in GitHub Desktop.
Send magent link through email to remote machine to start auto download
#!/usr/bin/env python3
# One chanllenge remains:
# 1. how to check if the download is completed and send a notification.
# Chanllenges solved:
# how to extract the magent link inside gamil from werid additional links.
# Write a program that checks an email acount every 15 minutes for
# any instructions you email it and executes those instructions automatically.
# For example, BitTorrent is a peer-to-peer downloading system.
# Using free BitTorrent software such as qBittorrent,
# you can download large media files on your home computer.
# If you email the program a BitTorrent link, the program will eventually
# check its email, find this message, extract the link,
# and then launch qBittorrent to start downloading the file.
# This way, you can have your home computer begin downloads while you're away,
# and the download can be finished by the time you return home.
# Of course, you'll want the program to make sure the emails come from you.
# In particular, you might want to require that the emails contain a password,
# Since it is fairly trivial for hackers to fake a "from" address in emails.
# The program should delete the emails it finds
# so that it doesn't repeat instructions every time it checks the email account
# As an extra feature, have the program email
# or text you a confirmation every time it executes a command.
# Since you won't be sitting in front of the computer
# that is running the program, it's a good idea to use the logging functions
# to write a text file log that you can check if errors come up.
# The wait() method call will block until qBittorrent has stopped,
# and then your program can email or text you a notification
# that the download has completed.
import imaplib
import email
import email.header
import sys
import re
import os
import subprocess
import time
import pyautogui
# Config section
botEmail = ''
botEmailAppPass = ''
botEmailFolder = '"[Gmail]/All Mail"'
botEmailFilter = 'TEXT jarvis'
masterEmail = ''
masterCode = 'hey'
# End of config section
mail = imaplib.IMAP4_SSL('imap.gmail.com')
try:
rv, data = mail.login(botEmail, botEmailAppPass)
except imaplib.IMAP4.error:
print("LOGIN FAILED!!! ")
sys.exit(1)
rv, data = mail.select(botEmailFolder, readonly=False)
if rv == 'OK':
print('Processing mailbox...')
else:
print('ERROR: Unable to open mailbox ', rv)
mail.logout()
sys.exit(1)
rv, data = mail.search(None, botEmailFilter)
for num in data[0].split():
print(os.linesep, end='')
print('Processing email id = ', num, '...', sep='')
rv, data = mail.fetch(num, '(RFC822)')
if rv != 'OK':
print("ERROR getting message", num)
continue
msg = email.message_from_bytes(data[0][1])
for part in msg.walk():
# each part is a either non-multipart, or another multipart message
# that contains further parts... Message is organized like a tree
if part.get_content_type() == 'text/plain':
body = part.get_payload()
senderName, senderEmail = email.utils.parseaddr(msg['From'])
# Check for identity
if masterCode in msg['Subject'] and senderEmail == masterEmail:
print('Found a message from Master.')
else:
print('Unidentified sender, abort.')
continue
# Extract the bit torrent magent link
# Get ride of werid links Gmail puts in magent link
# like <http://2ftracker.leechers-paradise.org/>
magentLink = re.sub(
'<https?:\/\/([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?>',
'',
body)
print(magentLink)
# TODO: Send a text message to me when it starts
# Launch bit torret
bitTorrent = subprocess.Popen(['open', magentLink])
# Wait for the bit torrent client to load
time.sleep(3)
# Press enter to confirm the download
pyautogui.typewrite(['enter'])
print('Start downloading...')
# TODO: Send a text message to me when it finishes
# Delete the email after it's done
mail.store(num, '+X-GM-LABELS', '\\Trash')
mail.expunge()
mail.close()
mail.logout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment