黑马程序员技术交流社区

标题: 使用opentieba自动签到 [打印本页]

作者: aisini    时间: 2014-8-21 15:01
标题: 使用opentieba自动签到
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Threading;
  8. using Newtonsoft.Json;
  9. using Newtonsoft.Json.Linq;
  10. using Opentieba;

  11. namespace sersign
  12. {
  13.     class SignWalker
  14.     {
  15.         public readonly BDUSS b;
  16.         public readonly kwf k;
  17.         public SignWalker(BDUSS b, baseBar bb)
  18.         {
  19.             this.b = b;
  20.             this.k = new kwf(bb.fid, bb.kw);
  21.         }
  22.         public void threadProc()
  23.         {
  24.             Program.log(b.getUsername() + " 开签" + k.kw);
  25.             try
  26.             {
  27.                 // 错误最高重试次数:10
  28.                 int tyacou = 10;
  29.             loope:
  30.                 tyacou--;
  31.                 try
  32.                 {
  33.                     // 签!
  34.                     b.sign(k);
  35.                 }
  36.                 catch (TiebaField e)
  37.                 {
  38.                     if (e.error_code == 160002)
  39.                     {
  40.                         throw;
  41.                     }
  42.                     if (tyacou >= 0)
  43.                     {
  44.                         goto loope;
  45.                     }
  46.                     else
  47.                     {
  48.                         throw;
  49.                     }
  50.                 }
  51.                 catch (System.Net.WebException)
  52.                 {
  53.                     if (tyacou >= 0)
  54.                     {
  55.                         goto loope;
  56.                     }
  57.                     else
  58.                     {
  59.                         throw;
  60.                     }
  61.                 }
  62.                 Program.log(b.getUsername() + " 签"+k.kw+"成功。于" + DateTime.Now.ToLongTimeString());
  63.             }
  64.             catch (TiebaField e)
  65.             {
  66.                 Program.log(b.getUsername() + " 签" + k.kw + "失败。" + e.error_code + ": " + e.error_msg + " 于" + DateTime.Now.ToLongTimeString());
  67.             }
  68.             catch (Exception e)
  69.             {
  70.                 Program.log(b.getUsername() + " 签" + k.kw + "失败。" + e.ToString() + " 于" + DateTime.Now.ToLongTimeString());
  71.             }
  72.             Program.doneLeft--;
  73.         }
  74.     }
  75.     class Program
  76.     {
  77.         public static volatile int doneLeft = 0;
  78.         public static FileStream fslog;
  79.         static void Main(string[] args)
  80.         {
  81.             // 加载日志log文件
  82.             if (!File.Exists("sign.log"))
  83.             {
  84.                 fslog = File.Create("sign.log");
  85.             }
  86.             else
  87.             {
  88.                 fslog = File.Open("sign.log", FileMode.Open, FileAccess.ReadWrite);
  89.                 fslog.Position = fslog.Length;
  90.             }
  91.             Program.log("==== " + DateTime.Now.ToLongDateString());
  92.             try
  93.             {
  94.                 // 加载配置文件
  95.                 FileStream reini = File.Open("userlist.json", FileMode.Open, FileAccess.Read);
  96.                 byte[] confstrbyte = new byte[reini.Length];
  97.                 reini.Read(confstrbyte, 0, (int)reini.Length);
  98.                 reini.Close();
  99.                 String jsonstr = Encoding.UTF8.GetString(confstrbyte);
  100.                 JObject conf = JSON.parse(jsonstr);
  101.                 JEnumerable<JToken> unpalist = conf["user_list"].Children();
  102.                 // 登录用户
  103.                 foreach (JToken t in unpalist)
  104.                 {
  105.                     try
  106.                     {
  107.                         // 登录
  108.                         BDUSS bl = new BDUSS(t["name"].Value<String>(), t["passwd"].Value<String>(), "", "");
  109.                         // 遍历喜欢吧
  110.                         ListBarResult lbr = bl.listLike(1);
  111.                         foreach (baseBar bb in lbr.likebars)
  112.                         {
  113.                             doneLeft++;
  114.                             // 增加签到任务
  115.                             new Thread(new SignWalker(bl, bb).threadProc).Start();
  116.                             Thread.Sleep(120);
  117.                         }
  118.                     }
  119.                     catch (TiebaField e)
  120.                     {
  121.                         Program.log(t["name"].Value<String>() + " " + e.error_code + ": " + e.error_msg + " 于" + DateTime.Now.ToLongTimeString());
  122.                     }
  123.                     catch (Exception e)
  124.                     {
  125.                         Program.log(t["name"].Value<String>() + " " + e.ToString() + " 于" + DateTime.Now.ToLongTimeString());
  126.                     }
  127.                 }
  128.             }
  129.             catch (Exception e)
  130.             {
  131.                 Program.log(e.ToString());
  132.             }
  133.             while (doneLeft > 0)
  134.             {
  135.                 Console.Write("还有" + doneLeft + "个...\n");
  136.                 Thread.Sleep(500);
  137.             }
  138.             fslog.Close();
  139.         }
  140.         public static void log(String s)
  141.         {
  142.             byte[] pb = Encoding.UTF8.GetBytes(s + "\r\n");
  143.             lock (fslog)
  144.             {
  145.                 fslog.Write(pb, 0, pb.Length);
  146.             }
  147.         }
  148.     }
  149. }
复制代码







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2