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

[复制链接]
发表于 2014-2-4 03:09:19 | 显示全部楼层 |阅读模式
大家最近都在热议古城钟楼的红火。不是加V号、不是官方号、不是皇冠会员。没有拟人化,没有段子、没有运营。1天之内从1万不到的粉丝新增了20多万并且还在持续传播和增长粉丝。我不研究它是怎么红起来的,但写个类似的程序实现它却很容易,程序才100行左右:
如果看不懂下面的代码,可以去看看这个 [科普贴]古城钟楼的微博报时是如何实现的?
* L9 V! Y8 x- G' [1 W
先来普及一下地支时间与北京时间的对应关系:
  • 【子时】夜半,又名子夜、中夜:十二时辰的第一个时辰。(北京时间23时至次日01时)。
  • 【丑时】鸡鸣,又名荒鸡:十二时辰的第二个时辰。(北京时间01时至03时)。
  • 【寅时】平旦,又称黎明、早晨、日旦等:时是夜与日的交替之际。(北京时间03时至05时)。
  • 【卯时】日出,又名日始、破晓、旭日等:指太阳刚刚露脸,冉冉初升的那段时间。(北京时间05时至07时)。
  • 【辰时】食时,又名早食等:古人“朝食”之时也就是吃早饭时间,(北京时间07时至09时)。
  • 【巳时】隅中,又名日禺等:临近中午的时候称为隅中。(北京时间09时至11时)。
  • 【午时】日中,又名日正、中午等:(北京时间11时至13时)。
  • 【未时】日昳,又名日跌、日央等:太阳偏西为日跌。(北京时间13时至15时)。
  • 【申时】哺时,又名日铺、夕食等:(北京时间15时至17时)。
  • 【酉时】日入,又名日落、日沉、傍晚:意为太阳落山的时候。(北京时间17时至19时)。
  • 【戌时】黄昏,又名日夕、日暮、日晚等:此时太阳已经落山,天将黑未黑。天地昏黄,万物朦胧,故称黄昏。(北京时间19时至21时)。
  • 【亥时】人定,又名定昏等:此时夜色已深,人们也已经停止活动,安歇睡眠了。人定也就是人静。(北京时间21时至23时)。
    6 F4 Q3 S" p% S% ]/ u" D: B
$ ^; G: v. H6 F2 z2 M! q+ M7 s
然后我们来分析一下它昨天发布的内容
  • 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 【亥时】铛~铛~铛~铛~铛~铛~铛~铛~铛~铛~3 r4 Q# n7 U( E9 C$ f" Z/ o( Y. r3 M

