[python] Shitецо
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- import boto3
- import email.parser as e_parser
- import os
- import requests
- import urllib3
- from time import sleep
- urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
- SLEEP_TIME = 5 # minutes
- SERVICE_NAME =
- REGION_NAME =
- AWS_ACCESS_KEY_ID =
- AWS_SECRET_ACCESS_KEY =
- WEBHOOK_URL =
- CHECKED_ID = []
- s3 = boto3.resource(
- service_name=SERVICE_NAME,
- region_name=REGION_NAME,
- aws_access_key_id=AWS_ACCESS_KEY_ID,
- aws_secret_access_key=AWS_SECRET_ACCESS_KEY
- )
- with open(file='mail_uid.txt', mode='+r') as f:
- for line in f:
- CHECKED_ID.append(line[:-1])
- while True:
- for obj in s3.Bucket().objects.all():
- # format vars
- msg_from = ""
- msg_to = ""
- msg_date = ""
- msg_subjects = ""
- msg_body = ""
- file_id = ""
- file_path = ""
- eTag = obj.key
- if eTag in CHECKED_ID:
- continue
- obj = s3.Bucket().Object(obj.key).get()
- obj = obj['Body']
- obj = obj.read()
- obj = obj.decode()
- parser = e_parser.FeedParser()
- parser.feed(data = obj)
- obj = parser.close()
- message = obj
- for part in message.walk():
- if part.get('from') is not None:
- msg_from = part.get('from')
- if part.get('to') is not None:
- msg_to = part.get('to')
- if part.get('date') is not None:
- msg_date = part.get('date')
- if part.get('Subject') is not None:
- msg_subjects = part.get('Subject')
- if part.get_content_type() == 'text/plain':
- msg_body = part.get_payload()
- # download attachiment
- if part.get_content_maintype() == 'multipart':
- continue
- if part.get('Content-Disposition') is None:
- continue
- file_name = part.get_filename()
- if bool(file_name):
- file_id = part.get('X-Attachment-Id')
- os.mkdir(f"attachiment/{file_id}")
- file_path = os.path.join(f"attachiment/{file_id}/", file_name)
- if not os.path.isfile(file_path) :
- fp = open(file_path, 'wb')
- fp.write(part.get_payload(decode=True))
- fp.close()
- #print("From: " +msg_from)
- #print("To: " +msg_to)
- #print("Subjects: " +msg_subjects)
- #print("Date: " +msg_date)
- #print("Text: " +msg_body)
- #print("File: " +file_path)
- to_send = {
- 'emailaddress':msg_to,
- 'emailsubject':msg_subjects,
- 'emailbody': msg_body,
- 'email-attachmenturl': 'some_str' + file_path
- }
- r = requests.post(WEBHOOK_URL, json=to_send, verify=False)
- with open(file='mail_uid.txt', mode='+a') as f:
- f.writelines(eTag+'\n')
- CHECKED_ID.append(eTag)
- #print(r.text)
- sleep(SLEEP_TIME * 60)
Editor
You can edit this paste and save as new: