]>
git.rmz.io Git - dotfiles.git/blob - isync/parse-mail.py
3 from datetime
import timedelta
4 from email
import message_from_file
10 FROM
= "{uploader} <noreply@youtube.com>"
13 {uploader} just uploaded a video
19 ───────────────────────────────────────────────────────────────────────
23 ───────────────────────────────────────────────────────────────────────
25 Manage notifications: https://www.youtube.com/subscription_manager
30 msg
= message_from_file(fd
)
33 if msg
.is_multipart():
34 for part
in msg
.get_payload():
35 if part
.get_content_type() != 'text/plain':
37 charset
= part
.get_content_charset()
38 payload
= part
.get_payload(decode
=True).decode(charset
)
40 payload
= msg
.get_payload(decode
=True).decode(charset
)
42 url
= re
.search(r
'^https?://www.youtube.com/watch\?v=[\w-]{11}', payload
, re
.MULTILINE
).group()
44 with youtube_dl
.YoutubeDL({'quiet' : True}
) as ytdl
:
45 info
= ytdl
.extract_info(url
, download
=False, force_generic_extractor
=True)
50 info
['duration'] = timedelta(seconds
=info
['duration'])
53 msg
['From'] = FROM
.format(**info
)
56 msg
['Subject'] = SUBJECT
.format(**info
)
58 msg
.set_payload(CONTENT
.format(**info
))
63 if __name__
== "__main__":
64 if len(sys
.argv
) == 1:
65 msg
= parse_file(sys
.stdin
)
67 with open(sys
.argv
[1], 'r') as fd
: