예제 #1
0
        /// <summary>
        /// 向卧龙接口提交任务
        /// </summary>
        public void CommintTask()
        {
            while (true)
            {
                try
                {
                    var wltask = GetWolongWeiboTasks(0);
                    if (wltask == null)
                    {
                        WolongApiSecurity.Log("没有需要向卧龙提交的微博搜索任务!");
                        Thread.Sleep(2000);
                        continue;
                    }

                    var para = new Dictionary <string, object>();
                    para.Add("task_id", wltask.TaskId);
                    para.Add("keyword", wltask.Keyword);
                    var url = WolongApiSecurity.FormatWolongUrl(para, "add");

                    var wc         = new WebClient();
                    var html       = wc.DownloadString(url);
                    var woLongData = (JObject)JsonConvert.DeserializeObject(html);
                    if (woLongData == null || woLongData["code"] == null)
                    {
                        WolongApiSecurity.Log("未返回数据");
                        Thread.Sleep(2000);
                        continue;
                    }
                    else if ((int)woLongData["code"] != 1200)
                    {
                        WolongApiSecurity.Log(woLongData["code"] + ":" + woLongData["data"]);
                        Thread.Sleep(2000);
                        continue;
                    }

                    UpdateWolongWeiboTaskStatus(1, wltask._id);
                    WolongApiSecurity.Log(string.Format("已提交任务:{0},任务:{1},关键词:{2}", wltask.TaskId, wltask.Keyword, wltask.Keyword));
                    Thread.Sleep(2000);
                }
                catch (Exception ex)
                {
                    WolongApiSecurity.Log(ex.Message);
                    Thread.Sleep(2000);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 从卧龙接口取回任务数据
        /// </summary>
        public void Start()
        {
            var skip = 0;

            while (true)
            {
                try
                {
                    var wltask = GetWolongWeiboTasks(1, skip);
                    if (wltask == null)
                    {
                        WolongApiSecurity.Log("没有未完成的微博任务!");
                        skip = 0;
                        Thread.Sleep(2000);
                        continue;
                    }

                    //if (!wltask.CreatedAt.HasValue)
                    //{
                    //    WolongApiSecurity.Log("任务时间错误!");
                    //    Thread.Sleep(2000);
                    //    continue;
                    //}

                    double delay;
                    var    utcNow = DateTime.UtcNow.AddHours(8.0);
                    if (!wltask.LastUpdateAt.HasValue)
                    {
                        var hours = (utcNow - wltask.CreatedAt).TotalHours;
                        delay = hours;
                    }
                    else
                    {
                        var hours = (utcNow - wltask.LastUpdateAt.Value).TotalHours;
                        delay = hours;
                    }


                    if (delay <= 1)
                    {
                        WolongApiSecurity.Log("没有需要更新的微博任务!");
                        skip++;
                        Thread.Sleep(2000);
                        continue;
                    }

                    // 开始更新微博任务数据
                    // 注意:卧龙接口使用UTC+8时间,注意时区转换!!
                    // 格式:2017-03-27 14:45:00
                    int page = 1;
                    while (true)
                    {
                        try
                        {
                            var para = new Dictionary <string, object>
                            {
                                { "task_id", wltask.TaskId },
                                { "count", 50 },
                                { "page", page },
                                { "endtime", utcNow.ToString("yyyy-MM-dd hh:mm:ss") }
                            };

                            if (!wltask.LastUpdateAt.HasValue)
                            {
                                para.Add("starttime", wltask.CreatedAt.ToString("yyyy-MM-dd hh:mm:ss"));
                            }
                            else
                            {
                                para.Add("starttime", wltask.LastUpdateAt.Value.ToString("yyyy-MM-dd hh:mm:ss"));
                            }

                            var url  = WolongApiSecurity.FormatWolongUrl(para, null);
                            var wc   = new WebClient();
                            var html = wc.DownloadString(url);
                            //var html = temp_readjson();

                            var woLongData = (JObject)JsonConvert.DeserializeObject(html);
                            if (woLongData == null || woLongData["code"] == null)
                            {
                                WolongApiSecurity.Log("未返回数据");
                                break;
                            }
                            else if ((int)woLongData["code"] != 1200)
                            {
                                WolongApiSecurity.Log(woLongData["code"] + ":" + woLongData["data"]);
                                Thread.Sleep(2000);
                                continue;
                            }
                            SaveWolongWeiboData.SaveData(woLongData, wltask);
                            WolongApiSecurity.Log(string.Format("任务{0},{1}已保存{2}微博数据,Page {3}/{4}",
                                                                wltask.TaskId, wltask.Keyword, woLongData["data"]["item_list"].Count(), page, woLongData["data"]["total_page"].Value <int>()));

                            if (!woLongData["data"]["item_list"].Any())
                            {
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            WolongApiSecurity.Log(ex.Message);
                        }
                        finally
                        {
                            page++;
                        }
                    }


                    var updatedate = DateTime.UtcNow.AddHours(13.0);

                    UpdateWLTaskTime(utcNow, wltask._id);
                    WolongApiSecurity.Log(string.Format("已完成任务:{0},关键词:{1}", wltask.TaskId, wltask.Keyword));
                }
                catch (Exception ex)
                {
                    WolongApiSecurity.Log(ex.Message);
                    Thread.Sleep(2000);
                }
            }
        }