Esempio n. 1
0
        private void btn_test_Click(object sender, EventArgs e)
        {
            Mblog        mblog       = null;
            string       mid         = "";
            string       ss          = "";
            string       url         = "";
            string       colresult   = "";
            MblogData    mbloglist   = null;
            int          isHave      = 0;
            WeiboComment comment     = null;
            string       links       = "";//多链接以,分割
            int          j           = 1;
            int          linknum     = 0;
            bool         isCoupon    = false;
            string       couponlinks = "";

            string[] urls     = File.ReadAllLines(("config/colweibo.txt"));
            string[] xiaohaos = File.ReadAllLines(("config/xiaohao.txt"));
            alimamacc = Alimama.Login();
            InitWeiboAccounts();
            weibocc = WeiboHandler.InitWeiboCookie(((DEWeiboAccount)DEWeiboAccounts[0]).Username, "config", 1);
            while (true)
            {
                foreach (string urltemp in urls)
                {
                    #region 抓取微博列表
                    url       = urltemp + "1";
                    ss        = "";
                    colresult = HttpHelper1.GetHttpsHtml(url, "", ref ss);
                    if (colresult.Contains(""))
                    {
                        colresult = colresult.Replace("page\":null", "page\":1");
                    }
                    mbloglist = Newtonsoft.Json.JsonConvert.DeserializeObject <MblogData>(colresult);
                    #endregion
                    #region 遍历处理需评论微博
                    foreach (Card card in mbloglist.Cards)
                    {
                        if (card.Mblog == null)
                        {
                            continue;
                        }
                        mblog = card.Mblog;
                        mid   = card.Mblog.Id;

                        isHave = Convert.ToInt32(SQLiteHelper.ExecuteScalar("select count(*) from mblog where id=" + mid));
                        if (isHave > 0)
                        {
                            continue;//如果数据已存在,则跳过
                        }
                        //先查看评论是否包含链接
                        comment = WeiboHandler.GetComment(mid, weibocc);
                        if (comment == null)
                        {
                            continue;
                        }

                        if (comment.Data.Html.Contains("图1:"))
                        {
                            string tbklinks_html = "";
                            linknum     = j = 1;
                            couponlinks = "";
                            HttpHelper1.GetStringInTwoKeyword(comment.Data.Html, ref tbklinks_html, "图1:", "<!-- 评论图片的处理 -->", 0);
                            Hashtable       tbklinks = new Hashtable();
                            Regex           reg      = new Regex(@"[a-zA-z]+://[^\s]*");
                            MatchCollection mc       = reg.Matches(tbklinks_html);

                            foreach (Match m in mc)
                            {
                                linknum++;
                                string weiboshortlink = m.Value.Replace("\"", "");
                                if (!weiboshortlink.StartsWith("http://t.cn"))
                                {
                                    continue;
                                }
                                links = links + weiboshortlink + ",";
                                //转换淘客微博评论链接为自有ID优惠券链接
                                //获取代理IP

                                //通过代理IP登录微博
                                //通过代理IP发布评论

                                string tbrealitem = "";
                                string result     = Alimama.GetItemResultWithWeiboShortUrl(weiboshortlink, alimamacc, ref tbrealitem);

                                if (tbrealitem.Contains("http://ai.taobao.com"))
                                {
                                    int staresult = SQLiteHelper.ExecuteNonQuery("insert into mblog(id,Source,Text,CreateAt,TbkLinks)values(@id,@Source,@Text,@CreateAt,@TbkLinks)", new[] {
                                        mblog.Id,
                                        mblog.Source,
                                        mblog.Text,
                                        mblog.CreatedAt,
                                        ""
                                    });
                                    break;
                                }



                                DEWeiboAccount deweiboaccount = WeiboHandler.GetOneAccount();
                                //string own_tbklink = Weibo.Common.Alimama.GetTbkLink(tbrealitem, deweiboaccount.Siteid, deweiboaccount.Adzoneid, alimamacc);//把淘宝客链接更换为自己链接
                                //if (own_tbklink == "" || own_tbklink == "链接不支持转化")
                                //{
                                //    int staresult = SQLiteHelper.ExecuteNonQuery("insert into mblog(id,Source,Text,CreateAt,TbkLinks)values(@id,@Source,@Text,@CreateAt,@TbkLinks)", new[] {
                                //                        mblog.Id,
                                //                        mblog.Source,
                                //                        mblog.Text,
                                //                        mblog.CreatedAt,
                                //                        ""
                                //                     });
                                //    break;//没有淘宝客链接,当前内容不可用,直接退出循环
                                //}
                                //else if (own_tbklink == "阿里妈妈登录失效")
                                //{

                                //    alimamacc = Alimama.Login();
                                //    if (Alimama.TestLogin(alimamacc))
                                //        ;
                                //    else
                                //    {

                                //        return;
                                //    }

                                //}
                                double maxRate      = 0;
                                string searchresult = Alimama.SearchItem(tbrealitem, alimamacc);
                                bool   isSuc        = Alimama.ApplyCampaign(searchresult, tbrealitem, ref alimamacc, ref maxRate);//申请定向计划

                                //获取优惠券信息
                                if (!searchresult.Contains("couponInfo\":\"无"))
                                {
                                    string couponshorturl       = Alimama.GetCouponInfo(searchresult, tbrealitem, alimamacc);
                                    string couponweiboShortlink = WeiboHandler.GetWeiboShorturl(couponshorturl);//微博短地址
                                    if (couponweiboShortlink != "")
                                    {
                                        couponlinks += "图" + j.ToString() + ":" + couponweiboShortlink + " ";
                                    }
                                    isCoupon = true;
                                }

                                //string weiboShortlink = WeiboHandler.GetWeiboShorturl(own_tbklink);//微博短地址
                                //own_tbklinks += "图" + j.ToString() + ":" + weiboShortlink + " ";

                                j++;
                            }
                        }

                        if (!couponlinks.Contains("图"))
                        {
                            continue;
                        }

                        //WebProxy proxy = DailiHelper.GetProxy();
                        //while (!DailiHelper.VerIP(proxy))
                        //{
                        //    proxy = DailiHelper.GetProxy();
                        //}
                        Random r               = new Random();
                        string xiaohao         = xiaohaos[r.Next(xiaohaos.Length)];
                        string xiaohaousername = xiaohao.Split(',')[0];
                        string xiaohaopassword = xiaohao.Split(',')[1];

                        string uid = "";
                        HttpHelper1.GetStringInTwoKeyword(url, ref uid, "uid=", "&", 0);
                        string refer = "http://www.weibo.com/u/" + uid + "/home?wvr=5";

                        CookieContainer xiaohaocc = WeiboHandler.InitWeiboCookie(xiaohaousername);
                        if (!WeiboHandler.TestLogin(xiaohaocc))
                        {
                            xiaohaocc = WeiboHandler.Login(xiaohaousername, xiaohaopassword);
                        }
                        while (xiaohaocc == null)
                        {
                            //取新号
                            xiaohao         = xiaohaos[r.Next(xiaohaos.Length)];
                            xiaohaousername = xiaohao.Split(',')[0];
                            xiaohaopassword = xiaohao.Split(',')[1];
                            //proxy = DailiHelper.GetProxy();
                            xiaohaocc = WeiboHandler.InitWeiboCookie(xiaohaousername);
                            if (!WeiboHandler.TestLogin(xiaohaocc))
                            {
                                xiaohaocc = WeiboHandler.Login(xiaohaousername, xiaohaopassword);
                            }
                        }
                        couponlinks = couponlinks.Replace("图", "");
                        string commentresult        = WeiboHandler.Comment(mblog.Id, uid, couponlinks, refer, xiaohaocc);
                        string decode_commentresult = Regex.Unescape(commentresult);
                        if (!decode_commentresult.Contains("抱歉") && !decode_commentresult.Contains("广告"))
                        {
                            if (!commentresult.Contains("{\"code\":\"100000\""))
                            {
                                //评论失败之后,再次评论
                                commentresult = WeiboHandler.Comment(mid, uid, "地址 " + couponlinks, refer, weibocc);
                            }
                            if (!commentresult.Contains("{\"code\":\"100000\""))
                            {
                                //第二次评论尝试
                                commentresult = WeiboHandler.Comment(mid, uid, "链接 " + couponlinks, refer, weibocc);
                            }
                            if (!commentresult.Contains("{\"code\":\"100000\""))
                            {
                                //第三次评论尝试
                                commentresult = WeiboHandler.Comment(mid, uid, "购 " + couponlinks, refer, weibocc);
                            }
                        }
                        if (commentresult.Contains("{\"code\":\"100000\""))
                        {
                            int statu = SQLiteHelper.ExecuteNonQuery("insert into mblog(id,Source,Text,CreateAt,TbkLinks)values(@id,@Source,@Text,@CreateAt,@TbkLinks)", new[] {
                                mblog.Id,
                                mblog.Source,
                                mblog.Text,
                                mblog.CreatedAt,
                                couponlinks
                            });//选品库下载完成后,保存微博数据到数据库
                        }
                    }//单个mblog处理结束
                    #endregion
                }
                Thread.Sleep(10 * 60 * 1000);
            }
        }
