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

[复制链接]
发表于 2014-2-4 03:09:19 | 显示全部楼层 |阅读模式
大家最近都在热议古城钟楼的红火。不是加V号、不是官方号、不是皇冠会员。没有拟人化,没有段子、没有运营。1天之内从1万不到的粉丝新增了20多万并且还在持续传播和增长粉丝。我不研究它是怎么红起来的,但写个类似的程序实现它却很容易,程序才100行左右:
如果看不懂下面的代码,可以去看看这个 [科普贴]古城钟楼的微博报时是如何实现的?
9 E- v  _9 K  j7 D" |) A. X- M
先来普及一下地支时间与北京时间的对应关系:
  • 【子时】夜半,又名子夜、中夜:十二时辰的第一个时辰。(北京时间23时至次日01时)。
  • 【丑时】鸡鸣,又名荒鸡:十二时辰的第二个时辰。(北京时间01时至03时)。
  • 【寅时】平旦,又称黎明、早晨、日旦等:时是夜与日的交替之际。(北京时间03时至05时)。
  • 【卯时】日出,又名日始、破晓、旭日等:指太阳刚刚露脸,冉冉初升的那段时间。(北京时间05时至07时)。
  • 【辰时】食时,又名早食等:古人“朝食”之时也就是吃早饭时间,(北京时间07时至09时)。
  • 【巳时】隅中,又名日禺等:临近中午的时候称为隅中。(北京时间09时至11时)。
  • 【午时】日中,又名日正、中午等:(北京时间11时至13时)。
  • 【未时】日昳,又名日跌、日央等:太阳偏西为日跌。(北京时间13时至15时)。
  • 【申时】哺时,又名日铺、夕食等:(北京时间15时至17时)。
  • 【酉时】日入,又名日落、日沉、傍晚:意为太阳落山的时候。(北京时间17时至19时)。
  • 【戌时】黄昏,又名日夕、日暮、日晚等:此时太阳已经落山,天将黑未黑。天地昏黄,万物朦胧,故称黄昏。(北京时间19时至21时)。
  • 【亥时】人定,又名定昏等:此时夜色已深,人们也已经停止活动,安歇睡眠了。人定也就是人静。(北京时间21时至23时)。
    : j! c* G$ g7 ~, G. J
7 w; b; I7 S+ ]) p& x" F- P
然后我们来分析一下它昨天发布的内容
  • 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 【亥时】铛~铛~铛~铛~铛~铛~铛~铛~铛~铛~) }$ S$ x  ~+ M
