# -*-coding=utf-8-*-
__author__ = 'Rocky'
# -*-coding=utf-8-*-
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
from email import encoders, utils
import urllib.request, urllib.error, urllib.parse
import time
import re
import sys
import os

from bs4 import BeautifulSoup

from email.header import Header
import imp

class GetContent():
    def __init__(self, id):

        # 给出的第一个参数 就是你要下载的问题的id
        # 比如 想要下载的问题链接是 https://www.zhihu.com/question/29372574
        # 那么 就输入 python zhihu.py 29372574

        id_link = "/question/" + id
        #self.getAnswer(id_link)
        self.getstorelist();
        #self.gettuangoulist();

    def save2file(self, filename, content):
        # 保存为电子书文件
        filename = filename + ".txt"
        f = open(filename, 'a', encoding="utf-8")
        f.write(content)
        f.close()
    def gettuangoulist(self):    
        url = "http://www.shihuo.cn/tuangou"
        print(url)
        user_agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
        # 构造header 伪装一下
        header = {"User-Agent": user_agent}
        req = urllib.request.Request(url, headers=header)
        try:
            resp = urllib.request.urlopen(req)
            content= resp.read().decode('utf-8');
            if content is None:
                print("Empty")
                return None
        except:
            print("Time out. Retry")
            time.sleep(30)
            # try to switch with proxy ip
            resp = urllib.request.urlopen(req)
            content = resp.read()
            if content is None:
                print("Empty")
                return None 
            return None
          # 这里已经获取了 网页的代码，接下来就是提取你想要的内容。 使用beautifulSoup 来处理，很方便
        try:
            bs = BeautifulSoup(content)
  
        except:
            print("Beautifulsoup error")
            return None
        #print(content)
        title = bs.title
        # 获取的标题
        print(title)
        filename_old = title.string.strip()
        print(filename_old)
        filename = re.sub('[\/:*?"<>|]', '-', filename_old)
        # 用来保存内容的文件名，因为文件名不能有一些特殊符号，所以使用正则表达式过滤掉
        store_list = bs.find_all("li", class_="") 
        
        html_up =  """\ 
        <html> 
          <head>识货团购</head> 
          <body>
        """
        html_middle = "";
        html_bottom = """\
          </body> 
        </html> 
        """         
        k = 0
        index = 0
        for each_list in store_list:
            store_name = each_list.find("div",class_="h2") 
            if(store_name!=None):
                print (store_name.a.string);
            store_price = each_list.find("div",class_="z2") 
            
            if(store_price!=None):
                html_middle= html_middle+str(each_list);
                '''
                store_rmb = store_price.find("span",class_="z1")
                i=0
                if(store_rmb!=None): 
                    for string in store_rmb.strings:
                        self.save2file(filename,string)
                        i = i + 1;
                        if(i ==1 and float(string)<=299):
                            print (string)
                            html_middle= html_middle+str(each_list);
                #print (store_rmb);            
            #for a in each_answer.strings:
                # 循环获取每一个答案的内容，然后保存到文件中
            #self.save2file(filename, str(a))
            '''
            k += 1
            index = index + 1
        html = html_up + html_middle + html_bottom;
        
        smtp_server = 'smtp.sina.com'
        from_mail = 'he_jingtai@sina.com'
        password = 'hjtgoodno1'
        to_mail = '1024654056@qq.com'
        
        sender = '17759118897@189.cn' 
        receiver = '1024654056@qq.com' 
        subject = '图片附件html发送邮件测试'        
        
        send_kindle=MailAtt(smtp_server,from_mail,password,to_mail)
        send_kindle.sendMail(sender,receiver,subject,html)          
    def getstorelist(self):
        url = "http://www.shihuo.cn/haitao/daigou?order=date"
        print(url)
        user_agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
        # 构造header 伪装一下
        header = {"User-Agent": user_agent}
        req = urllib.request.Request(url, headers=header)
        try:
            resp = urllib.request.urlopen(req)
            content= resp.read().decode('utf-8');
            if content is None:
                print("Empty")
                return None
        except:
            print("Time out. Retry")
            time.sleep(30)
            # try to switch with proxy ip
            resp = urllib.request.urlopen(req)
            content = resp.read()
            if content is None:
                print("Empty")
                return None 
            return None
          # 这里已经获取了 网页的代码，接下来就是提取你想要的内容。 使用beautifulSoup 来处理，很方便
        try:
            bs = BeautifulSoup(content)
  
        except:
            print("Beautifulsoup error")
            return None
        #print(content)
        title = bs.title
        # 获取的标题
        print(title)
        filename_old = title.string.strip()
        print(filename_old)
        filename = re.sub('[\/:*?"<>|]', '-', filename_old)
        # 用来保存内容的文件名，因为文件名不能有一些特殊符号，所以使用正则表达式过滤掉
        store_list = bs.find_all("li", class_="") 
        
        html_up =  """\ 
        <html> 
          <head>测试一下</head> 
          <body>
        """
        html_middle = "";
        html_bottom = """\
          </body> 
        </html> 
        """         
        k = 0
        index = 0
        for each_list in store_list:
            store_name = each_list.find("div",class_="title") 
            #if(store_name!=None):
                #print (store_name.a.string);
            store_price = each_list.find("div",class_="price") 
            if(store_price!=None):
                store_rmb = each_list.find("span")
                i=0
                for string in store_rmb.strings:
                    self.save2file(filename,string)
                    i = i + 1;
                    if(i ==2 and float(string)<200):
                        html_middle= html_middle+str(each_list);
                #print (store_rmb);            
            #for a in each_answer.strings:
                # 循环获取每一个答案的内容，然后保存到文件中
            #self.save2file(filename, str(a))
            k += 1
            index = index + 1
        html = html_up + html_middle + html_bottom;
        
        smtp_server = 'smtp.sina.com'
        from_mail = 'he_jingtai@sina.com'
        password = 'hjtgoodno1'
        to_mail = '1024654056@qq.com'
        
        sender = '17759118897@189.cn' 
        receiver = '1024654056@qq.com' 
        subject = '图片附件html发送邮件测试'        
        
        send_kindle=MailAtt(smtp_server,from_mail,password,to_mail)
        send_kindle.sendMail(sender,receiver,subject,html)  
    
        
    def getAnswer(self, answerID):
        host = "http://www.shihuo.cn"
        url = "http://www.shihuo.cn/haitao/daigou?order=date"
        print(url)
        user_agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
        # 构造header 伪装一下
        header = {"User-Agent": user_agent}
        req = urllib.request.Request(url, headers=header)

        try:
            resp = urllib.request.urlopen(req)
            content= resp.read().decode('utf-8');
            if content is None:
                print("Empty")
                return None
        except:
            print("Time out. Retry")
            time.sleep(30)
            # try to switch with proxy ip
            resp = urllib.request.urlopen(req)
            content = resp.read()
            if content is None:
                print("Empty")
                return None
        # 这里已经获取了 网页的代码，接下来就是提取你想要的内容。 使用beautifulSoup 来处理，很方便
        try:
            bs = BeautifulSoup(content)

        except:
            print("Beautifulsoup error")
            return None
        #print(content)
        title = bs.title
        # 获取的标题
        print(title)

        filename_old = title.string.strip()
        print(filename_old)
        filename = re.sub('[\/:*?"<>|]', '-', filename_old)
        # 用来保存内容的文件名，因为文件名不能有一些特殊符号，所以使用正则表达式过滤掉

        self.save2file(filename, title.string)

        store_list = bs.find_all("li", class_="")        
        k = 0
        index = 0
        for each_list in store_list:
            store_name = each_list.find("div",class_="title") 
            if(store_name!=None):
                self.save2file(filename, "\n\n-------------------------store %s via  -------------------------\n\n" % k)
                print (store_name.a.string);
                self.save2file(filename, str(store_name.a.string))
            store_price = each_list.find("div",class_="price") 
            if(store_price!=None):
                store_rmb = each_list.find("span")
                i=0
                for string in store_rmb.strings:
                    self.save2file(filename,string)
                    i = i + 1;
                    if(i ==2 and float(string)<200):
                        print(string);
                #print (store_rmb);            
            #for a in each_answer.strings:
                # 循环获取每一个答案的内容，然后保存到文件中
            #self.save2file(filename, str(a))
            k += 1
            index = index + 1        
        
        
        
        #detail = bs.find("li", class_="")
        #self.save2file(filename, "\n\n\n\n--------------------Link %s ----------------------\n\n"  %url)
        #self.save2file(filename, "\n\n\n\n--------------------Detail----------------------\n\n")
        # 获取问题的补充内容


        smtp_server = 'smtp.sina.com'
        from_mail = 'he_jingtai@sina.com'
        password = 'hjtgoodno1'
        to_mail = '17759118897@189.cn'
        
        sender = '17759118897@189.cn' 
        receiver = '1024654056@qq.com' 
        subject = '图片附件html发送邮件测试'        

        send_kindle=MailAtt(smtp_server,from_mail,password,to_mail)
        send_kindle.sendMail(sender,receiver,subject,html)
        # 调用发送邮件函数，把电子书发送到你的kindle用户的邮箱账号，这样你的kindle就可以收到电子书啦
        print(filename)