Esempio n. 2
0
        private void btn_ReadExcel_Click(object sender, EventArgs e)
        {
            string[] items    = File.ReadAllLines(this.txt_FileAddr.Text, Encoding.UTF8);
            string[] contents = new string[0];
            if (this.txt_ContentFileAddr.Text != "")
            {
                contents = File.ReadAllLines(this.txt_ContentFileAddr.Text, Encoding.UTF8);
            }
            int xuanpinkuNum = Convert.ToInt32(this.numericUpDown1.Value);

            if (cbox_UseContentNum.Checked && this.txt_ContentFileAddr.Text != "")
            {
                xuanpinkuNum = contents.Length;
            }

            for (int i = 0; i < xuanpinkuNum; i++)
            {
                string content = "";
                if (contents.Length > 0 && i < contents.Length)
                {
                    content = contents[i];
                }
                int[]  randomNums   = FrmMain.GetRandomNum(9, 1, items.Length - 1);
                int    j            = 1;
                string dirname      = "";
                string own_tbklinks = "";
                foreach (int itemnum in randomNums)
                {
                    string[] item    = items[itemnum].Split('\t');
                    string   picurl  = item[2];
                    string   tbklink = item[15];
                    string   brand   = item[4];
                    if (item.Length == 22)
                    {
                        tbklink = item[10];
                    }
                    if (j == 1)
                    {
                        if (content != "")
                        {
                            dirname = content;
                        }
                        else
                        {
                            if (brand.Length >= 4)
                            {
                                dirname = brand.Substring(0, 4);
                            }
                            else
                            {
                                dirname = brand;
                            }
                        }
                        //创建选品文件夹
                        if (!Directory.Exists("temp/"))
                        {
                            Directory.CreateDirectory("temp");
                        }

                        if (!Directory.Exists("temp/" + dirname + "/"))
                        {
                            Directory.CreateDirectory("temp/" + dirname + "/");
                        }
                        else
                        {
                            continue;
                        }
                    }
                    //下载图片
                    HttpHelper1.HttpDownloadFile(picurl, "temp/" + dirname + "/" + j.ToString() + ".jpg", new System.Net.CookieContainer());


                    string weiboShortlink = WeiboHandler.GetWeiboShorturl(tbklink);
                    while (weiboShortlink == "")
                    {
                        System.Threading.Thread.Sleep(30 * 1000);
                        weiboShortlink = WeiboHandler.GetWeiboShorturl(tbklink);
                    }
                    own_tbklinks += "【P" + j.ToString() + ":" + weiboShortlink + "】 ";
                    j++;
                }
                if (j == 10)
                {
                    File.WriteAllText("temp/" + dirname + "/comment.txt", own_tbklinks);
                    //如果文件夹重名,先删除老的文件夹
                    if (Directory.Exists("待发布/" + dirname))
                    {
                        Directory.Delete("待发布/" + dirname, true);
                    }
                    Directory.Move("temp/" + dirname, "待发布/" + dirname);//下載完成放入待发布库
                }
                else
                {
                    Directory.Delete("temp/" + dirname, true);
                }
            }
        }