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

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

% L$ w3 e$ J, \4 c; t) F0 q
然后我们来分析一下它昨天发布的内容
  • 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 B" t9 R# b7 |" c* D( }

- \# L7 s% x( Z1 ^: u0 ?, o( S
根据上面,得出它有以下规律:
  • 每两个小时发布一条微博,
  • 发布时间是偶数小时的0分,这个时间并不是地支时间的开始时间,而是正中间的那个时间点
  • 内容是相应的地支时间,加上根据时间而变化的“铛~”的次数,
  • “铛~”的次数与时有关,12小时以前与小时相同,12小时以后与[小时减12]相同。; v# s( T' {# x1 W3 C5 r
西安钟楼里的钟就是这样敲的吗?去过钟楼一次,没登上去,也没注意。
& v3 a2 f# K7 Z( Y! L3 D7 W' t' A
完整代码如下:
需要自行修改App相关值:

7 P" L, B+ p+ Q1 a$ i" V1 u$ V

  1. ( {: d& n1 H/ C& E4 T/ a6 B
  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;
      C' P5 E: Q: U, v
  2. using System.Collections.Generic;
    # T: M6 J: |( U, q3 o, f% U
  3. using System.Text;: I. t8 r8 D9 Q- p, u0 z; D2 T) c8 t
  4. using NetDimension.Weibo;0 j4 G  v. {& ]& U; e
  5. using System.Timers;
    9 w; h: ?; n6 N8 y% o" r/ \6 Z
  6. using System.Globalization;* [1 l( |# M0 Z  b7 y

  7. - m% g3 U! O, [/ f" ^
  8. namespace WeiboTimer
    5 n4 N9 n8 `3 [! V
  9. {
    " K: O$ S; X* r2 r* Y4 o- s
  10.     class Program2 c6 u' G* l0 B( Z0 \6 R
  11.     {. P  Y" Y5 e1 P* {. V8 H0 |$ \
  12.         static string AppKey = "********";
    + U. ]* E$ ^' w4 \* i$ o
  13.         static string AppSecret = "*******";
    ) y# t6 Z$ O9 Q# t9 v* Z
  14.         static string CallbackUrl = "****************";8 B, N. z$ `" p; B+ v7 J2 S
  15.         static Timer t = new Timer();; f8 j" i4 e; e) H# Q9 q
  16.         static OAuth oauth = null;
    8 F- y; Y& j4 m2 z
  17.         static Client Sina = null;5 o( I4 @% Y4 M# `
  18. 7 |: i+ E$ s+ M+ n2 K  V1 e! `' I2 ~
  19.         static void Main(string[] args)0 |+ P5 @1 h2 G, w8 Z- ?5 K: q
  20.         {            
    6 W) c" L, T. z- i
  21.             oauth = Authorize();
    ) N+ x. W0 _: T# _2 H! w+ y8 `
  22.             if (!string.IsNullOrEmpty(oauth.AccessToken)){Console.Write("登录成功!");}
    3 ^' D" h( E6 Y
  23. . G( ~8 |" L' [. n9 o+ P1 O# [2 @
  24.             Sina = new Client(oauth);
    5 v, h& `/ z4 j+ ?4 x( ]$ N3 f
  25.             string uid = Sina.API.Entity.Account.GetUID();/ E9 C2 S4 r7 M) d3 B" H; e2 K: }. R. E
  26.             var entity_userInfo = Sina.API.Entity.Users.Show(uid);  w+ f0 ?) e! \( N2 n: P4 X
  27.             Console.WriteLine("昵称:{0},微博地址:http://weibo.com/{1}", entity_userInfo.ScreenName, entity_userInfo.ProfileUrl);
    % f: s9 R4 m0 z3 N/ e! U9 ^
  28. . T& z  o: g$ R' n# X/ [7 V
  29.             try2 c! t" j# q7 D  D$ c7 q- b
  30.             {
    5 A* F5 o: l" [
  31.                 t.Interval = 1000 * 60;     //设置间隔时间为1分钟* E' J& c' Y. G- y% |
  32.                 t.Elapsed += new ElapsedEventHandler(TimerElapsedEvent); //到达时间的时候执行事件;
    , ^! L% K9 n( x: x
  33.                 t.AutoReset = true;         //设置是执行一次(false)还是一直执行(true);
      {+ ]( e; l8 u( I$ h
  34.                 t.Start();                  //启动Timer对象;   S1 T6 O- P" |% j+ r. [$ x
  35.             }9 P/ z, T/ ^( f# E
  36.             catch (Exception ex)& N. H2 B1 g4 |
  37.             {8 c9 `. W4 l5 ^$ c1 K4 K7 d
  38.                 Console.WriteLine("定时任务异常:" + ex.Message);
    1 V+ i2 I  J9 B2 C" i' m, n
  39.                 t.Stop();
      z- R- D% P, K) i9 ]2 ?
  40.             }- B" s4 A2 X3 T) J- J1 G
  41.             Console.WriteLine("定时任务开始工作。。。");' m7 L. Y5 o2 H
  42.             Console.ReadKey();
    + p- {- [& R: J  I/ P$ G8 p
  43.         }: x: n/ r( e7 g, U, ]
  44.   e7 j+ @3 M% R8 K* Q
  45.         static OAuth Authorize()
    & w! P( i" P. ~" |' y1 @
  46.         {
    * ?7 D! O: @- t6 R% ?$ s! _
  47.             OAuth o = new OAuth(AppKey, AppSecret, CallbackUrl);. W- S" Z( D% f
  48.             while (!ClientLogin(o))) g8 u+ Z2 N, w+ W
  49.             {
    ) v. u) }  D: X8 d4 h8 k# J7 W
  50.                 Console.WriteLine("登录失败,请重试。");" K6 l, W' r. ^
  51.             }
    ! r) V; u) s9 |" S' q
  52.             return o;
    $ o3 m  z  U. [7 |
  53.         }
    7 c* Y" |% q2 d3 n  Y

  54. 1 [3 `# g* Z: K* y" M" l
  55.         private static bool ClientLogin(OAuth o). r$ Y& G; {: P% Z5 p3 `+ O
  56.         {8 b" x9 B4 D7 k  a
  57.             Console.Write("微博账号:");
    ! I5 Q: A& Z1 a7 D9 R$ M! _
  58.             string account = Console.ReadLine();1 ]" l8 e/ `: p: _5 k
  59.             Console.Write("登录密码:");
    / m5 }* |' V6 `+ t# m7 D- |
  60.             ConsoleColor originColor = Console.ForegroundColor;
    9 Q; \9 M0 w9 j9 i7 l3 @
  61.             Console.ForegroundColor = Console.BackgroundColor; //知道这里是在干啥不?其实是为了不让你们看到我的密码^_^7 q1 q, s( p3 t2 V) @; i4 ?
  62.             string password = Console.ReadLine();
    7 S) C' b" q) A8 `
  63.             Console.ForegroundColor = originColor; //恢复前景颜色。
    4 t" M) n' _1 [2 ~
  64.             return o.ClientLogin(account, password);- u. K8 ?1 f8 h
  65.         }4 L  x. F1 X2 x7 P. p

  66. ( t* n4 X3 L8 o; b  ~
  67.         static void TimerElapsedEvent(object sender, ElapsedEventArgs e)
    1 X7 W& e3 }+ m: d% w% Y( N
  68.         {1 Z: E! W! n4 Z# D, k4 J
  69.             DateTime iNow = DateTime.Now;; n7 n" Z9 A+ s
  70.             try
    + B) p/ Y, I* W! P! x1 a4 f
  71.             {
    ' |) o, [( c" t7 N) X6 \
  72.                 string content = makeContent(iNow.Hour, iNow.Minute);
    , v! m  z0 `9 ~7 S  i" r' h
  73.                 if (!string.IsNullOrEmpty(content))6 U) d) S2 E6 {) ^' J1 ~
  74.                 {: R' |5 j5 ^7 B2 N! \: z
  75.                     var statusInfo = Sina.API.Entity.Statuses.Update(content);$ t: C  a" S  i7 U$ h. r3 T
  76.                     DateTime dtCreate = DateTime.ParseExact(statusInfo.CreatedAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);3 t& A1 A  z" B+ s; t% d3 L
  77.                     Console.WriteLine("本机时间:{0}, 微博时间:{1}, 发布内容:{2}", string.Format("{0:G}", iNow), string.Format("{0:G}", dtCreate), statusInfo.Text);
    1 J3 [0 B5 ^* D+ |% a; o- }
  78.                 }2 H/ |, V9 L# w+ [. Q: d( f' i
  79.             }/ A3 e8 m) }2 ?4 I
  80.             catch (Exception ex)
    " u8 h" o3 Q8 p9 |4 B8 s" K, w
  81.             {- t2 q5 p- T6 K" Q
  82.                 Console.WriteLine("定时任务异常:" + ex.Message);
    9 c: e& L; F2 c1 F
  83.             }
    # s( z8 l8 u' d+ ?+ K# t
  84.         }
    4 I) V. z+ Z& ]9 F$ g7 [7 l
  85. 2 t0 _" W. ~  u5 \2 `* \! h: @. M
  86.         static string makeContent(int hour24, int minute)2 ?- V6 _6 h4 A- `8 \6 i
  87.         {
    1 Z% j/ P& @! {6 M* |
  88.             string[] diZhi = "子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥".Split('|');7 X/ o. J0 Z/ k9 M  s4 M( Q
  89.             string extWeibo = "";; ]0 r* B( |4 M0 V0 w
  90.             if (hour24 % 2 == 0 && minute == 0); G& d7 E  ?; h0 g
  91.             {
    + e" L: o2 X9 I( u; s
  92.                 for (int i = 0; i < ((hour24 >= 12)? (hour24 - 12): hour24); i++)9 c0 N* Q0 u: h" c
  93.                 {
    0 H+ B0 Q. s7 F, R& [0 Y& D0 @
  94.                     extWeibo += "铛~";; J, ?# h% q$ o5 z
  95.                 }  O) O; m1 ~' D) h& i
  96.                 return "【" + diZhi[hour24 / 2] + "时】" + extWeibo;
    * y. L$ r! G2 ?3 x/ {! Z
  97.             }
    : D5 |4 s& S. T8 I$ M
  98.             else# u3 E% ?0 O- _6 c) l8 h5 @
  99.             {8 K) |( Y3 i# R8 S
  100.                 return "";6 U& C7 h% E# b  K
  101.             }7 w' l* i6 S8 U" O' B* ~
  102.         }" g0 C8 ?% I7 X; b) l2 F
  103.     }
    " M. k4 Q6 H/ [4 m5 P) ?7 [
  104. }
复制代码
完整程序下载:
运行前需要安装.NET Framework。 点击这里下载.NET Framework 4 并安装,这个有40多M
+ z$ g; f9 J  e* X3 v然后下载我的程序:guchengzhonglou.rar (已经内置Weico.iPhone了哦,亲)7 e, A' O6 [4 f) L8 V
, q* E( |0 a2 K. v% A




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

使用道具 举报

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

使用道具 举报

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

支付宝扫一扫打赏

微信扫一扫打赏

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