import os
import codecs
import dataset
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')

table=db['homework']
lst=os.listdir(r'C:\data\homework')

def proc_email(fname):
    fr=codecs.open(fname,'r','utf-8')
    lines=fr.readlines()
    fr.close()
    email=None
    url=None

    for l in lines:
        if 'From:' in l:
            elmts=l.split("<")
            eml=elmts[1].replace('>','')
            print(eml)
            email=eml
        if 'http' in l:
            print(l)
            url=l
            if email is not None:
                break
    table.insert({'fname':fname,'email':email,'url':url})

for l in lst:
    fname=r'C:\data\homework'+"\\"+l
    if '.eml' in fname:
        print(fname)
        proc_email(fname)
#print(lst)