class MailAtt():
    def __init__(self, smtp_server, from_mail, password, to_mail):
        self.server = smtp_server
        self.username = from_mail.split("@")[0]
        self.from_mail = from_mail
        self.password = password
        self.to_mail = to_mail

        # 初始化邮箱设置

    def sendMail(self,sender,receiver,subject,html): 
        smtpserver = 'smtp.189.cn' 
        username = '17759118897' 
        password = 'hjtgood'
    
        msg = MIMEMultipart('alternative') 
        msg['Subject'] = Header(subject,'utf-8')
    
       
        htm = MIMEText(html,'html','utf-8') 
        msg.attach(htm)
    
     
    
    #构造图片
        '''
        fp = open('QQ截图未命名1.jpg','rb') 
        msgImage = MIMEImage(fp.read()) 
        fp.close()
    
        msgImage.add_header('Content-ID','<image1>') 
        msg.attach(msgImage)
        
        fp = open('QQ截图未命名1.jpg','rb') 
        msgImage = MIMEImage(fp.read()) 
        fp.close()
     
        msgImage.add_header('Content-ID','<image2>') 
        msg.attach(msgImage)    
        '''
     
    
    #构造附件
    
        #att = MIMEText(open('Pictures.rar','rb').read(),'base64','utf-8') 
        #att["Content-Type"] = 'application/octet-stream' 
        #att["Content-Disposition"] = 'attatchment;filename="Pictures.rar"' 
        #msg.attach(att)
    
        smtp = smtplib.SMTP() 
        smtp.connect('smtp.189.cn') 
        smtp.login(username,password) 
        smtp.sendmail(sender,receiver,msg.as_string()) 
        smtp.quit()    

    def send_txt(self, filename):
        # 这里发送附件尤其要注意字符编码，当时调试了挺久的，因为收到的文件总是乱码
        self.filename = "C:\\Users\\heinic\\content\\" + filename + ".txt"
        content = open(self.filename, 'rb').read()        
        self.msg = MIMEText(content,_subtype='plain',_charset='gb2312')  
        self.msg['Subject'] = 'sssssss'  
        self.msg['From'] = self.from_mail  
        self.msg['To'] =  self.to_mail 
        try:  
            self.smtp  = smtplib.SMTP()  
            self.smtp.connect(self.server)  
            self.smtp.login(self.username, self.password)
            self.smtp.sendmail(self.msg['from'], self.msg['To'], self.msg.as_string())  
            self.smtp.close()  
            return True  
        except Exception as e:  
            print (str(e))  
            return False          
        
        
        
        '''
        
        self.smtp = smtplib.SMTP()
        self.smtp.connect(self.server)
        self.smtp.login(self.username, self.password)
        #self.smtp = smtplib.SMTP_SSL(self.server, 465)
        #self.smtp.ehlo()    
        #self.smtp.login('1024654056@qq.com','mhpdosozbielbddc')#XXX为用户名，XXXXX为密码 
        #创建一个带附件的实例
    #msg = MIMEMultipart()
    #构造附件1

    #msg.attach(att1)        
        #self.msg = MIMEMultipart()

        self.filename = "C:\\Users\\heinic\\content\\" + filename + ".txt"
        #self.msg['Date'] = utils.formatdate(localtime=1)
        content = open(self.filename, 'rb').read()
        #self.att = MIMEText(content, 'base64', 'gb2312')
        self.msg = MIMEText(content, 'plain', 'utf-8') 
        self.msg['to'] = self.to_mail
        self.msg['from'] = self.from_mail
        self.msg['Subject'] = "convert"        
        #self.msg.attach(content1)
        #attfile = 'C:\\Users\\heinic\\Desktop\\题库.txt'
        #basename = os.path.basename(attfile)
        #fp = open(self.filename, 'rb')
        #self.att = MIMEText(fp.read(), 'base64', 'gb2312')
        #self.att["Content-Type"] = 'application/octet-stream'
        #self.att.add_header('Content-Disposition', 'attachment',filename=('gbk', '', self.filename))
        #encoders.encode_base64(self.att)        

        # print content
        #self.att = MIMEText(open('C:\\Users\\heinic\\Desktop\\题库.txt', 'rb').read(), 'base64', 'gb2312')
        #self.att["Content-Type"] = 'application/octet-stream'
        #self.att["Content-Disposition"] = 'attachment; filename="题库.txt"'#这里的filename可以任意写，写什么名字，邮件中显示什么名字        
        #self.att = MIMEText(content, 'base64', 'gb2312')
        #self.att['Content-Type'] = 'application/octet-stream'
        #self.att["Content-Disposition"] = "attachment;filename=\"%s\"" %(self.filename.encode('gb2312'))
        #self.att["Content-Disposition"] = 'attachment; filename="题库.txt"';
        # print self.att["Content-Disposition"]
        #self.msg.attach(self.att)

        self.smtp.sendmail(self.msg['from'], self.msg['to'], self.msg.as_string())
          
        self.smtp.quit()
        '''  

if __name__ == "__main__":

    sub_folder = os.path.join(os.getcwd(), "content")
    # 专门用于存放下载的电子书的目录

    if not os.path.exists(sub_folder):
        os.mkdir(sub_folder)

    os.chdir(sub_folder)

    id="35461941"
    #id = sys.argv[1]
    # 给出的第一个参数 就是你要下载的问题的id
    # 比如 想要下载的问题链接是 https://www.zhihu.com/question/29372574
    # 那么 就输入 python zhihu.py 29372574

    
    # id_link="/question/"+id
    obj = GetContent(id)
    # obj.getAnswer(id_link)

    # 调用获取函数

    print("Done")