예제 #1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        string verifier = Request.QueryString.Get("oauth_verifier");
        if (!string.IsNullOrEmpty(verifier))
        {
            string appKey = Constant.app_key_QQ;
            string appSecret = Constant.app_secret_QQ;
            OAuth oauth = new OAuth(appKey, appSecret);
            string name;
            oauth.Token = (string)Session["QQ_oauth_token"];
            oauth.TokenSecret = (string)Session["QQ_oauth_token_secret"];
            if (oauth.GetAccessToken(verifier, out name))
            {
                Session["QQ_oauth_token"] = oauth.Token;
                Session["QQ_oauth_token_secret"] = oauth.TokenSecret;
                Session["QQ_user_id"] = name;

                DictEntity dt = new DictEntity();
                dt.App = "QQ";
                dt.UserID = name;
                dt.Key = "Token";
                dt.Value = oauth.Token;
                Dict.Save(dt);
                dt.Key = "TokenSecret";
                dt.Value = oauth.TokenSecret;
                Dict.Save(dt);

                //MaxTimeline
                Timeline api = new Timeline(oauth);
                var data = api.GetBroadcast_timeline(PageFlag.First, 0, 1);
                if (data.Tweets.Length > 0)
                {
                    OpenTSDK.Tencent.Objects.Tweet tw = data.Tweets[0];
                    if (tw.Timestamp > 0)
                    {
                        DictEntity dtT = new DictEntity();
                        dtT.App = "QQ";
                        dtT.Key = "MaxTimeline";
                        dtT.UserID = name;
                        dtT.Value = tw.Timestamp.ToString();
                        Dict.Save(dtT);
                    }
                }

                //T.SaveRelation();

                //Response.Write(oauth.TokenSecret);
                Response.Redirect("Default.aspx");
            }

        }
    }