5 @/ [9 w$ J* D' h1 ~" @3 {
根据上面,得出它有以下规律:
  • 每两个小时发布一条微博,
  • 发布时间是偶数小时的0分,这个时间并不是地支时间的开始时间,而是正中间的那个时间点
  • 内容是相应的地支时间,加上根据时间而变化的“铛~”的次数,
  • “铛~”的次数与时有关,12小时以前与小时相同,12小时以后与[小时减12]相同。; {2 y3 C3 P  |  p$ {# ~0 W" }
西安钟楼里的钟就是这样敲的吗?去过钟楼一次,没登上去,也没注意。
4 H  x& ~7 Q& A2 C- F2 J/ L
完整代码如下:
需要自行修改App相关值:
8 g0 H* {% r+ J  Z  ~) q

  1. ( V- I1 u7 w! h# V  h0 q5 W
  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;% @# y# \( D6 j
  2. using System.Collections.Generic;* b  K2 S6 t3 @: e# J6 L
  3. using System.Text;
    ' }" k" b# b5 ?! T7 i7 ?9 C
  4. using NetDimension.Weibo;3 j7 S+ K- d9 l+ i* H1 \
  5. using System.Timers;
    & _( d( K: y4 ~" p$ U" n
  6. using System.Globalization;
    9 M, B6 t. F. @1 e
  7. / F" z9 I6 Q" ~8 H' w
  8. namespace WeiboTimer
    ' ?7 y: R  X$ h2 j% m
  9. {$ M# @% T9 o. v: W) Q# @! l
  10.     class Program; l5 s: T: n( k  @* b- ~
  11.     {; W; `, m6 k' ]6 a5 J
  12.         static string AppKey = "********";( j0 l: J" M8 i! S
  13.         static string AppSecret = "*******";2 Q- R+ M, w7 X: A- q0 ^
  14.         static string CallbackUrl = "****************";
    8 }* L# E. `* [% i
  15.         static Timer t = new Timer();
    5 \2 [* i# v+ C/ v
  16.         static OAuth oauth = null;! K& f' i2 g# ~/ }
  17.         static Client Sina = null;
    ) N" _- w1 D3 s* \; c  n9 _

  18. 6 R7 V1 z; o3 h4 G+ }' u$ g( p6 A
  19.         static void Main(string[] args)' q% G9 Z. V, K
  20.         {            
    ; l8 Z1 T/ a( A' q  u2 G* Y6 o
  21.             oauth = Authorize();
    : j6 q; @. t$ |2 N
  22.             if (!string.IsNullOrEmpty(oauth.AccessToken)){Console.Write("登录成功!");}+ v! w) V+ [; m; m% s

  23. % I3 v% }  u( ]
  24.             Sina = new Client(oauth);0 g6 a- K+ V* `8 v9 p0 f  B, X9 P
  25.             string uid = Sina.API.Entity.Account.GetUID();" y8 K9 p7 V) E1 J
  26.             var entity_userInfo = Sina.API.Entity.Users.Show(uid);
    $ ]5 V7 ]8 Q: h
  27.             Console.WriteLine("昵称:{0},微博地址:http://weibo.com/{1}", entity_userInfo.ScreenName, entity_userInfo.ProfileUrl);" C* s4 s- c- W, F- _8 U

  28. - r! W1 e: V7 b3 v4 ^8 @3 P
  29.             try
    " p+ J! P, w: O( H# k# T: F
  30.             {( N( }, z" Z, l7 |2 ~8 `
  31.                 t.Interval = 1000 * 60;     //设置间隔时间为1分钟, R$ Q1 `* E9 c1 ]6 V
  32.                 t.Elapsed += new ElapsedEventHandler(TimerElapsedEvent); //到达时间的时候执行事件;
    % l5 N- h3 n- a$ p5 _. c
  33.                 t.AutoReset = true;         //设置是执行一次(false)还是一直执行(true);
    ( f0 ~5 _' |) X# S+ ~8 [
  34.                 t.Start();                  //启动Timer对象; . [* P  ]; R! ]0 h( J/ i; |! q2 a
  35.             }( K* I$ `: F9 @
  36.             catch (Exception ex)
    5 k& R3 k, y! v/ o! G
  37.             {3 P9 @# ~! i& y2 A$ K8 q- l
  38.                 Console.WriteLine("定时任务异常:" + ex.Message);3 Q' d1 t- A2 W+ c
  39.                 t.Stop();
    0 h0 L* Z) w0 R! m2 L) h3 X0 h
  40.             }% e( D  A+ Q( V- S8 |$ U4 r
  41.             Console.WriteLine("定时任务开始工作。。。");" H; |/ u/ ]. K0 S/ v" \
  42.             Console.ReadKey();
    . q0 b% N0 P- W0 J
  43.         }* ]# \4 W% ?$ r
  44. . N+ ]$ }4 [8 Y8 v! n  V2 z+ n
  45.         static OAuth Authorize()
    0 ]) j) E7 U9 L$ b
  46.         {& S5 W) U" V$ D, ~. n
  47.             OAuth o = new OAuth(AppKey, AppSecret, CallbackUrl);) K9 D! C1 n! o; Q$ a* |/ w
  48.             while (!ClientLogin(o))% m- a- \5 _3 a& a+ w" i( k0 U2 ]2 ]8 m
  49.             {
    + o0 n9 a; o: q" e0 ^+ ^
  50.                 Console.WriteLine("登录失败,请重试。");
    9 j1 H) b& K3 @/ {; [
  51.             }
    . a( R- I1 {, {  y6 u9 o8 a
  52.             return o;
    8 |4 P5 _3 p( e7 k
  53.         }
    ! T* B1 D7 }: N1 n) X( d

  54. ! Q- I! I& {( s0 _: z' W
  55.         private static bool ClientLogin(OAuth o)
    # D6 s  M- K% I) a; a4 g
  56.         {0 i, c6 U& Y/ `: H; d; s$ \
  57.             Console.Write("微博账号:");
    : u! ~7 R0 {4 i4 g7 S- ~& p
  58.             string account = Console.ReadLine();
    1 @6 }1 u' b7 X! A$ H2 c7 h  g
  59.             Console.Write("登录密码:");/ Y1 G1 m+ n3 ?+ O+ d8 S1 q
  60.             ConsoleColor originColor = Console.ForegroundColor;$ J. A6 z( j$ @3 x8 l
  61.             Console.ForegroundColor = Console.BackgroundColor; //知道这里是在干啥不?其实是为了不让你们看到我的密码^_^2 J5 E+ j% P5 h3 M  c
  62.             string password = Console.ReadLine();
    8 Z. d% X+ }5 a# _# ^# E
  63.             Console.ForegroundColor = originColor; //恢复前景颜色。
    " Q* }9 _; C$ {! D; B* n* D  J
  64.             return o.ClientLogin(account, password);
      Q5 t# q0 Q" y' r' y) k( U1 l5 J$ G
  65.         }/ t/ G/ i$ U  ~
  66. 3 V4 _1 f- B" j5 Q. S
  67.         static void TimerElapsedEvent(object sender, ElapsedEventArgs e)& l* m- Q- o, x4 S# ^
  68.         {" a, _% u& R& ~
  69.             DateTime iNow = DateTime.Now;. H3 l" K& @5 V6 M
  70.             try
    7 A) R$ B, s( s" g- f( |) s
  71.             {" \- K* c7 H8 s4 ?- q
  72.                 string content = makeContent(iNow.Hour, iNow.Minute);) X( g% G0 D' R" J& R
  73.                 if (!string.IsNullOrEmpty(content))! Q$ H+ d% O, Y+ O. t6 L5 T
  74.                 {
    - B# }( j- R7 [  t
  75.                     var statusInfo = Sina.API.Entity.Statuses.Update(content);2 X/ v& P  v$ M* D" [' ~% V6 L1 @
  76.                     DateTime dtCreate = DateTime.ParseExact(statusInfo.CreatedAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
    # w) M, z0 n5 U
  77.                     Console.WriteLine("本机时间:{0}, 微博时间:{1}, 发布内容:{2}", string.Format("{0:G}", iNow), string.Format("{0:G}", dtCreate), statusInfo.Text);% d3 @* Q' a: S& u$ M
  78.                 }
    & ^# {7 R/ a* b
  79.             }
    % F; b; a# x- c1 d! j; n6 R! `* a: G
  80.             catch (Exception ex)9 k- N) x2 \- u/ w* X
  81.             {, }5 q: H4 V4 ~, W
  82.                 Console.WriteLine("定时任务异常:" + ex.Message);: Y. f$ ]  ?) X8 g6 a4 x- o
  83.             }
    ' `8 x% G) C3 Y! ^. T% e: d1 m
  84.         }: z. U( {) K1 G/ X( f+ w
  85.   }" @2 B$ N7 z& g6 B/ q% t0 M
  86.         static string makeContent(int hour24, int minute)/ m* H+ {! P% o- d+ u& }, i# a
  87.         {
    $ k+ ]8 T4 v+ X+ a
  88.             string[] diZhi = "子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥".Split('|');
    $ ~9 f3 z7 L4 @8 m
  89.             string extWeibo = "";
    / a5 v' s; J$ Y  U1 d/ M5 P
  90.             if (hour24 % 2 == 0 && minute == 0)
      R6 s% {. R0 z3 ]/ a& N, h  |8 y3 Z
  91.             {
    1 I+ @7 Y# F6 k$ Q
  92.                 for (int i = 0; i < ((hour24 >= 12)? (hour24 - 12): hour24); i++)
    5 r* B5 Y. b3 v9 X2 c% i
  93.                 {
    : v: ?- p3 B) g) Y8 B' w
  94.                     extWeibo += "铛~";5 h. W6 V) T) y8 j
  95.                 }% N: k; E6 H6 I( m* T
  96.                 return "【" + diZhi[hour24 / 2] + "时】" + extWeibo;
    " u8 v9 O/ ?  u1 r) p. `( u
  97.             }
    : Y* O6 s. q5 h, f
  98.             else1 n7 C& V. d$ }5 x; ]2 ]! O4 G& \8 w$ _
  99.             {1 P/ I( Q9 B4 p( ~8 P) L7 Z. V
  100.                 return "";6 x# q6 J! }3 y
  101.             }
    7 p) N4 ]9 `( d$ @
  102.         }$ I% y* V" v8 P9 E$ b8 Y" E( o$ J0 u; H
  103.     }) C( g  J- {; P, ~/ n* t
  104. }
复制代码
完整程序下载:
运行前需要安装.NET Framework。 点击这里下载.NET Framework 4 并安装,这个有40多M
+ p& |( `1 f8 @" ]* R然后下载我的程序:guchengzhonglou.rar (已经内置Weico.iPhone了哦,亲)$ \; f' d* H5 G1 s( i/ q

) {/ A0 `: j8 G) A




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

使用道具 举报

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

使用道具 举报

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

支付宝扫一扫打赏

微信扫一扫打赏

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