zoukankan      html  css  js  c++  java
  • 古城钟楼微博地支报时程序铛,100行代码实现,价值一天20万粉丝

    古城钟楼 微博 铛 古城钟楼微博 地支 报时 自动 一天20万粉丝 程序 
    作者:http://txw1958.cnblogs.com/
    原文:http://www.cnblogs.com/txw1958/archive/2013/01/09/guchengzhonglou-weibo-timer.html


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

    如果看不懂下面的代码,可以去看看这个 [科普贴]古城钟楼的微博报时是如何实现的? 

    先来普及一下地支时间与北京时间的对应关系:

    • 【子时】夜半,又名子夜、中夜:十二时辰的第一个时辰。(北京时间23时至次日01时)。  
    • 【丑时】鸡鸣,又名荒鸡:十二时辰的第二个时辰。(北京时间01时至03时)。  
    • 【寅时】平旦,又称黎明、早晨、日旦等:时是夜与日的交替之际。(北京时间03时至05时)。
    • 【卯时】日出,又名日始、破晓、旭日等:指太阳刚刚露脸,冉冉初升的那段时间。(北京时间05时至07时)。  
    • 【辰时】食时,又名早食等:古人“朝食”之时也就是吃早饭时间,(北京时间07时至09时)。
    • 【巳时】隅中,又名日禺等:临近中午的时候称为隅中。(北京时间09时至11时)。  
    • 【午时】日中,又名日正、中午等:(北京时间11时至13时)。  
    • 【未时】日昳,又名日跌、日央等:太阳偏西为日跌。(北京时间13时至15时)。  
    • 【申时】哺时,又名日铺、夕食等:(北京时间15时至17时)。  
    • 【酉时】日入,又名日落、日沉、傍晚:意为太阳落山的时候。(北京时间17时至19时)。 
    • 【戌时】黄昏,又名日夕、日暮、日晚等:此时太阳已经落山,天将黑未黑。天地昏黄,万物朦胧,故称黄昏。(北京时间19时至21时)。  
    • 【亥时】人定,又名定昏等:此时夜色已深,人们也已经停止活动,安歇睡眠了。人定也就是人静。(北京时间21时至23时)。

    然后我们来分析一下它昨天发布的内容

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

    完整程序下载: 

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

  • 相关阅读:
    gnats配置文件
    在Mac中安装python,配置python环境
    利用git bash和git gui向git远程仓库提交文件
    os模块
    django中的locale()函数
    django配置静态文件
    sso单点登录
    django get_object_or_404
    关于token
    Django的CSRF机制
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6467927.html
Copyright © 2011-2022 走看看