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

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

  j$ D7 j% L5 y+ K, f. T0 S
完整代码如下:
需要自行修改App相关值:
3 }7 k' J- x0 C! p

  1. " g9 Y& e8 x% U: d  w: r4 \: y  P
  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;; ?: u2 ]8 _9 x" a% v; K
  2. using System.Collections.Generic;$ N/ Y! s3 f7 O1 y7 U$ }9 j
  3. using System.Text;! ]$ y) R# [* |. N  I
  4. using NetDimension.Weibo;5 G; P0 ?( h* z& p$ _' \0 X
  5. using System.Timers;5 i; \* f  U2 X+ s$ V% d
  6. using System.Globalization;
    2 ]$ Z& d0 u/ [

  7. $ N' o' i; I8 b6 l0 H# @2 p/ o& E
  8. namespace WeiboTimer- B3 v* ~: W' j  f. }0 ^
  9. {6 f% v- O8 _. Y* F* |
  10.     class Program6 o, F. C! I/ ]5 y3 ?7 @; a+ N
  11.     {
    8 X, v; p' o( `
  12.         static string AppKey = "********";
    6 y, E2 }3 Q* w
  13.         static string AppSecret = "*******";
    4 ~, J  i/ s" S, Z* K- h' q; `/ Z
  14.         static string CallbackUrl = "****************";: `% d' _3 T' U* a7 k" V) X
  15.         static Timer t = new Timer();  m1 L2 \) V+ E2 P% {
  16.         static OAuth oauth = null;* z% X( E3 _9 X: r
  17.         static Client Sina = null;6 Y, H. F7 I4 ~& @# S( V: J, u
  18.   s1 {3 C* N- v
  19.         static void Main(string[] args)
    4 Z) Q9 X8 I8 Z  i' z
  20.         {            ( S& I/ ~( D" E8 `( y- E$ V! T
  21.             oauth = Authorize();- o7 O. ~0 ~" D
  22.             if (!string.IsNullOrEmpty(oauth.AccessToken)){Console.Write("登录成功!");}
    % U3 R2 J! P" C. a2 C

  23. 6 e2 y/ k& V# I! u% {. t
  24.             Sina = new Client(oauth);
    * ]+ r6 p8 C$ n& Q: X
  25.             string uid = Sina.API.Entity.Account.GetUID();4 ?5 l8 ]- W9 Q0 `  v
  26.             var entity_userInfo = Sina.API.Entity.Users.Show(uid);) p* \. o2 F7 K, }. h  E) D0 ]3 u
  27.             Console.WriteLine("昵称:{0},微博地址:http://weibo.com/{1}", entity_userInfo.ScreenName, entity_userInfo.ProfileUrl);
    # X3 e; V6 n) w5 D% R
  28. 9 y5 e9 w3 K8 O  h* p3 p) y
  29.             try0 j0 n; t  k" Q) c; |; X: h$ x
  30.             {- z  S, O" H+ e
  31.                 t.Interval = 1000 * 60;     //设置间隔时间为1分钟
    ! i5 Z: J* W  U' t! ~! N
  32.                 t.Elapsed += new ElapsedEventHandler(TimerElapsedEvent); //到达时间的时候执行事件;
    # U* M2 F& s  f. b  f- Z
  33.                 t.AutoReset = true;         //设置是执行一次(false)还是一直执行(true);
    5 v$ C5 `8 w# f- f! P
  34.                 t.Start();                  //启动Timer对象; # [2 g: P/ A6 P& z1 @
  35.             }
    # E! W; }2 h  s- _. b
  36.             catch (Exception ex)+ z4 x0 V7 G+ S" N, E6 b* n2 T& z
  37.             {3 O+ ?! z/ T) v6 U' Y% g9 |
  38.                 Console.WriteLine("定时任务异常:" + ex.Message);
    : j! j2 p% u* b7 H- [* B
  39.                 t.Stop();# h; v6 X$ P, E2 `1 ]& l  Q; O
  40.             }' s* \3 J/ g5 d- ^) i% H
  41.             Console.WriteLine("定时任务开始工作。。。");
    & ^/ i; W! N  _
  42.             Console.ReadKey();
    % ?! n0 g& p& h, t1 T6 f
  43.         }+ {9 ~+ A) R* Z2 ]

  44. 8 |& Z1 }* l- _
  45.         static OAuth Authorize()/ A; z5 P9 K. {
  46.         {9 W9 l+ f0 M- B' x3 I/ V  J2 Y& C' g8 N
  47.             OAuth o = new OAuth(AppKey, AppSecret, CallbackUrl);
    ' H; k& K+ B! R6 y+ x9 G3 O' U
  48.             while (!ClientLogin(o))
    ) x+ M' w- r1 F% F, }- W
  49.             {
    . O/ F, L+ L: q$ a8 Q
  50.                 Console.WriteLine("登录失败,请重试。");
    0 ?- }+ a8 h- s! c; L* P! _
  51.             }4 T5 R3 P" Q+ _' E  Y& f
  52.             return o;* I" T# K3 S% d2 O, A6 A
  53.         }
    2 d- @9 b( T- m' L) \  z4 q6 e5 p
  54. : l8 R; Z) E4 Z4 F) E" x
  55.         private static bool ClientLogin(OAuth o)
    ) H3 o( ~! {( |
  56.         {; q8 F: ]0 d1 ]) v2 P
  57.             Console.Write("微博账号:");- D" }+ C6 e: U) E' b0 U
  58.             string account = Console.ReadLine();
    # v2 t. v/ h9 \9 K5 o
  59.             Console.Write("登录密码:");* f. F% {3 K4 \3 m" \" S9 `
  60.             ConsoleColor originColor = Console.ForegroundColor;
    $ e$ C) f: ?: Z1 H2 c) ?; a
  61.             Console.ForegroundColor = Console.BackgroundColor; //知道这里是在干啥不?其实是为了不让你们看到我的密码^_^4 F: ~5 _: ^; {- B- o" Y0 V
  62.             string password = Console.ReadLine();5 R* A  J- s7 P
  63.             Console.ForegroundColor = originColor; //恢复前景颜色。# X4 L' C3 w  T% N) ~6 `/ ]2 N
  64.             return o.ClientLogin(account, password);
    3 Q# ^8 ]* W( k. q
  65.         }: M- G& k4 `- p. M# y. d, b: {0 E
  66. 0 R* d3 y6 ~3 m; J; o5 d' @  }8 y
  67.         static void TimerElapsedEvent(object sender, ElapsedEventArgs e)
    9 y1 m. z5 @% e  B- F9 i
  68.         {1 t$ p2 Z, G' _+ h! r. E
  69.             DateTime iNow = DateTime.Now;  O$ l2 d( P9 `( c$ f
  70.             try( y2 _3 {0 |3 @' m; f
  71.             {- l/ {/ f8 c) N! ]) D- m
  72.                 string content = makeContent(iNow.Hour, iNow.Minute);
    - X( r: e' {6 l& M4 q3 l( O
  73.                 if (!string.IsNullOrEmpty(content))
    9 L3 i) ^5 @, W; T1 c( [
  74.                 {
    2 k8 z8 u% d6 F/ k+ Z( s
  75.                     var statusInfo = Sina.API.Entity.Statuses.Update(content);
    5 _/ Q4 N. d, E3 w
  76.                     DateTime dtCreate = DateTime.ParseExact(statusInfo.CreatedAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);, W1 ?9 l6 u) L& \% M4 a
  77.                     Console.WriteLine("本机时间:{0}, 微博时间:{1}, 发布内容:{2}", string.Format("{0:G}", iNow), string.Format("{0:G}", dtCreate), statusInfo.Text);
    ( y4 ?+ `: k2 ^- }3 |9 k- A& y3 X3 S) h
  78.                 }
    ! Y7 s; ^2 e* m) _8 E
  79.             }
    8 k5 J: t( b) m& e0 m
  80.             catch (Exception ex)
    ' c: }8 |% r1 R2 Y1 Z
  81.             {
    5 V' S2 b, a, u0 Q" S) _
  82.                 Console.WriteLine("定时任务异常:" + ex.Message);! W$ k% X: {' ^  l$ Z
  83.             }7 u+ L( X  a  G0 U
  84.         }/ r: ~3 h7 O. W% h: U. T
  85. 7 W! \( K9 a, E  g
  86.         static string makeContent(int hour24, int minute)
    7 E: e5 x$ X$ H/ Q- y* c% v
  87.         {; ?1 y) t% \5 {  Q& a
  88.             string[] diZhi = "子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥".Split('|');; K# h) q; W4 D- A# s; j! Y
  89.             string extWeibo = "";
    5 B5 g7 T' @1 M
  90.             if (hour24 % 2 == 0 && minute == 0)1 x, _/ \. `" G3 p' E9 V; |
  91.             {
    8 A2 Z! B+ v' Y/ w$ i3 r: ?
  92.                 for (int i = 0; i < ((hour24 >= 12)? (hour24 - 12): hour24); i++)% I: ?* c! V; ]" U. x
  93.                 {
    ; C2 o# B; v+ y% I7 c% F4 N& H
  94.                     extWeibo += "铛~";
    . W# T, f- ]! p" z8 C& U/ l; [% N; }
  95.                 }& x# e) k) `, P% u( p5 `& \( X% j
  96.                 return "【" + diZhi[hour24 / 2] + "时】" + extWeibo;
    * ]& U7 g% d3 ^; z
  97.             }
    ( D7 W8 X' h  h5 |: n5 t7 @) W
  98.             else/ L' F. E8 `3 H! ~
  99.             {0 _  I" o, J$ j2 x) e  ?6 c3 e
  100.                 return "";2 [( {( E+ T7 N' t  c; F1 c. G" k0 k- y
  101.             }  H# T9 K4 V- m, m
  102.         }% k# _9 W2 |7 @; h' w7 d
  103.     }# I0 r( B  B/ R0 R/ @$ i
  104. }
复制代码
完整程序下载:
运行前需要安装.NET Framework。 点击这里下载.NET Framework 4 并安装,这个有40多M - G9 c: ?- S3 O) H' `
然后下载我的程序:guchengzhonglou.rar (已经内置Weico.iPhone了哦,亲)8 m6 }; l7 ^5 i8 r, g) N

4 I( k2 K1 |; G5 V) |$ L8 c+ @




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

使用道具 举报

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

使用道具 举报

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

支付宝扫一扫打赏

微信扫一扫打赏

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