예제 #2
0
        public static void Run()
        {
            try
               {
               //实例化OAuth对象
               string appKey = Constant.app_key_QQ;
               string appSecret = Constant.app_secret_QQ;

               List<RelationEntity> lt = Relation.Get();

               foreach (RelationEntity rt in lt)
               {
                   #region  单个用户
                   try
                   {
                       Dictionary<string, string> Main = new Dictionary<string, string>();
                       Dictionary<string, string> Sub = new Dictionary<string, string>();

                       Main = Dict.Get(rt.App, rt.UserID);
                       Sub = Dict.Get(rt.SubApp, rt.SubUserID);

                       OAuth oauth = new OAuth(appKey, appSecret);
                       oauth.Token = Main["Token"];// //"91c8a7555f694dd3bd7a55178dfa952d";            //Access Token
                       oauth.TokenSecret = Main["TokenSecret"];// "2e01aa675faf5764d59564537f142f51";      //Access Secret

                       Timeline api = new Timeline(oauth);
                       //各取数据出来
                       long maxTimeline = Main.ContainsKey("MaxTimeline") ? (long)Convert.ToDecimal(Main["MaxTimeline"]) : 0;
                       decimal MaxId = Sub.ContainsKey("MaxId") ? Convert.ToDecimal(Sub["MaxId"]) : 0;
                       TimelineData data = null;
                       try
                       {
                           data = api.GetBroadcast_timeline(PageFlag.First, 0, Constant.QQRequestNum);
                       }
                       catch (Exception ex)
                       {
                           log.Warn("{4},{0},{1},{2},{3}", rt.App, rt.UserID, rt.SubApp, rt.SubUserID, rt.ID);
                           log.Warn("{0},{1}", ex.Message, ex.StackTrace);
                           Console.WriteLine("-----");
                           Console.Write("{4},{0},{1},{2},{3},GetQQDataError", rt.App, rt.UserID, rt.SubApp, rt.SubUserID, rt.ID);
                       }
                        XmlDocument sinaT = null;
                        try
                        {

                            sinaT = Task.GetSinaUserTimeline(Sub["Token"], Sub["TokenSecret"], MaxId.ToString());
                        }
                        catch (Exception ex)
                        {
                            log.Warn("{4},{0},{1},{2},{3}", rt.App, rt.UserID, rt.SubApp, rt.SubUserID, rt.ID);
                            log.Warn("{0},{1}", ex.Message, ex.StackTrace);
                            Console.WriteLine("-----");
                            Console.Write("{4},{0},{1},{2},{3},GetSinaDataError", rt.App, rt.UserID, rt.SubApp, rt.SubUserID, rt.ID);
                        }

                       long maxTimelineTemp = maxTimeline;
                       decimal MaxIdTemp = 0;
                       if (data != null)
                       {
                           try
                           {
                               #region QQ to Sina

                               if (data.Tweets.Length > 0)
                               {

                                   //Master
                                   DictEntity dt = new DictEntity();
                                   dt.App = rt.App;
                                   dt.Key = "MaxTimeline";
                                   dt.UserID = rt.UserID;

                                   DictEntity dt2 = new DictEntity();
                                   dt2.App = rt.SubApp;
                                   dt2.Key = "MaxId";
                                   dt2.UserID = rt.SubUserID;

                                   for (int i = data.Tweets.Length; i > 0; i--)
                                   {
                                       try
                                       {
                                           OpenTSDK.Tencent.Objects.Tweet tw = data.Tweets[i - 1];
                                           if (tw.Timestamp > maxTimeline)
                                           {
                                               string Token = Sub["Token"];
                                               string TokenSecret = Sub["TokenSecret"];
                                               decimal Tid = 0;
                                               string pic = string.Empty;
                                               //只发原创
                                               if (tw.Type == 1)
                                               {
                                                   if (string.IsNullOrEmpty(tw.Image))
                                                   {
                                                       Tid = Sina(tw.Origtext, Token, TokenSecret);
                                                   }
                                                   else
                                                   {
                                                       pic = GetHttpFile(tw.Image + "/2000");
                                                       if (!string.IsNullOrEmpty(pic))
                                                       {
                                                           Tid = Sina(tw.Origtext, Token, TokenSecret, pic);
                                                       }
                                                       else
                                                       {
                                                           Tid = Sina(tw.Origtext, Token, TokenSecret);
                                                       }
                                                   }
                                               }

                                               //dt.Value = tw.Timestamp.ToString();
                                               if (maxTimelineTemp < tw.Timestamp)
                                               {
                                                   maxTimelineTemp = tw.Timestamp;
                                               }
                                               if (MaxIdTemp < Tid)
                                               {
                                                   MaxIdTemp = Tid;
                                               }
                                           }
                                       }
                                       catch (Exception ex)
                                       {
                                           log.Warn("{4},{0},{1},{2},{3}", rt.App, rt.UserID, rt.SubApp, rt.SubUserID, rt.ID);
                                           log.Warn("{0},{1}", ex.Message, ex.StackTrace);
                                           Console.Write(ex.Message);
                                           Console.Write("{4},{0},{1},{2},{3}", rt.App, rt.UserID, rt.SubApp, rt.SubUserID, rt.ID);

                                       }
                                   }
                                   if (maxTimeline < maxTimelineTemp)
                                   {
                                       dt.Value = maxTimelineTemp.ToString();
                                       Dict.Save(dt);
                                   }
                                   if (MaxIdTemp > 0)
                                   {
                                       dt2.Value = MaxIdTemp.ToString();
                                       Dict.Save(dt2);
                                   }
                               }
                               #endregion
                           }
                           catch (Exception ex)
                           {
                               log.Warn("{0},{1}", ex.Message, ex.StackTrace);
                           }
                       }
                       #region Sina TO QQ

                       try
                       {
                           if (sinaT != null)
                           {
                               long maxTimelineTemp2 = maxTimelineTemp;
                               decimal MaxIdTemp2 = MaxIdTemp;
                               XmlNodeList x = sinaT.SelectNodes("/statuses/status");
                               if (x.Count > 0)
                               {
                                   DictEntity dt = new DictEntity();
                                   dt.App = rt.App;
                                   dt.Key = "MaxTimeline";
                                   dt.UserID = rt.UserID;

                                   DictEntity dt2 = new DictEntity();
                                   dt2.App = rt.SubApp;
                                   dt2.Key = "MaxId";
                                   dt2.UserID = rt.SubUserID;

                                   foreach (XmlNode xn in x)
                                   {
                                       decimal id = Util.GetXmlNodeValue<decimal>(xn.SelectSingleNode("id"));
                                       string text = Util.GetXmlNodeValue<string>(xn.SelectSingleNode("text"));

                                       string original_pic = string.Empty;
                                       try
                                       {
                                           original_pic = Util.GetXmlNodeValue<string>(xn.SelectSingleNode("original_pic"));

                                           if (!string.IsNullOrEmpty(original_pic))
                                           {
                                               original_pic = GetHttpFile(original_pic);
                                           }
                                       }
                                       catch (Exception ex)
                                       {
                                           log.Warn("{0},{1}", ex.Message, ex.StackTrace);
                                       }

                                       XmlNode retweeted_status = xn.SelectSingleNode("//retweeted_status");

                                       TweetOperateResult QQresult = null;
                                       //转播的不发啦
                                       if (retweeted_status == null)
                                       {
                                           QQresult = Task.QQ(text, Main["Token"], Main["TokenSecret"], original_pic);
                                           if (maxTimelineTemp2 < QQresult.Timestamp)
                                           {
                                               maxTimelineTemp2 = QQresult.Timestamp;
                                           }
                                       }

                                       if (MaxIdTemp2 < (long)id)
                                       {
                                           MaxIdTemp2 = id;
                                       }

                                   }
                                   if (maxTimelineTemp2 > maxTimelineTemp)
                                   {
                                       dt.Value = maxTimelineTemp2.ToString();
                                       Dict.Save(dt);
                                   }
                                   if (MaxIdTemp2 > MaxIdTemp)
                                   {
                                       dt2.Value = MaxIdTemp2.ToString();
                                       Dict.Save(dt2);
                                   }
                               }
                               //-----
                           }
                       }
                       catch (Exception ex)
                       {
                           log.Warn("{0},{1}", ex.Message, ex.StackTrace);
                       }
                       #endregion
                   }

                   catch (Exception ex)
                   {
                       Console.WriteLine(ex.Message);
                       log.Warn("{4},{0},{1},{2},{3}", rt.App, rt.UserID, rt.SubApp, rt.SubUserID, rt.ID);
                       log.Warn("{0},{1}", ex.Message, ex.StackTrace);

                   }

                   #endregion
               }
               }
               catch (Exception ex)
               {
               log.Warn("{0},{1}", ex.Message, ex.StackTrace);
               }
        }