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

'''把前1000个关键词的搜索结果保存,为以后的搜索调整做对比'''

import time
from libs.parameter import generateEnv
from lnc.search.simple import Simple

simple = Simple(generateEnv('alpha'))
simple.login()
time.sleep(5)

keyword_file = 'input/keywords.txt'
keywords = [unicode(k.strip(), 'utf-8') for k in open(keyword_file, 'r')]

output = open('output/after-adjust.csv', 'a')

b = 1
for keyword in keywords:
    res = simple.get_results_list(keyword)
    if res:
        lines = map(None, (keyword,), (res['total'],), (res['time'],), res['title'])
        for l in lines:
            line = [x if x is not None else u'' for x in l]
            output.write("%s|%s|%s|%s\n" % tuple(line))
        output.write("\n")
        output.flush()

simple.close(logout=True)

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

'''
从input/exact_title.txt文件里面读取标题
然后进行搜索,检查第一条结果是否中标题完全匹配
注意附件的标题会在后面有一个括号,括号里面是附件的文件名
'''

import re, time
from libs.parameter import generateEnv
from lnc.search.simple import Simple

input_titles = [unicode(x.strip(), 'utf-8') for x in open('input/exact_title.txt')]

lnc_simple = Simple(generateEnv('alpha'))
lnc_simple.login()
time.sleep(5)

p = re.compile("\(\d+.[a-zA-Z]{,4}\)", re.IGNORECASE)

output = open('logs/exact_title.csv', 'a')

for it in input_titles:
    title_list = lnc_simple.get_results_list(it)
    first_equal = False
    if title_list:
        if len(title_list[0]) > len(it) and title_list[0].find(it) > -1:
            remain_title = title_list[0][len(it):].strip()
            if p.match(remain_title):
                first_equal = True
        elif it == title_list[0]: