#-*- coding: utf-8 -*-
import requests
import re
import os
import time
import random
ua_pc = [ 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
        'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
        'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
        'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;',
        'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)',
        'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
        ' Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
        'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
        'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11',
         'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
        ' Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)',
         'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)',
        ' Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)',
        'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)',
        ' Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)',
         'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)']
ua_mobile = ['Mozilla/5.0(iPhone;U;CPUiPhoneOS4_3_3likeMacOSX;en-us)AppleWebKit/533.17.9(KHTML,likeGecko)Version/5.0.2Mobile/8J2Safari/6533.18.5',
                'Mozilla/5.0(iPad;U;CPUOS4_3_3likeMacOSX;en-us)AppleWebKit/533.17.9(KHTML,likeGecko)Version/5.0.2Mobile/8J2Safari/6533.18.5',
                'MQQBrowser/26Mozilla/5.0(Linux;U;Android2.3.7;zh-cn;MB200Build/GRJ22;CyanogenMod-7)AppleWebKit/533.1(KHTML,likeGecko)Version/4.0MobileSafari/533.1',
                 'Opera/9.80(Android2.3.4;Linux;OperaMobi/build-1107180945;U;en-GB)Presto/2.8.149Version/11.10',
                 'Mozilla/5.0(iPhone;U;CPUiPhoneOS4_3_3likeMacOSX;en-us)AppleWebKit/533.17.9(KHTML,likeGecko)Version/5.0.2Mobile/8J2Safari/6533.18.5']
header_pc = {'User-Agent':random.choice(ua_pc)}
header_mobile ={'User-Agent': random.choice(ua_mobile)}
path = os.getcwd()
if not os.path.exists(path+"\\Qzone_background_image"):
    os.mkdir(path+"\\Qzone_background_image")
url = "https://i.qq.com/"
regex = r'//qzs.qq.com/qzone/v6/v6_config/upload/(.+?).jpg'
regex = re.compile(regex)
url_js = 'https://qzonestyle.gtimg.cn/qzone/qzactStatics/configSystem/data/179/config1.js'
reg = re.compile('http://(.+?).jpg')
count = 0
image = []
end = 1512093661
download = []
print("定时爬取空间背景图片的脚本正在执行")
while time.time() < end:
    try:
        ua = {'User-Agent':random.choice(ua_pc)}
        print(ua)
        #爬取背景
        r = requests.get(url,headers = ua)
        list = re.findall(regex,r.text)
        image = []
        for i in list:
            img = 'https://qzs.qq.com/qzone/v6/v6_config/upload/'+str(i)+'.jpg'
            if img not in image:
                image.append(img)
        #爬取js
        r_js = requests.get(url_js)
        list = re.findall(reg,r_js.text)
        for i in list:
            img = 'http://'+str(i)+'.jpg'
            if img not in image:
                image.append(img)
        image = set(image)
        for i in image:
            if i in download:
                continue
            r = requests.get(i,headers = ua)
            with open(path+"\\Qzone_background_image"+"\\"+str(count)+".jpg",'wb') as f:
                print("正在下载第 %s 张图片" %(count))
                f.write(r.content)
                download.append(i)
            count+=1
    except:
        print("error")
    time.sleep(3600)
print("脚本执行完成")
