# -*- coding: utf-8 -*-

from lxml import html
import requests, json, re, itertools

naverComicUrl = 'http://comic.naver.com'
naverComicListUrl = naverComicUrl + '/webtoon/creation.nhn?view=image'


def init_thumbnails():
	response = requests.get(naverComicListUrl)
	parsedBody = html.fromstring(response.text)
	thumbnailDivisions = parsedBody.xpath("//div[@class='thumb']")
	thumbnails = [{'title': td.xpath('./a/@title')[0], 'href': td.xpath('./a/@href')[0], 'imgsrc': td.xpath('./a/img/@src')[0], 'id': re.search('[0-9]+', td.xpath('./a/@href')[0]).group(0)} for td in thumbnailDivisions]

	for thumbnail in thumbnails:
		print 'Getting Stars from ' + thumbnail['title'].encode('utf-8')
		thumbnail['stars'] = makeStarList(thumbnail['href'])
		thumbnail['author'] = getAuthor(thumbnail['href'])

	with open('static/thumbnails.json', 'w') as fp:
		json.dump(thumbnails, fp)

def endPageNum(href):
	return int(html.fromstring(requests.get(naverComicUrl + href + '&page=1000').text).xpath("//span[@class='current']")[0].text)

def makeStarList(href):
	pages = [naverComicUrl + href + '&page=' + str(num) for num in xrange(1, endPageNum(href) + 1)]
	stars = list(itertools.chain(*[[float(star.text)
				for star
				in [x.xpath('./strong')[0]
					for x
					in html.fromstring(requests.get(page).text).xpath("//div[@class='rating_type']")]]
			for page
			in pages]))
	return stars[::-1]

def getAuthor(href):
	author = html.fromstring(requests.get(naverComicUrl + href).text).xpath("//span[@class='wrt_nm']")[0].text
	return author

def missing_pages(tn):
	pages = [naverComicUrl + '/webtoon/detail.nhn?titleId=' + tn['id'] + '&no=' + str(num) for num in xrange(1, len(tn['stars']) + 1)]
	main_title = html.fromstring(requests.get('http://comic.naver.com/main.nhn').text).xpath('//head/title')[0].text
	mpgs = [i+1 for i, page in enumerate(pages) if html.fromstring(requests.get(page).text).xpath('//head/title')[0].text == main_title]
	print tn['title'].encode('utf-8'), mpgs
	return mpgs

def validation():
	with open('static/thumbnails.json', 'r') as fp:
		thumbnails = json.load(fp)
	withMissing = [{
		'author' : t['author'],
		'title' : t['title'],
		'href' : t['href'],
		'stars' : t['stars'],
		'imgsrc' : t['imgsrc'],
		'id' : t['id'],
		'missing' : [str(pn) for pn in missing_pages(t)]
	} for t in thumbnails]
	with open('static/thumbnails.json', 'w') as fp:
		fp.write(withMissing)


if __name__ == '__main__':
	#init_thumbnails()
	validation()
@@ -76,16 +76,21 @@
 tertools
+, sys
 %0A%0AnaverC
@@ -2005,19 +2005,24 @@
 idation(
+start
 ):%0A
-
 %09with op
@@ -2096,25 +2096,91 @@
 p)%0A%09
-withMissing
+for i in xrange(int(start), len(thumbnails)):%0A%09%09t = thumbnails%5Bi%5D%0A%09%09thumbnail
  = 
-%5B
 %7B%0A
+%09
 %09%09'a
@@ -2203,16 +2203,17 @@
 or'%5D,%0A%09%09
+%09
 'title' 
@@ -2226,16 +2226,17 @@
 itle'%5D,%0A
+%09
 %09%09'href'
@@ -2251,16 +2251,17 @@
 ef'%5D,%0A%09%09
+%09
 'stars' 
@@ -2270,24 +2270,25 @@
 t%5B'stars'%5D,%0A
+%09
 %09%09'imgsrc' :
@@ -2303,16 +2303,17 @@
 rc'%5D,%0A%09%09
+%09
 'id' : t
@@ -2320,16 +2320,17 @@
 %5B'id'%5D,%0A
+%09
 %09%09'missi
@@ -2377,18 +2377,28 @@
 )%5D%0A%09
-%7D for t in
+%09%7D%0A%09%09thumbnails%5Bi%5D =
  thu
@@ -2403,19 +2403,18 @@
 humbnail
-s%5D
 %0A
+%09
 %09with op
@@ -2456,16 +2456,17 @@
 s fp:%0A%09%09
+%09
 fp.write
@@ -2470,20 +2470,42 @@
 ite(
-withMissing)
+thumbnails)%0A%09%09print i, 'complete!'
 %0A%0A%0Ai
@@ -2550,16 +2550,16 @@
 nails()%0A
-
 %09validat
@@ -2562,9 +2562,20 @@
 idation(
+sys.argv%5B1%5D
 )

modify validation to save every update