#!/usr/bin/python
# -*- coding: UTF-8 -*-
import  sys, time, json
from datetime import datetime,timedelta

sys.path.append(r'C:\\Users\\admin\\Documents\\Visual Studio 2015\\Projects\\PythonApplication1\\PythonApplication1')  
from taikoropensdk import TaikorOauthClient
from Model.Item import *
from Model.AnalyzeData import *
from Person import Person

#此内容最终被封装成一个函数，按照周期每隔多长时间调用一次使用。

# 1.基础配置预定义
giUserId = "TU20161027161331711608" 
giAppSecert = "46af9da375b007aa20e49b0391bff871b607f2d8e068fac0e58ba9f33ecd749f9e3205761e5e5167cf38170c6c94b7730a026d3d33d40b77a80ef6f2fcd3e84a526f8d38de0c82678d5bfdb2407e089ee17715491e746adfbc6d33998e7fafa658537331366c46e11c8c46b91446bc6d38ff59fa0cbe65b8486fafe14a7f5ab1"
giSyncIssueID = "GuoHai"
giSource = "Q38APDrtEfU="
giBaseUrl = "http://180.153.146.60:7978/"
giOnlyHasCopyright = "false"
skiphour = 2#stime 和etime 查询范围几个小时
 

# 2.创建连接，通过权限校验，获取token
try:
    client = TaikorOauthClient(giUserId,giAppSecert)
    
except Exception,err:
    print 1,err
else:
    #3.没报错的情况下，构建要传递的参数parameters
    
    """
    实例化一个timespan
    请注意它的参数顺序
    timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])
    """
    i = 0
    step = 1000 #1次读取多少条
    etime = int(time.time())
    print etime
    stime = int(time.mktime((datetime.now() - timedelta(hours=skiphour)).timetuple()))
    print stime
    parameters = {'Source': giSource,'Stime':stime, 'Etime': etime,'Count':0,'Skip':0}
    if(giOnlyHasCopyright.upper() == "TRUE"):
      parameters.setdefault("HasCopyright", true)
    parameters["Count"] = step
    while True: 
    #开始while
         parameters["Skip"] = i * step
         resultJson = client.httpGet("Search/FullContents", parameters);
         if(not resultJson or resultJson==""):  break;
         #解析出本次传输的100条数据为100个对象
         ItemList=[];
         client.handleItemList(resultJson,ItemList);
         if(not ItemList or len(ItemList)<=0): 
          break;
         else:
          i=i+1; 
          testkankan=ItemList[0];
          print i;
         #开始分析性代码
         #伪代码： for item in  ItemList: 循环分析。。。




         #结束分析性代码
   #结束while