"""
추천수 200 이상의 color palette 정보를 가져와서 저장한다.
"""

import urllib3, re, time
http = urllib3.PoolManager()
Color_list = []
for num in range(80000):
    site = 'http://www.colourlovers.com/palette/' + str(num)
    response = http.request('POST', site)
    data = str(response.data)
    
    
    if re.findall('Page Not Loved',data):#존재하지 않는 페이지
        pass
    elif re.findall('This palette has gone missing!',data):#삭제된 페이지
        pass
    elif re.findall('This Palette has been marked as adult content',data):#제한된 페이지
        pass
    else:
        try:#오류발생시 저장후 중지
            a,b = re.findall('num-scores-count_'+str(num)+'">.*?</h4',data)[0].split('>')
        except IndexError:
            print(str(num)+'번째 에서 오류발생')
            break;
        a,b = b.split('<')
        if int(a.replace(',','')) > 200:
            temp = []            
            c = re.findall('\:\[.*\]\,',data)            
            for item in re.findall('[A-Z0-9]{6}',str(c)):
                if item != '000000' and item != 'FFFFFF':
                    temp.append(item)
            temp.insert(0,len(temp))
            Color_list.append(temp)
            print(str(num) + str(temp)) #진행상황 출력
            #time.sleep(1)            

file = open('Color_list.txt','w+')
for item in Color_list:
    for item2 in item:
        file.write("%s "%item2)
    file.write("\n")
file.close()