; O& c: c0 Z- X& i( q: P, O
根据上面,得出它有以下规律:
  • 每两个小时发布一条微博,
  • 发布时间是偶数小时的0分,这个时间并不是地支时间的开始时间,而是正中间的那个时间点
  • 内容是相应的地支时间,加上根据时间而变化的“铛~”的次数,
  • “铛~”的次数与时有关,12小时以前与小时相同,12小时以后与[小时减12]相同。
    , o$ l# W- z: Y% t2 F1 s
西安钟楼里的钟就是这样敲的吗?去过钟楼一次,没登上去,也没注意。

' Z7 t8 y' O; K7 R6 B6 `7 l% A
完整代码如下:
需要自行修改App相关值:

* m, C0 A& D2 P3 k$ ]- m
  1. # S, e, G/ W. K
  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;
    5 ^& ~( `# q1 Z' Q* E8 t( t
  2. using System.Collections.Generic;
    + _7 I% I/ r5 k6 H: y
  3. using System.Text;8 C" _4 q# ]% _  Q3 f
  4. using NetDimension.Weibo;/ G5 e" Z' ]7 b- d
  5. using System.Timers;
    + d$ R( r# e. W/ o5 i
  6. using System.Globalization;3 v: V! E7 d% z9 e

  7. 3 e$ d+ t) R) K' e
  8. namespace WeiboTimer# h/ ^. n+ G% y# u* e
  9. {
    3 d/ R+ |, p* W# D' r$ `. h1 G
  10.     class Program
    7 A& b, |: \1 H2 Z7 ]* j! W* e
  11.     {
    2 N. u5 A( L5 W3 l; P
  12.         static string AppKey = "********";
    3 g' D2 j% K6 j( J( A; w6 B3 X8 l3 y
  13.         static string AppSecret = "*******";/ w- T% n0 v, R
  14.         static string CallbackUrl = "****************";
    - W% [  P; V1 @3 o. o( k- g
  15.         static Timer t = new Timer();2 g  [1 Y8 D! T
  16.         static OAuth oauth = null;" j3 F! D  N# o0 a# i0 A) d6 j
  17.         static Client Sina = null;
      O0 U; a% i9 V$ z0 c# c& J! ~

  18. + C7 n: C, M9 l  y
  19.         static void Main(string[] args)
    . Z7 E+ q8 _9 n4 B
  20.         {            
    " ^  b/ V5 Y* g6 S
  21.             oauth = Authorize();
      e* R$ N  B& ?2 Q/ P7 W
  22.             if (!string.IsNullOrEmpty(oauth.AccessToken)){Console.Write("登录成功!");}
    0 I, ~$ y2 t1 q! J/ M$ W+ H' X
  23. * ?1 T* y& w8 d
  24.             Sina = new Client(oauth);9 D+ y4 r" F$ j/ Z
  25.             string uid = Sina.API.Entity.Account.GetUID();1 m1 q! U, O  ?0 H# ]/ X
  26.             var entity_userInfo = Sina.API.Entity.Users.Show(uid);
    7 W  W$ M4 g; v3 D
  27.             Console.WriteLine("昵称:{0},微博地址:http://weibo.com/{1}", entity_userInfo.ScreenName, entity_userInfo.ProfileUrl);" a9 [: H+ z4 v) s9 B
  28. 6 a9 k) m" k. l0 N$ g7 _
  29.             try
    - e0 ?9 r* x/ C. Q0 C8 a4 g
  30.             {$ x, x) t+ M* [
  31.                 t.Interval = 1000 * 60;     //设置间隔时间为1分钟
      w) q/ B6 R; @* j
  32.                 t.Elapsed += new ElapsedEventHandler(TimerElapsedEvent); //到达时间的时候执行事件;
    9 h6 l, a1 w. R+ W- _9 S( j
  33.                 t.AutoReset = true;         //设置是执行一次(false)还是一直执行(true); 6 |# ?8 s3 m% s  Y
  34.                 t.Start();                  //启动Timer对象;
    , A$ Z1 ~' A" ]+ F$ {
  35.             }' w) a. C0 L; N3 c. a/ F
  36.             catch (Exception ex)1 g- J. p" K! A. h7 ]. M: t+ I$ E: M
  37.             {+ F7 L9 x6 \3 X' c3 s- a2 l$ X  @4 r
  38.                 Console.WriteLine("定时任务异常:" + ex.Message);0 `' o& ^4 m* x, j; @) g
  39.                 t.Stop();
    6 p9 B4 o6 |: f& ^+ y
  40.             }
    ' Y; z3 f& Z% }% T4 X0 N5 b; v5 m3 R2 @
  41.             Console.WriteLine("定时任务开始工作。。。");
    . M6 N* f% G8 m* L
  42.             Console.ReadKey();
    : x# S+ {: x6 s, z; M1 }
  43.         }
    % d9 M: ^1 ?9 m& T

  44. ' \: z3 N% [6 q
  45.         static OAuth Authorize()
    ( `& \2 k5 s% i. z& {/ ?/ _
  46.         {/ A  B5 m/ S' s- V, r! M4 A
  47.             OAuth o = new OAuth(AppKey, AppSecret, CallbackUrl);
    8 o% C, y, U+ y: T, r! F  `" M
  48.             while (!ClientLogin(o))+ P# K' F  d" ~
  49.             {
    * s$ }0 D0 L9 c6 G& c: _& x/ Y
  50.                 Console.WriteLine("登录失败,请重试。");
    / T( x& x: s+ c& Q9 D9 l
  51.             }& Z0 W, K" p* A; y
  52.             return o;
    : |0 W$ Z# V  A7 I" U! u8 c) Q- `
  53.         }
    + f! i  @; m% T1 v$ s+ m

  54. & |# ~4 o. S0 f8 \, O1 U9 B
  55.         private static bool ClientLogin(OAuth o)
    : }8 G. o: d7 q2 X
  56.         {3 x3 B9 {; U# p" |
  57.             Console.Write("微博账号:");# Q' B" `4 M' A& t' }* [5 b
  58.             string account = Console.ReadLine();8 ?  t2 w7 v. ^8 L& Q. X7 t9 `
  59.             Console.Write("登录密码:");
    $ P. H# _# H& u- k* D8 b
  60.             ConsoleColor originColor = Console.ForegroundColor;
    $ l0 M' Z6 S" Q  I. R6 a- e
  61.             Console.ForegroundColor = Console.BackgroundColor; //知道这里是在干啥不?其实是为了不让你们看到我的密码^_^1 C7 V& ?6 v8 l+ p: M/ e) d6 ]
  62.             string password = Console.ReadLine();9 Q% ?, K! Y  @6 G8 o, @
  63.             Console.ForegroundColor = originColor; //恢复前景颜色。9 r+ i8 A$ f; x# Q/ {' e2 u9 O
  64.             return o.ClientLogin(account, password);1 R" B2 P9 n% ~+ X3 A
  65.         }3 v5 r7 r, I5 w% \2 S3 c

  66. ; B: n5 [- H: b, e5 Z; I1 v8 k
  67.         static void TimerElapsedEvent(object sender, ElapsedEventArgs e)
    2 t. P8 c& z* B  ^0 d3 j3 V! v4 Z
  68.         {
    * M) n  W) F. K
  69.             DateTime iNow = DateTime.Now;
    ' P8 l/ M+ u  V3 E. j2 ^# v- ^
  70.             try
    ' J% I/ T  i" m' @: E
  71.             {
    ) S' C$ `8 f- u
  72.                 string content = makeContent(iNow.Hour, iNow.Minute);; `( u( l0 Q8 W/ J/ D% S
  73.                 if (!string.IsNullOrEmpty(content))8 ]3 M5 p5 G5 I. \. K
  74.                 {
    ! ~6 O9 G0 q: ]
  75.                     var statusInfo = Sina.API.Entity.Statuses.Update(content);9 L2 A  ~4 z2 `$ i
  76.                     DateTime dtCreate = DateTime.ParseExact(statusInfo.CreatedAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
    3 g2 [, |: H0 S9 @! v. ?& h7 o* L
  77.                     Console.WriteLine("本机时间:{0}, 微博时间:{1}, 发布内容:{2}", string.Format("{0:G}", iNow), string.Format("{0:G}", dtCreate), statusInfo.Text);
    1 H4 r! q& e& [
  78.                 }
    ) i2 R, x! I' q1 ^, P# x. f
  79.             }" Z& l" c% ^% ]( p( ?4 r% L) }
  80.             catch (Exception ex)
    ( I9 L/ x2 m9 a; L! Q& w
  81.             {
    9 q9 c; F$ S% _' U/ o; F9 F6 {! C" j
  82.                 Console.WriteLine("定时任务异常:" + ex.Message);+ g' _: |  ?4 H* R
  83.             }
    + ~  [, G2 |0 C- h8 j/ S- O
  84.         }
    ( |$ k% d0 B# G! e1 D# h8 k: H

  85. ; r- h( j6 G" [  X
  86.         static string makeContent(int hour24, int minute)
    : ]* e3 V/ r" u% J( L) N. s( L
  87.         {' _2 C" ?( f& ~- b' Q6 b$ S/ H
  88.             string[] diZhi = "子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥".Split('|');
    6 D+ I; Y5 A* N9 O8 e: A* ~$ B8 v
  89.             string extWeibo = "";# s5 S3 P/ S1 z( a0 [+ v" X  f1 j
  90.             if (hour24 % 2 == 0 && minute == 0)8 j: y( X- o( i3 ]& m
  91.             {
    . k2 p- K$ x$ t- H* X
  92.                 for (int i = 0; i < ((hour24 >= 12)? (hour24 - 12): hour24); i++)
    ) R" z; n" P4 M, I
  93.                 {. t2 c  W) n0 x5 T, O
  94.                     extWeibo += "铛~";: s: E4 V9 Y1 L" S6 {4 @
  95.                 }
    1 m4 [5 p' b) z6 p) v# Q1 I6 F# n. H9 a
  96.                 return "【" + diZhi[hour24 / 2] + "时】" + extWeibo;
      t% b) v) a7 J* y9 T. S% i: E
  97.             }5 j/ i* ]- b+ E3 ~! ~' n
  98.             else
    ; B. u: Z0 q: `. H- h) s
  99.             {
    % w: o+ @; B/ Y3 b' t- ]+ O! V& `
  100.                 return "";7 t- r& e, z8 s  p6 ~
  101.             }
    : \/ v' q: \, l; Q$ T
  102.         }+ C0 m6 T& X6 E$ c  L; W
  103.     }1 S9 X: c/ x' G: z* B  C
  104. }
复制代码
完整程序下载:
运行前需要安装.NET Framework。 点击这里下载.NET Framework 4 并安装,这个有40多M
+ a% o# x- A! F- T6 C4 @/ s然后下载我的程序:guchengzhonglou.rar (已经内置Weico.iPhone了哦,亲)- m, @. {6 t9 h+ T/ I) L8 }: I9 }
" u: u: `' _) J8 c- u* g




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

使用道具 举报

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

使用道具 举报

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

支付宝扫一扫打赏

微信扫一扫打赏

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