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

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

. {8 R1 k/ h. N
然后我们来分析一下它昨天发布的内容
  • 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 【亥时】铛~铛~铛~铛~铛~铛~铛~铛~铛~铛~) E8 d3 p8 p9 J' F# ]

( T0 j3 Q+ [3 L8 R. N9 @- @) u
根据上面,得出它有以下规律:
  • 每两个小时发布一条微博,
  • 发布时间是偶数小时的0分,这个时间并不是地支时间的开始时间,而是正中间的那个时间点
  • 内容是相应的地支时间,加上根据时间而变化的“铛~”的次数,
  • “铛~”的次数与时有关,12小时以前与小时相同,12小时以后与[小时减12]相同。
    ; r% T8 n/ B# p0 p7 B
西安钟楼里的钟就是这样敲的吗?去过钟楼一次,没登上去,也没注意。

9 z- H* B# B! Q1 H( t2 @8 N3 k
完整代码如下:
需要自行修改App相关值:
& s0 H" T) j, K3 {' Z
  1. - H4 C3 U7 A2 H0 S! W8 ?, A
  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;0 x7 ?. l/ W3 G/ L: }$ h0 R
  2. using System.Collections.Generic;
    , j, x9 Q( K6 ]
  3. using System.Text;9 w8 s8 a$ J2 H  J, c. E
  4. using NetDimension.Weibo;
    ( _* d- s' i6 b: f
  5. using System.Timers;
    ! k, R4 ~/ W2 g# N0 j
  6. using System.Globalization;
    * p% k1 {# x8 q9 ~8 _. A( A9 p
  7. # c/ K# N# B! E! f! C, p; o
  8. namespace WeiboTimer
    3 [! n. t4 J8 |+ Q! k
  9. {
    * Q8 s% z0 I( l* h' ^
  10.     class Program9 v: f, t# E, k. W6 Q' X* U
  11.     {
    2 S8 z' n" S7 B! R# s% M/ G" ^
  12.         static string AppKey = "********";
    ; D0 d! J- b$ t  g6 v2 b
  13.         static string AppSecret = "*******";& C' Z& z5 M% F% p. v$ @( R+ t0 o: J
  14.         static string CallbackUrl = "****************";
    , `7 f( ?1 |8 B& N, O
  15.         static Timer t = new Timer();
    ' s+ Q& x4 k, @" J; \7 L
  16.         static OAuth oauth = null;( y* ?8 r8 ]8 q8 C% c
  17.         static Client Sina = null;
    ; D3 Y( W4 |  F8 O# b
  18. / Y: d8 B5 _, T7 d  [( Z7 L) Q
  19.         static void Main(string[] args)' m( s5 l, N- D' }9 V6 W
  20.         {            
    ; @. s8 @" s6 o% g7 I3 L
  21.             oauth = Authorize();: }4 W& J  b* A, `, P. B7 T
  22.             if (!string.IsNullOrEmpty(oauth.AccessToken)){Console.Write("登录成功!");}& E, q) X% Z6 f# X5 Z  y5 y  ~
  23. ' u$ a' b( S* y0 N2 X2 m
  24.             Sina = new Client(oauth);7 o9 |5 c3 w* \6 I+ l2 B
  25.             string uid = Sina.API.Entity.Account.GetUID();! b+ I/ O& A" s
  26.             var entity_userInfo = Sina.API.Entity.Users.Show(uid);
    $ j: A6 {* R$ j& t
  27.             Console.WriteLine("昵称:{0},微博地址:http://weibo.com/{1}", entity_userInfo.ScreenName, entity_userInfo.ProfileUrl);
    & o& m7 q$ Z  ~  o& n6 ?

  28. 1 @2 }  ?' K& {+ ?' t0 t
  29.             try5 x4 J; \, a% Q
  30.             {+ C" g7 K0 G' j( Y: B
  31.                 t.Interval = 1000 * 60;     //设置间隔时间为1分钟
    + B# _) R: [& h3 r& `4 D) i
  32.                 t.Elapsed += new ElapsedEventHandler(TimerElapsedEvent); //到达时间的时候执行事件; # C3 `" Q9 _+ b7 W% {% V* c$ R7 E, s
  33.                 t.AutoReset = true;         //设置是执行一次(false)还是一直执行(true);
    0 D+ R5 S! U" n
  34.                 t.Start();                  //启动Timer对象;
    3 [" v' K' @. \3 N# t* c  I0 L
  35.             }
    $ L% I' ?0 g  B  f9 N
  36.             catch (Exception ex)
    ! j4 s7 k% x* V: ~) Y1 r
  37.             {0 {+ }8 O/ H# d
  38.                 Console.WriteLine("定时任务异常:" + ex.Message);
    + C+ r* L! @* t# d; W/ b% D
  39.                 t.Stop();
    # T% m2 r, N# H; V0 D* o0 X
  40.             }
    0 W! y' U, F- t6 B# G
  41.             Console.WriteLine("定时任务开始工作。。。");8 E8 ?) H1 M. d; O6 {
  42.             Console.ReadKey();  v# [/ r- g& ?' j
  43.         }# M) A. S0 t- @5 n, l

  44. 9 W' b, p. s# u: Q4 U# k" [4 f
  45.         static OAuth Authorize()
    7 v# n& L, w/ G3 p2 s* N8 e& \8 u
  46.         {
    ; ^3 I4 Y9 V# l% B/ D
  47.             OAuth o = new OAuth(AppKey, AppSecret, CallbackUrl);2 V2 H7 A/ E, S* _. G
  48.             while (!ClientLogin(o))
    + K" ]4 K! T. Z3 \8 s2 ^# [
  49.             {
    + i- x; K& Y+ |# \- H- R
  50.                 Console.WriteLine("登录失败,请重试。");
    0 Z/ j! E5 K/ C
  51.             }
    0 k) f9 U7 G' \3 f, p: j
  52.             return o;
    . |3 s) h2 I) R! Z- z+ q; t1 C
  53.         }* x" ^* x6 ]2 _  R! B

  54. 7 D& P! k( o/ Z& e& f/ U
  55.         private static bool ClientLogin(OAuth o)+ R1 l( w: \- E) ~" G1 g
  56.         {
    & S1 [, _. `% d) h8 u9 D
  57.             Console.Write("微博账号:");
    : R& }4 w2 R( A
  58.             string account = Console.ReadLine();
    6 M7 Y% U: I/ K+ P+ y9 p
  59.             Console.Write("登录密码:");
    $ X/ M/ E3 W) k0 l; g6 V
  60.             ConsoleColor originColor = Console.ForegroundColor;8 Y1 n. x" }9 G
  61.             Console.ForegroundColor = Console.BackgroundColor; //知道这里是在干啥不?其实是为了不让你们看到我的密码^_^
    5 L" R( S( @- r* e' q
  62.             string password = Console.ReadLine();: Z, a8 O; ^  P% }5 F2 {3 i
  63.             Console.ForegroundColor = originColor; //恢复前景颜色。
    + [2 A7 |: V# t( z5 l3 f& c& J  [
  64.             return o.ClientLogin(account, password);
    % n, `! q+ q0 m$ `) Z& a$ k
  65.         }
    - l$ q  y5 M, i4 n* Q
  66. & h, x7 _6 z: r) r
  67.         static void TimerElapsedEvent(object sender, ElapsedEventArgs e)
    , b! f5 G) t/ S$ d8 g
  68.         {
    ( _$ U, M  |* ~) G" R. C
  69.             DateTime iNow = DateTime.Now;- d9 f2 ?' i4 d$ Z
  70.             try) E1 `# w: @2 {4 E, z4 k
  71.             {1 d6 i4 L, E. g! a- o
  72.                 string content = makeContent(iNow.Hour, iNow.Minute);
    . f) I( w2 n& [) R
  73.                 if (!string.IsNullOrEmpty(content))  a$ E" j3 ?" {  [9 J
  74.                 {+ j% r( D' g4 j  K
  75.                     var statusInfo = Sina.API.Entity.Statuses.Update(content);
    / q( V( ]! |2 E7 l: ~
  76.                     DateTime dtCreate = DateTime.ParseExact(statusInfo.CreatedAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);& X! J- _2 Q9 s3 O3 i
  77.                     Console.WriteLine("本机时间:{0}, 微博时间:{1}, 发布内容:{2}", string.Format("{0:G}", iNow), string.Format("{0:G}", dtCreate), statusInfo.Text);
    " [+ Y/ r, w7 W1 @" \
  78.                 }+ m; k$ w* W$ A- x9 L
  79.             }
    / G. O& F: q2 K/ o
  80.             catch (Exception ex)9 E5 A# n9 b' C5 m% [! h
  81.             {
    ) }  b2 z7 K/ z4 J& X9 q
  82.                 Console.WriteLine("定时任务异常:" + ex.Message);# ^3 C, v/ w$ k6 m$ c+ G! |
  83.             }
    % @; c$ k5 Q- B% H7 [/ y& T' ~
  84.         }
    % j- w9 S8 x: ?- @; R- W) l5 ~4 @

  85. * ?% `" n% Q6 Y. c$ R! U
  86.         static string makeContent(int hour24, int minute)
    ! S6 L& t1 `8 i. l* p1 V; q
  87.         {
    : n4 n9 f4 `' x* O7 w. `
  88.             string[] diZhi = "子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥".Split('|');: e. E  A* H* F' z8 b: S3 w
  89.             string extWeibo = "";
    3 W' J1 f; [( S
  90.             if (hour24 % 2 == 0 && minute == 0)
    ' I5 `7 M) ?. `8 ?( j% S
  91.             {- O$ p4 w5 R0 Z" i
  92.                 for (int i = 0; i < ((hour24 >= 12)? (hour24 - 12): hour24); i++)
    6 c" s# g8 S( `4 g8 @
  93.                 {8 g8 y8 q2 c4 {; w- f4 S
  94.                     extWeibo += "铛~";
    : l( l; ]! Y: q4 b* b
  95.                 }
    8 I$ J# j0 \4 G% N  Q* {
  96.                 return "【" + diZhi[hour24 / 2] + "时】" + extWeibo;
    6 {9 Z4 ]7 p5 T" O7 ?4 N& C
  97.             }/ u6 ^/ K9 E6 B  e7 R
  98.             else$ N7 x6 ^5 f$ v( c
  99.             {* X6 {/ [* `- H8 Q( F& V' V
  100.                 return "";
    6 D7 n& C2 T$ h
  101.             }; w5 F* T) T" t8 J% b
  102.         }2 h1 k3 M8 C; c7 T
  103.     }( R+ n% U: e- T& ]1 T8 U" k
  104. }
复制代码
完整程序下载:
运行前需要安装.NET Framework。 点击这里下载.NET Framework 4 并安装,这个有40多M
, C# v- S/ @3 L8 C1 i3 T然后下载我的程序:guchengzhonglou.rar (已经内置Weico.iPhone了哦,亲)
( z4 u9 N7 z# z3 m& r- T

8 E1 \" \# \; Q4 |. G- A, C! e




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

使用道具 举报

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

使用道具 举报

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

支付宝扫一扫打赏

微信扫一扫打赏

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