# -*- coding:utf-8 -*-
import urlparse
import re

import requests

from model import session
from model.new_t import New_t
from tasks import new


class Producer(object):
    def __init__(self):
        self.base_url = 'https://api.dexi.io/'
        self.headers = {
            'X-DexiIO-Access': '8cd665427d06e2174790cb2836ec66d2',
            'X-DexiIO-Account': '038db958-24d4-4542-98b0-89c27dd88cc9'
        }
        self.categories = {u'行业资讯': 1,
                           u'国家政策': 2,
                           u'直销企业': 3,
                           u'直销项目': 4,
                           u'直销人物': 5,
                           u'负面消息': 6,
                           u'海外资讯': 7,
                           u'奖金制度': 8,
                           u'直销百科': 9}

    def get(self, url, params=None):
        return requests.get(urlparse.urljoin(self.base_url, url), params=params, headers=self.headers)

    def post(self, url, data, params=None):
        return requests.post(urlparse.urljoin(self.base_url, url), data=data, params=params, headers=self.headers)

    @staticmethod
    def convert(s):
        return ''.join([r'\u', s.strip('&#x;')]).decode('unicode_escape')

    # def run(self):
    #     r = self.get('runs', {'limit': 100})
    #     print r.json()
    #     runs = r.json()
    #     new_count = 0
    #     run_count = 1
    #     print '------------------ got {} rows --------------'.format(len(runs['rows']))
    #     for run in runs['rows']:
    #         print '------------------ start handle {} rows --------------'.format(run_count)
    #         robot = self.get('/robots/{}'.format(run['robotId'])).json()
    #         category = self.categories[robot['tags'][0]]
    #         last_result = self.get('/runs/{}/latest/result'.format(run['_id'])).json()
    #         print last_result
    #         if 'rows' in last_result:
    #             for values in last_result['rows']:
    #                 item = dict(zip(last_result['headers'], values))
    #                 if item['title'] is not None:
    #                     item['title'] = re.sub(r'&#x....;', lambda match: self.convert(match.group()), item['title'])
    #                 else:
    #                     continue
    #                 if item['content']:
    #                     item['content'] = re.sub(r'&#x....;', lambda match: self.convert(match.group()),
    #                                              item['content'])
    #                 else:
    #                     continue
    #                 if re.search(ur'直销人早报\d+', item['title']):
    #                     continue
    #                 if item['time']:
    #                     item['time'] = re.sub(r'&#x....;', lambda match: self.convert(match.group()), item['time'])
    #                     item['time'] = re.compile(r'\u5e74|\u6708').sub('-', item['time'])
    #                     item['time'] = re.compile(r'\u65e5').sub('', item['time'])
    #                     item['time'] = re.compile(r'\\').sub('', item['time'])
    #                 item['category'] = category
    #                 item['category_name'] = self.categories[robot['tags'][0]]
    #                 print item
    #                 # push to tasks
    #                 item['seq'] = new_count
    #                 # checked same new in database
    #                 try:
    #                     res = session.query(New.id).filter(New.title == item['title']).first()
    #                 except Exception as e:
    #                     print e.message
    #                     res = 0
    #                 print('----------checked  res: {}------------'.format(res))
    #                 if res is None:
    #                     new.save_new(item)
    #                     new_count += 1
    #                 else:
    #                     print('----------- record was existed. -----------')
    #                 print new_count
    #         else:
    #             print 'response {} status'.format(last_result['code'])
    #         run_count += 1

    def run(self):
        data = session.query(New_t).filter().offset(11000).limit(5000).all()
        print '------got {} row ------'.format(len(data))
        deal_count = 0
        for item in data:
            print '------dealing {} row-------'.format(deal_count)
            if item.title is None:
                continue
            if item.content is None:
                continue
            if re.search(ur'直销人早报\d+', item.title):
                continue
            item.id = deal_count
            new.save_new(item)
            deal_count += 1
        print '-----jobs done------'



