[微信查询接口] 古城钟楼微博地支报时程序铛,100行代码实现,价值一天20万粉丝

[复制链接]
发表于 2014-2-4 03:09:19 | 显示全部楼层 |阅读模式
大家最近都在热议古城钟楼的红火。不是加V号、不是官方号、不是皇冠会员。没有拟人化,没有段子、没有运营。1天之内从1万不到的粉丝新增了20多万并且还在持续传播和增长粉丝。我不研究它是怎么红起来的,但写个类似的程序实现它却很容易,程序才100行左右:
如果看不懂下面的代码,可以去看看这个 [科普贴]古城钟楼的微博报时是如何实现的?

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

: y4 \/ ~/ i1 g: f6 l$ x2 ^
然后我们来分析一下它昨天发布的内容
  • 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 【亥时】铛~铛~铛~铛~铛~铛~铛~铛~铛~铛~7 x! X2 U8 T3 m

$ m4 a/ ?( r2 b- p- \: c9 T* Z7 p
根据上面,得出它有以下规律:
  • 每两个小时发布一条微博,
  • 发布时间是偶数小时的0分,这个时间并不是地支时间的开始时间,而是正中间的那个时间点
  • 内容是相应的地支时间,加上根据时间而变化的“铛~”的次数,
  • “铛~”的次数与时有关,12小时以前与小时相同,12小时以后与[小时减12]相同。
    5 e3 i( w% g4 X- F
西安钟楼里的钟就是这样敲的吗?去过钟楼一次,没登上去,也没注意。

% k$ t; I0 E* l: C- ]# j. c" V
完整代码如下:
需要自行修改App相关值:

4 V6 |& ~5 `/ ~7 Z! r' M# y3 u
  1. 1 r6 K1 k5 Q  n3 ^
  2. <p style="font: 14px/25px verdana, Arial, Helvetica, sans-serif; margin: 10px auto; padding: 0px; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><span style="margin: 0px; padding: 0px; color: rgb(255, 0, 0);"></span> </p>
复制代码
  1. using System;. v! U/ C9 }3 K; K+ R. _& U6 b& H
  2. using System.Collections.Generic;1 ]" D6 W* D, g
  3. using System.Text;! }% a6 E9 z! `& O3 H
  4. using NetDimension.Weibo;: T- Y( m7 I" L& j
  5. using System.Timers;
    + C$ h9 q/ f% V, p+ L2 B
  6. using System.Globalization;
    * }+ T8 I0 D! C$ B* X8 w

  7. 0 c. S4 U5 g" Z6 \
  8. namespace WeiboTimer
    9 @5 k; a- T1 A0 E' [  J
  9. {2 T" J0 t0 F- j* l& d
  10.     class Program
    ; s) {/ g: w/ M  r
  11.     {
    1 f: J. K9 M. k5 p3 v! A
  12.         static string AppKey = "********";
    + p" d1 r9 I, {1 M& J
  13.         static string AppSecret = "*******";
    ; K  b7 l. v( h( m% l, T
  14.         static string CallbackUrl = "****************";
    % e6 V2 s6 N1 }  u- l/ P4 Z
  15.         static Timer t = new Timer();% |( R# G2 _6 s7 Z& S( v
  16.         static OAuth oauth = null;$ G. g+ R8 G3 S/ J) R& Y6 o
  17.         static Client Sina = null;
    $ _9 u! m' t, M* U8 K

  18. # s+ ^( B0 ?2 ?2 O0 x9 [( d
  19.         static void Main(string[] args)
    * {0 z" }6 A  ~+ I6 E+ ]- W& l0 v
  20.         {            
    7 ^  p" K& Y( _7 P( M$ m5 e; k
  21.             oauth = Authorize();
    - s/ y$ _" u7 l) F% e* Q8 G6 _' A
  22.             if (!string.IsNullOrEmpty(oauth.AccessToken)){Console.Write("登录成功!");}
    ( a# v# B# S5 `3 i; J3 K
  23. ! D6 |+ G5 R& D% p4 K/ ]
  24.             Sina = new Client(oauth);2 y) E. a& q$ v9 D3 b! u1 u
  25.             string uid = Sina.API.Entity.Account.GetUID();4 c  w! A& |5 G5 @; h* |
  26.             var entity_userInfo = Sina.API.Entity.Users.Show(uid);# ~9 h) Q: R0 n. V. J3 K
  27.             Console.WriteLine("昵称:{0},微博地址:http://weibo.com/{1}", entity_userInfo.ScreenName, entity_userInfo.ProfileUrl);  V$ H" W6 A4 h7 |' X8 Q! ?

  28. $ {5 O. i! o, [
  29.             try2 h2 k: g7 L: n/ ~* P, |  Q# a
  30.             {
    . i: I- V1 A" [- |
  31.                 t.Interval = 1000 * 60;     //设置间隔时间为1分钟
    - W+ R* T) q1 B/ y
  32.                 t.Elapsed += new ElapsedEventHandler(TimerElapsedEvent); //到达时间的时候执行事件; % E) a" N& z( g+ Q
  33.                 t.AutoReset = true;         //设置是执行一次(false)还是一直执行(true);
    4 l2 y- s$ h' u+ N# D1 M% K
  34.                 t.Start();                  //启动Timer对象; 3 [+ L6 |. F3 b- ?- K, A4 a
  35.             }0 J9 g: _2 }* ?3 g7 M, c
  36.             catch (Exception ex)
    ( y5 `9 J9 G* [( g1 ?
  37.             {
    2 S$ e3 e* h; _/ I
  38.                 Console.WriteLine("定时任务异常:" + ex.Message);
    # l: F: j, Y) K2 y4 a; U! [4 u
  39.                 t.Stop();
    + j2 j: p; _- ?6 z( R0 M( h7 [3 O1 l( k
  40.             }6 Z3 z" C/ N% L7 D7 X8 c
  41.             Console.WriteLine("定时任务开始工作。。。");
    . m# m+ n- k' a: n, K
  42.             Console.ReadKey();+ }' U0 ~3 O* t0 b$ C# l/ ?
  43.         }
    - A( x# T. e/ {: W& p% a

  44. , @2 c& r' f% l3 ^) o
  45.         static OAuth Authorize(), j8 t: |  t" ~: L9 P6 x/ g
  46.         {
    ; g1 r1 W) t  R: H3 I
  47.             OAuth o = new OAuth(AppKey, AppSecret, CallbackUrl);- }6 G, Q- a5 }, k
  48.             while (!ClientLogin(o))
    3 k# J. z6 z& T; E  g+ v5 w
  49.             {
    0 a+ s: d: D+ g
  50.                 Console.WriteLine("登录失败,请重试。");
    + Q* u' o4 F4 o/ |) Y
  51.             }5 P1 M* f7 g# n4 w; A! B
  52.             return o;
    7 s0 i: E8 \0 O! T' {/ j
  53.         }
    1 d- ^  A' n$ i: @; B2 l  ^

  54. 9 }/ [4 A( R) w7 `! r
  55.         private static bool ClientLogin(OAuth o)
    ' K- v6 k  X( v2 s
  56.         {
    * A1 ~- h7 |# K" f0 w
  57.             Console.Write("微博账号:");3 b& m- U- f6 x* H. {4 m# O/ D
  58.             string account = Console.ReadLine();5 v+ g7 b* ]+ M  b! t3 ?* t
  59.             Console.Write("登录密码:");& y/ s) ?6 `) C0 \4 k2 c
  60.             ConsoleColor originColor = Console.ForegroundColor;" K( R. g2 D& }0 ~1 I+ u) G
  61.             Console.ForegroundColor = Console.BackgroundColor; //知道这里是在干啥不?其实是为了不让你们看到我的密码^_^
    1 z! Z$ n2 b0 k3 @2 @; j( U, a
  62.             string password = Console.ReadLine();
    7 D$ H8 H: ~4 Y5 V/ A
  63.             Console.ForegroundColor = originColor; //恢复前景颜色。. h. M% q" ^& ~* a5 u6 ]* I! ]
  64.             return o.ClientLogin(account, password);
    " b/ Q0 z( C$ E5 Q
  65.         }: }; f& c5 o. u: R  D( V; F
  66. * z! R8 y2 S5 }5 A' {
  67.         static void TimerElapsedEvent(object sender, ElapsedEventArgs e)
    9 Q! |/ a  T: p. H3 }
  68.         {
    & m" m$ G& M9 n+ k3 q& S: v
  69.             DateTime iNow = DateTime.Now;
    6 j! k* T) r* ~% b% _4 a8 l4 b
  70.             try
    . s) s8 @2 o9 F3 f" X
  71.             {
    5 F, D4 q. o) e; [
  72.                 string content = makeContent(iNow.Hour, iNow.Minute);1 h. g* S  h+ @
  73.                 if (!string.IsNullOrEmpty(content))
    $ Z# I+ [, [8 b0 x/ F2 t& m" y
  74.                 {! a  B# Q* S1 h9 Z& c& ]% D# N% n
  75.                     var statusInfo = Sina.API.Entity.Statuses.Update(content);
    4 l: ^% Y6 {6 p: x' J+ o; [9 q
  76.                     DateTime dtCreate = DateTime.ParseExact(statusInfo.CreatedAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);% |0 [6 _& h& ~. _! V. y
  77.                     Console.WriteLine("本机时间:{0}, 微博时间:{1}, 发布内容:{2}", string.Format("{0:G}", iNow), string.Format("{0:G}", dtCreate), statusInfo.Text);
    2 S# p. P# v' k/ c( Y* p6 s& L
  78.                 }' b4 _% V8 c% s- w
  79.             }; |2 |0 b& |8 s; H) ]0 L( s5 ^
  80.             catch (Exception ex), U# e# O, [( @3 X" w, X' h
  81.             {/ D6 J  {  B7 m' p8 n8 v
  82.                 Console.WriteLine("定时任务异常:" + ex.Message);7 Q; x/ z% w2 E9 u: M
  83.             }5 w7 C6 @: z- ~+ A8 ]3 q* n
  84.         }2 V3 [* N3 z: T+ u6 ?
  85. + Q0 g5 T  |; n/ ?5 ~$ |8 p, n1 E: f
  86.         static string makeContent(int hour24, int minute)
    ! U; _, ^& A" T* d6 E' D2 @
  87.         {. O. V. c5 r/ X. g6 l! ?- T1 }/ B- m; l
  88.             string[] diZhi = "子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥".Split('|');4 R. [, S2 }' q* S" w
  89.             string extWeibo = "";- @$ O! ]5 j; P" @4 Y/ E* J# T8 M  \
  90.             if (hour24 % 2 == 0 && minute == 0)% }8 h" n9 V* V' p. o
  91.             {
    ; [: d) ?6 _2 C# b& v
  92.                 for (int i = 0; i < ((hour24 >= 12)? (hour24 - 12): hour24); i++)
    6 ^& `* [0 u. T7 T" u
  93.                 {
    ! y1 ~# a! j9 l8 \6 H
  94.                     extWeibo += "铛~";5 [* @+ X, h2 Q6 p( E
  95.                 }
    , F) u, z$ I& F* ~% {+ ]
  96.                 return "【" + diZhi[hour24 / 2] + "时】" + extWeibo;4 K& H, Z1 P: d4 G
  97.             }
      E# X9 W9 [8 Y
  98.             else
    8 J' X) a- O9 }9 u9 u
  99.             {) U: y( K/ J! f4 W- a- O
  100.                 return "";. C6 F' F7 Y+ j4 J
  101.             }$ \% e" R  k. H9 C6 P
  102.         }
    8 w0 {% Q6 R6 t! d" A( f! L
  103.     }
    ; y+ J6 P6 v/ m" O
  104. }
复制代码
完整程序下载:
运行前需要安装.NET Framework。 点击这里下载.NET Framework 4 并安装,这个有40多M ) S" R" f" V( @  ]* J
然后下载我的程序:guchengzhonglou.rar (已经内置Weico.iPhone了哦,亲)
4 V: V) M: ^$ G* I! ]2 }/ a( k

" Q& P" j$ k, W2 F5 x




上一篇:微信支付
下一篇:微信公众平台开发(66)人品计算
回复

使用道具 举报

发表于 2014-3-4 21:23:23 | 显示全部楼层
这呢吗啊啊
回复 支持 反对

使用道具 举报

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

快速回复 返回顶部 返回列表