Skip to content

Instantly share code, notes, and snippets.

@naterh
Created January 10, 2017 07:04
Show Gist options
  • Save naterh/4d58814fc5b8f18c45404b9ac28099eb to your computer and use it in GitHub Desktop.
Save naterh/4d58814fc5b8f18c45404b9ac28099eb to your computer and use it in GitHub Desktop.
Helper util for getting Dex files into Dexlink
#!/usr/bin/env python
#
# Helper util for removing SD1* line from emailed DEX files & uploading to Dexlink
#
import base64
import email
import sys
import smtplib
import imaplib
import getpass
import datetime
import commonregex
import requests
import time
from os import listdir
from os.path import isfile, join
from time import sleep
from lxml import html
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.message import MIMEMessage
DEBUG = False
AUTO_SKIP = True
IMAP_SERVER = 'imap.gmail.com'
EMAIL_ACCOUNT = "[email protected]"
EMAIL_FOLDER = "DexFiles"
OUTPUT_DIRECTORY = './'
PASSWORD = 'OMGawdzLolLol'
def is_base64(s):
s = ''.join([s.strip() for s in s.split("\n")])
try:
enc = base64.b64encode(base64.b64decode(s)).strip()
return enc == s
except TypeError:
return False
def upload_dex(filename):
USER = "LolLol"
PWORD = "Y-Right"
LOGIN_URL = "https://dexlink.paylabnetworks.com/auth/login"
URL = "https://dexlink.paylabnetworks.com/import/upload"
URL2 = "https://dexlink.paylabnetworks.com/import"
session_requests = requests.session()
result = session_requests.get(LOGIN_URL)
tree = html.fromstring(result.text)
auth_token = list(set(tree.xpath("//input[@name='_token']/@value")))[0]
payload = {
"email": USER,
"password": PWORD,
"_token": auth_token
}
# Perform login
result = session_requests.post(LOGIN_URL, data=payload, headers=dict(referer = LOGIN_URL))
if result.ok:
print('Processing file {}'.format(filename))
with open(filename, 'rb') as f:
filez = {'file': (filename, f)}
pload = {
"_token": auth_token,
"imei": "OP88",
"type": "update",
}
# Scrape url
result = session_requests.post(URL, data=pload, files=filez, headers=dict(referer=URL2))
if not result.ok:
print('Something went terribly wrong!')
print(result.text)
sys.exit(1)
print('Yippee! Its alive!')
sleep(5)
def process_mailbox(M):
"""
Dump all dex emails in the folder to files in output directory.
"""
mails = []
date = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%d-%b-%Y")
rv, data = M.search(None, '(SENTSINCE {date})'.format(date=date))
if rv != 'OK':
print "No messages found!"
return
for num in data[0].split():
rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK':
print "ERROR getting message", num
return
mail = email.message_from_string(data[0][1])
payload1 = mail.get_payload()
if isinstance(payload1, list):
payload1 = payload1[0]
if hasattr(payload1, 'get_payload'):
payload1 = payload1.get_payload()
if isinstance(payload1, email.Message.Message):
payload1 = payload1.get_payload()
try:
if is_base64(payload1):
payload1 = base64.b64decode(payload1)
except Exception as e:
print(e)
continue
mails.append(mail)
print('{} - {}'.format(mail['From'], mail['Subject']))
print('Total messages - {}'.format(len(mails)))
mail1 = mail
if mail1.get_content_maintype() == 'multipart':
for part in mail1.walk():
paydata = ''
if part.get_content_maintype == 'multipart': continue
if part.get('Content-Disposition') is None: continue
filename = part.get_filename()
if not filename.startswith('DEX') or filename in os.listdir():
continue
fp = open(filename, 'wb')
for line in part.get_payload(decode=True).split('\n'):
if 'SD1*' not in line:
fp.write(line + '\n')
paydata = paydata + line + '\n'
fp.close()
print '%s saved!' % filename
upload_dex(filename)
def main():
M = imaplib.IMAP4_SSL(IMAP_SERVER)
M.login(EMAIL_ACCOUNT, PASSWORD)
rv, data = M.select(EMAIL_FOLDER)
if rv == 'OK':
print "Processing mailbox: ", EMAIL_FOLDER
process_mailbox(M)
M.close()
else:
print "ERROR: Unable to open mailbox ", rv
M.logout()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment