-
Notifications
You must be signed in to change notification settings - Fork 0
/
magnet-handler.py
executable file
·89 lines (73 loc) · 3.24 KB
/
magnet-handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python3
#import os
#import sys
import getpass
import smtplib
#from email import encoders
#from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from urllib.parse import unquote
from gi.repository import Notify
def sendmail(server, port, recipients, msg):
# with smtplib.SMTP(server, port) as s:
## FIXME: ^ that syntax didn't work on Kaylee, suspect Python version mismatch is to blame
s = smtplib.SMTP(server, port)
s.ehlo()
s.starttls()
s.ehlo()
s.sendmail(msg['From'], recipients, msg.as_string())
s.close()
def genmail(sender, recipient, magnets):
outer = MIMEMultipart()
outer['To'] = recipient
outer['From'] = sender
if len(magnets) > 1:
outer['Subject'] = "{} magnet links".format(len(magnets))
elif len(magnets) == 1:
for i in magnets[0].split('&'):
if i.startswith('dn='):
outer['Subject'] = unquote(i[3:])
if not outer['Subject']:
# Magnet links aren't required to have the 'dn' attribute
outer['Subject'] = "1 magnet link"
outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
outer.attach(MIMEText(
'\n'.join(magnets), 'plain'
))
## FIXME: Future improvement, handle torrent files as well.
# # List of attachments
# attachments = ['FULL PATH TO ATTACHMENTS HERE']
#
# # Add the attachments to the message
# for file in attachments:
# try:
# with open(file, 'rb') as fp:
# msg = MIMEBase('application', "octet-stream")
# msg.set_payload(fp.read())
# encoders.encode_base64(msg)
# msg.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file))
# outer.attach(msg)
# except:
# print("Unable to open one of the attachments. Error: ", sys.exc_info()[0])
# raise
return outer
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description="Email magnet links for automatic torrenting",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
username = getpass.getuser()
# FIXME: Use a config file as well
parser.add_argument('magnets', metavar='MAGNET-LINK', help="The magnet link you wish to email", nargs='+')
parser.add_argument('--server', '-s', metavar='SMTP-SERVER', help="Specify an alternate SMTP server", default=REPLACEME)
parser.add_argument('--port', '-p', metavar='SMTP-PORT', help="Specify an alternate SMTP port", default=25)
parser.add_argument('--from', '-f', metavar='FROM', help="Specify who the email will be from", default=username, dest='sender')
parser.add_argument('--recipient','-r', metavar='RECIPIENT', help="Specify who the email will go to", default=REPLACEME)
args = parser.parse_args()
assert all(i.startswith('magnet:') for i in args.magnets), "Magnet links must start with 'magnet:'"
msg = genmail(args.sender, args.recipient, args.magnets)
sendmail(args.server, args.port, args.recipient, msg)
Notify.init(parser.prog)
Notify.Notification.new(parser.prog,"Sent email '{}'".format(msg['Subject']), 'mail-send').show()