`
445822357
  • 浏览: 740290 次
文章分类
社区版块
存档分类
最新评论

古城钟楼微博地支报时程序铛,100行代码实现

 
阅读更多

大家最近都在热议古城钟楼的红火。不是加V号、不是官方号、不是皇冠会员。没有拟人化,没有段子、没有运营。1天之内从1万不到的粉丝新增了20多万并且还在持续传播和增长粉丝。我不研究它是怎么红起来的,但写个类似的程序实现它却很容易,程序才100行左右:

我们来分析一下它发布的内容

复制代码
1月8日22:00来自Weico.iPhone    【亥时】铛~铛~铛~铛~铛~铛~铛~铛~铛~铛~
1月8日20:00来自Weico.iPhone    【戌时】铛~铛~铛~铛~铛~铛~铛~铛~
1月8日18:00来自Weico.iPhone    【酉时】铛~铛~铛~铛~铛~铛~
1月8日16:00来自Weico.iPhone    【申时】铛~铛~铛~铛~
1月8日14:00来自Weico.iPhone    【未时】铛~铛~
1月8日12:00来自Weico.iPhone    【午时】
1月8日10:00来自Weico.iPhone    【巳时】铛~铛~铛~铛~铛~铛~铛~铛~铛~铛~
1月8日08:00来自Weico.iPhone    【辰时】铛~铛~铛~铛~铛~铛~铛~铛~
1月8日06:00来自Weico.iPhone    【卯时】铛~铛~铛~铛~铛~铛~
1月8日04:00来自Weico.iPhone    【寅时】铛~铛~铛~铛~
1月8日02:00来自Weico.iPhone    【丑时】铛~铛~
1月8日00:00来自Weico.iPhone    【子时】
复制代码

它是
每两个小时发布一条微博,
发布时间是偶数小时的0分,
内容是相应的地支时间,
加上根据时间而变化的“铛~”的次数,
“铛~”的次数与时有关,12小时以前与小时相同,12小时以后与[小时减12]相同。

西安钟楼里的钟就是这样敲的吗?去过钟楼一次,没登上去,也没注意。

好了,开始了:
首先程序发博微得有App,古城钟楼微博发布使用的是Weico.iPhone,这是一个公开的App了,我们自己申请一个,定义如下:

static string AppKey = "12345678";
static string AppSecret = "7234545228a7626f7767778b1e249e6a";
static string CallbackUrl = "https://www.doucube.com/oauth2/default.html";

我们使用C#来实现这个程序,利用新浪微博官方推荐的Weibo API OAuth2 C#版

第一步,就是用API登录,下面是API中的源码,去掉了一些冗余的东东,

复制代码
static OAuth Authorize()
{
    OAuth o = new OAuth(AppKey, AppSecret, CallbackUrl);
    while (!ClientLogin(o))
    {
        Console.WriteLine("登录失败,请重试。");
    }
    return o;
}
复制代码

在这里实现手动填写微博账号和登录密码,同时登录密码不显示(前景色和背景色相同)

复制代码
private static bool ClientLogin(OAuth o)
{
    Console.Write("微博账号:");
    string account = Console.ReadLine();
    Console.Write("登录密码:");
    ConsoleColor originColor = Console.ForegroundColor;
    Console.ForegroundColor = Console.BackgroundColor; //知道这里是在干啥不?其实是为了不让你们看到我的密码^_^
    string password = Console.ReadLine();
    Console.ForegroundColor = originColor; 
    return o.ClientLogin(account, password);
}
复制代码

登录成功后,返回自己的uid,昵称,证明是自己登上去了

Sina = new Client(oauth);
string uid = Sina.API.Entity.Account.GetUID();
var entity_userInfo = Sina.API.Entity.Users.Show(uid);
Console.WriteLine("昵称:{0},微博地址:http://weibo.com/{1}", entity_userInfo.ScreenName, entity_userInfo.ProfileUrl);

接下来是一个比较重要的的定时任务, 我们使用一个定时器,因为要精确到分钟,所以程序需要每分钟执行一次,

复制代码
try
{
    t.Interval = 1000 * 60;     //设置间隔时间为1分钟
    t.Elapsed += new ElapsedEventHandler(TimerElapsedEvent); //到达时间的时候执行事件; 
    t.AutoReset = true;         //设置是执行一次(false)还是一直执行(true); 
    t.Start();                  //启动Timer对象; 
}
catch (Exception ex)
{
    Console.WriteLine("定时任务异常:" + ex.Message);
    t.Stop();
}
复制代码

再接下来就是根据小时和分钟判断当前要不要“铛~”一下,根据上面的分析,写一个函数,传入24小时制的时间和分钟,如果需要“铛~”,就返回要发布的微博内容:

复制代码
static string makeContent(int hour24, int minute)
{
    //地支时间做成数组
    string[] diZhi = "子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥".Split('|');
    string extWeibo = "";
    
    //当前是偶数小时且分钟为0 
    if (hour24 % 2 == 0 && minute == 0)
    {
        //根据小时来数敲多少钟
        for (int i = 0; i < ((hour24 >= 12)? (hour24 - 12): hour24); i++)
        {
            extWeibo += "铛~";
        }
        return "" + diZhi[hour24 / 2] + "时】" + extWeibo;
    }
    else
    {
        return "";
    }
}
复制代码

最后,如果当前需要敲钟,就把微博发布上去,如果发现发布时间和微博时间不同步怎么办?
这里把当前时间也打印出来,如果不同步,就把自己的时间调整成和微博时间一样吧。

if (!string.IsNullOrEmpty(content))
{
    var statusInfo = Sina.API.Entity.Statuses.Update(content);
    DateTime dtCreate = DateTime.ParseExact(statusInfo.CreatedAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
    Console.WriteLine("本机时间:{0}, 微博时间:{1}, 发布内容:{2}", string.Format("{0:G}", iNow), string.Format("{0:G}", dtCreate), statusInfo.Text);
}

程序测试时运行界面如下:

完整代码如下:

需要自行修改App相关值:

复制代码
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using NetDimension.Weibo;
  5 using System.Timers;
  6 using System.Globalization;
  7 
  8 namespace WeiboTimer
  9 {
 10     class Program
 11     {
 12         static string AppKey = "12345678";
 13         static string AppSecret = "7234545228a7626f7767778b1e249e6a";
 14         static string CallbackUrl = "https://www.doucube.com/oauth2/default.html";
 15 
 16         static Timer t = new Timer();
 17         static OAuth oauth = null;
 18         static Client Sina = null;
 19 
 20         static void Main(string[] args)
 21         {            
 22             oauth = Authorize();
 23             if (!string.IsNullOrEmpty(oauth.AccessToken))
 24             {
 25                 Console.Write("登录成功!");
 26             }
 27             Sina = new Client(oauth);
 28 
 29             try
 30             {
 31                 string uid = Sina.API.Entity.Account.GetUID();
 32                 var entity_userInfo = Sina.API.Entity.Users.Show(uid);
 33                 Console.WriteLine("昵称:{0},微博地址:http://weibo.com/{1}", entity_userInfo.ScreenName, entity_userInfo.ProfileUrl);
 34 
 35                 try
 36                 {
 37                     t.Interval = 1000 * 60;     //设置间隔时间为1分钟
 38                     t.Elapsed += new ElapsedEventHandler(TimerElapsedEvent); //到达时间的时候执行事件; 
 39                     t.AutoReset = true;         //设置是执行一次(false)还是一直执行(true); 
 40                     t.Start();                  //启动Timer对象; 
 41                 }
 42                 catch (Exception ex)
 43                 {
 44                     Console.WriteLine("定时任务异常:" + ex.Message);
 45                     t.Stop();
 46                 }
 47             }
 48             catch (WeiboException ex)
 49             {
 50                 Console.WriteLine("出错啦!" + ex.Message);
 51             }
 52 
 53             Console.WriteLine("定时任务开始工作。。。");
 54             Console.ReadKey();
 55         }
 56 
 57         static OAuth Authorize()
 58         {
 59             OAuth o = new OAuth(AppKey, AppSecret, CallbackUrl);
 60             while (!ClientLogin(o))
 61             {
 62                 Console.WriteLine("登录失败,请重试。");
 63             }
 64             return o;
 65         }
 66 
 67         private static bool ClientLogin(OAuth o)
 68         {
 69             Console.Write("微博账号:");
 70             string account = Console.ReadLine();
 71             Console.Write("登录密码:");
 72             ConsoleColor originColor = Console.ForegroundColor;
 73             Console.ForegroundColor = Console.BackgroundColor; //知道这里是在干啥不?其实是为了不让你们看到我的密码^_^
 74             string password = Console.ReadLine();
 75             Console.ForegroundColor = originColor; //恢复前景颜色。
 76             return o.ClientLogin(account, password);
 77         }
 78 
 79         public void logger(string content)
 80         {
 81             DateTime currentTime = DateTime.Now;
 82             Console.Write(currentTime.ToString("d") + " " + currentTime.ToString("T"));
 83             Console.WriteLine(" " + content);
 84         }
 85 
 86         static void TimerElapsedEvent(object sender, ElapsedEventArgs e)
 87         {
 88             DateTime iNow = DateTime.Now;
 89 
 90             try
 91             {
 92                 string content = makeContent(iNow.Hour, iNow.Minute);
 93                 if (!string.IsNullOrEmpty(content))
 94                 {
 95                     var statusInfo = Sina.API.Entity.Statuses.Update(content);
 96                     DateTime dtCreate = DateTime.ParseExact(statusInfo.CreatedAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
 97                     Console.WriteLine("本机时间:{0}, 微博时间:{1}, 发布内容:{2}", string.Format("{0:G}", iNow), string.Format("{0:G}", dtCreate), statusInfo.Text);
 98                 }
 99                 
100             }
101             catch (Exception ex)
102             {
103                 Console.WriteLine("定时任务异常:" + ex.Message);
104             }
105         }
106 
107         static string makeContent(int hour24, int minute)
108         {
109             string[] diZhi = "子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥".Split('|');
110             string extWeibo = "";
111             if (hour24 % 2 == 0 && minute == 0)
112             {
113                 for (int i = 0; i < ((hour24 >= 12)? (hour24 - 12): hour24); i++)
114                 {
115                     extWeibo += "铛~";
116                 }
117                 return "" + diZhi[hour24 / 2] + "时】" + extWeibo;
118             }
119             else
120             {
121                 return "";
122             }
123         }
124     }
125 }
复制代码

完整程序下载:

运行前需要安装.NET Framework。点击这里下载.NET Framework 4并安装,这个有40多M
然后下载我的程序:guchengzhonglou.rar(已经内置Weico.iPhone了哦,亲)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics