[python] Shitецо

Viewer

copydownloadembedprintName: Shitецо
  1. import boto3
  2. import email.parser as e_parser
  3. import os
  4. import requests
  5. import urllib3
  6. from time import sleep
  7.  
  8. urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
  9.     
  10. SLEEP_TIME = 5 # minutes
  11. SERVICE_NAME = 
  12. REGION_NAME = 
  13. AWS_ACCESS_KEY_ID =
  14. AWS_SECRET_ACCESS_KEY = 
  15.  
  16. WEBHOOK_URL = 
  17.  
  18. CHECKED_ID = []
  19.  
  20. s3 = boto3.resource(
  21.     service_name=SERVICE_NAME,
  22.     region_name=REGION_NAME,
  23.     aws_access_key_id=AWS_ACCESS_KEY_ID,
  24.     aws_secret_access_key=AWS_SECRET_ACCESS_KEY
  25. )
  26.  
  27. with open(file='mail_uid.txt', mode='+r') as f:
  28.     for line in f:
  29.         CHECKED_ID.append(line[:-1])
  30.     
  31.  
  32. while True:
  33.     for obj in s3.Bucket().objects.all():
  34.  
  35.         # format vars
  36.         msg_from = ""
  37.         msg_to = ""
  38.         msg_date = ""
  39.         msg_subjects = ""
  40.         msg_body = ""
  41.         file_id = ""
  42.         file_path = ""
  43.  
  44.         eTag = obj.key
  45.         if eTag in CHECKED_ID:
  46.             continue
  47.  
  48.         obj = s3.Bucket().Object(obj.key).get()
  49.  
  50.         obj = obj['Body']
  51.         obj = obj.read()
  52.         obj = obj.decode()
  53.  
  54.         parser = e_parser.FeedParser()
  55.         parser.feed(data = obj)
  56.         obj = parser.close()
  57.         message = obj
  58.  
  59.         for part in message.walk():
  60.             if part.get('from') is not None:
  61.                 msg_from = part.get('from')
  62.  
  63.             if part.get('to') is not None:
  64.                 msg_to = part.get('to')
  65.  
  66.             if part.get('date') is not None:
  67.                 msg_date = part.get('date')
  68.  
  69.             if part.get('Subject') is not None:
  70.                 msg_subjects = part.get('Subject')
  71.  
  72.             if part.get_content_type() == 'text/plain':
  73.                 msg_body = part.get_payload()
  74.  
  75.  
  76.             # download attachiment
  77.             if part.get_content_maintype() == 'multipart':
  78.                 continue
  79.             if part.get('Content-Disposition') is None:
  80.                 continue
  81.             file_name = part.get_filename()        
  82.             if bool(file_name):
  83.                 file_id = part.get('X-Attachment-Id')
  84.                 os.mkdir(f"attachiment/{file_id}")
  85.                 file_path = os.path.join(f"attachiment/{file_id}/", file_name)
  86.                 if not os.path.isfile(file_path) :
  87.                     fp = open(file_path, 'wb')
  88.                     fp.write(part.get_payload(decode=True))
  89.                     fp.close()
  90.  
  91.         #print("From: " +msg_from)
  92.         #print("To: " +msg_to)
  93.         #print("Subjects: " +msg_subjects)
  94.         #print("Date: " +msg_date)
  95.         #print("Text: " +msg_body)
  96.         #print("File: " +file_path)
  97.  
  98.         to_send = {
  99.             'emailaddress':msg_to,
  100.             'emailsubject':msg_subjects,
  101.             'emailbody': msg_body,
  102.             'email-attachmenturl''some_str' + file_path
  103.         }
  104.         r = requests.post(WEBHOOK_URL, json=to_send, verify=False)
  105.  
  106.         with open(file='mail_uid.txt', mode='+a') as f:
  107.             f.writelines(eTag+'\n')
  108.             CHECKED_ID.append(eTag)
  109.         
  110.         #print(r.text)
  111.  
  112.     sleep(SLEEP_TIME * 60)
  113.  

Editor

You can edit this paste and save as new:


File Description
  • Shitецо
  • Paste Code
  • 18 Jun-2021
  • 3.03 Kb
You can Share it: