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

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

) x4 B+ K0 \/ l8 {# I
然后我们来分析一下它昨天发布的内容
  • 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 【亥时】铛~铛~铛~铛~铛~铛~铛~铛~铛~铛~
    4 _# p" g  A! P  i5 @0 n0 b
6 j% p3 v$ I3 G; v; Q& p7 I5 v& p
根据上面,得出它有以下规律:
  • 每两个小时发布一条微博,
  • 发布时间是偶数小时的0分,这个时间并不是地支时间的开始时间,而是正中间的那个时间点
  • 内容是相应的地支时间,加上根据时间而变化的“铛~”的次数,
  • “铛~”的次数与时有关,12小时以前与小时相同,12小时以后与[小时减12]相同。
    6 l! A) [- ?  E' E# j' u
西安钟楼里的钟就是这样敲的吗?去过钟楼一次,没登上去,也没注意。

* b7 h9 k+ M  M9 c
完整代码如下:
需要自行修改App相关值:

- c9 |1 F6 s. \# Y
  1. : D( r5 L/ R% Z; |4 X+ h5 U% K9 n
  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;' w3 q6 O4 ]) T* B% k% H
  2. using System.Collections.Generic;6 L. f1 b2 [, f' Q( ]+ g. G2 S
  3. using System.Text;5 _3 [$ c) _! j4 i* R0 `
  4. using NetDimension.Weibo;
    ; o1 q  u  [/ @: ?& S' `0 a; A
  5. using System.Timers;* J5 y7 r# h) C7 a& u1 F% U
  6. using System.Globalization;
    , C6 C( B3 _' I

  7. 2 D# |+ l# g( w; c
  8. namespace WeiboTimer7 x! s9 S$ S% ?; H3 a
  9. {6 H* \3 C$ o7 p6 U$ N, P7 i
  10.     class Program. j; U4 B  Y- _$ u
  11.     {2 `1 n% \, w( [
  12.         static string AppKey = "********";
    6 j7 _. N2 [4 {
  13.         static string AppSecret = "*******";/ _) @# W! y2 V6 v9 ^, `& I
  14.         static string CallbackUrl = "****************";
    & g7 t3 J" i& d7 f, h  i5 t3 d
  15.         static Timer t = new Timer();
    * Z7 ]. X9 o% W. Z* C
  16.         static OAuth oauth = null;
    * k, G# Y* N5 E. @2 e- s2 b( l
  17.         static Client Sina = null;
    3 ?( P! Y6 _) N
  18. 0 t/ N" y) f, J" U; h3 X! F( x4 e  }
  19.         static void Main(string[] args). n. p) i3 x: c# ?& Y
  20.         {            $ l7 T/ O2 U- q. c0 M
  21.             oauth = Authorize();
    ! W0 i# k4 d! M
  22.             if (!string.IsNullOrEmpty(oauth.AccessToken)){Console.Write("登录成功!");}
    " u0 \& k6 m. Q3 _4 `0 }

  23. 1 M4 g$ u8 o0 c" [& n( ?2 I1 T
  24.             Sina = new Client(oauth);" [+ ^8 ]5 E. R: _) o1 |
  25.             string uid = Sina.API.Entity.Account.GetUID();
    9 Z! z6 [3 F4 L; Y; M
  26.             var entity_userInfo = Sina.API.Entity.Users.Show(uid);! q' n" d4 L, p% g& t
  27.             Console.WriteLine("昵称:{0},微博地址:http://weibo.com/{1}", entity_userInfo.ScreenName, entity_userInfo.ProfileUrl);& E* t, h# ]2 B+ v

  28. * B/ e% E4 X. ^3 v3 c: \% n; d7 I
  29.             try
    + S, w/ t$ o$ b3 f4 V  I
  30.             {, T- o+ y6 ]  ~1 `0 m0 A
  31.                 t.Interval = 1000 * 60;     //设置间隔时间为1分钟7 L+ Z. O" n5 `9 I1 A
  32.                 t.Elapsed += new ElapsedEventHandler(TimerElapsedEvent); //到达时间的时候执行事件;
    * Y4 v) v4 a% ~# z* j0 ?
  33.                 t.AutoReset = true;         //设置是执行一次(false)还是一直执行(true);
      i/ V" X" s3 j4 W  x* s& y
  34.                 t.Start();                  //启动Timer对象; ' b3 f, O( e) b! H& |
  35.             }9 z; \, ]3 u; U4 Q0 n. L
  36.             catch (Exception ex)
    : Z) {- [% }% y. B6 P& U  y% N
  37.             {/ B2 h  k' o2 O& |% ?7 V
  38.                 Console.WriteLine("定时任务异常:" + ex.Message);! z6 }; R: f' c* J% Y
  39.                 t.Stop();
    . @* p' I( T% `5 V
  40.             }
    2 T* t$ t& Y* d% ?+ j3 f0 k$ Z
  41.             Console.WriteLine("定时任务开始工作。。。");
    1 W; u& z7 j9 H7 S& f3 z6 Q
  42.             Console.ReadKey();9 D3 N! Q2 F4 X  Q/ ]9 G
  43.         }
    . f$ I& \# M) c
  44. - }* H! r5 q% `6 ~6 k/ i% L
  45.         static OAuth Authorize(). Z; \( G2 y4 A+ N$ j8 M1 B9 Q  }
  46.         {( |4 d; F% j  ?1 X# C
  47.             OAuth o = new OAuth(AppKey, AppSecret, CallbackUrl);
    3 M- z1 o9 I* \' C4 F
  48.             while (!ClientLogin(o))
    : ]. I+ M; @" v9 Z
  49.             {
    + @1 F1 W9 n$ }* @% X/ b# G
  50.                 Console.WriteLine("登录失败,请重试。");! S0 k  L4 |* D" `$ f
  51.             }
    ' J/ I1 [, {$ c  ^+ I' N
  52.             return o;! |- ^, M. k# Y% d) n2 ^
  53.         }
    & R- z' H$ b2 ~+ j
  54. 1 z  r, k2 h! r) Y
  55.         private static bool ClientLogin(OAuth o)5 ~! V  f2 B- \: s: ?, c1 i- n. `
  56.         {
      \  c1 S1 m7 B4 W6 J/ w# _& H
  57.             Console.Write("微博账号:");, Z6 A, l' d6 c! \
  58.             string account = Console.ReadLine();' n* L( T% `8 j; _! l
  59.             Console.Write("登录密码:");- N! B2 K! I& Y6 X; y
  60.             ConsoleColor originColor = Console.ForegroundColor;
    1 N7 e: }2 j3 e3 e7 I
  61.             Console.ForegroundColor = Console.BackgroundColor; //知道这里是在干啥不?其实是为了不让你们看到我的密码^_^
    6 X' x5 e+ h9 \9 q( A1 S
  62.             string password = Console.ReadLine();
    2 T' j( Z! E8 d
  63.             Console.ForegroundColor = originColor; //恢复前景颜色。% j( S$ n0 u( R8 L: K
  64.             return o.ClientLogin(account, password);. D6 G$ b5 ?- q6 k
  65.         }
    , B- l7 U; v: {9 S+ d  K

  66. ) M. g1 |: @7 e! _0 t
  67.         static void TimerElapsedEvent(object sender, ElapsedEventArgs e)
    * ]( K; y- i  K6 x% w8 N5 q
  68.         {% L, A7 h7 a3 l+ r2 r- g) H
  69.             DateTime iNow = DateTime.Now;% c4 a" o! M; g0 u. R+ J
  70.             try9 _* r+ a1 d' Z( l. q4 s
  71.             {# b. ~& D$ ]( m8 l8 s& \
  72.                 string content = makeContent(iNow.Hour, iNow.Minute);  Y+ O# I: O) j( S- o1 z
  73.                 if (!string.IsNullOrEmpty(content))
    ' L. a6 U; k: ?* ^+ B7 v1 @% y6 Z
  74.                 {
    , S' K# H' z) j5 I* _# A# l; O( o8 A
  75.                     var statusInfo = Sina.API.Entity.Statuses.Update(content);  x% x8 w. V7 J9 l* s- e$ T" G1 h
  76.                     DateTime dtCreate = DateTime.ParseExact(statusInfo.CreatedAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);( H9 S, k. P; q
  77.                     Console.WriteLine("本机时间:{0}, 微博时间:{1}, 发布内容:{2}", string.Format("{0:G}", iNow), string.Format("{0:G}", dtCreate), statusInfo.Text);
    ! \4 y& ]* B+ x7 [. v2 s
  78.                 }2 n9 g0 ~# B7 |% Q
  79.             }. k. a3 V; W& H) Z
  80.             catch (Exception ex)$ q  `% K) B: e. q. a
  81.             {
    : B) O8 f7 P3 s7 ~& M: y4 _- `
  82.                 Console.WriteLine("定时任务异常:" + ex.Message);
    0 ~* W/ E' g' A$ k
  83.             }) n+ g! `1 z5 s' o+ J6 d
  84.         }
    8 }* e3 M) W& J0 i' H& d2 B
  85. - D' w& d5 D% ]3 A7 |3 s
  86.         static string makeContent(int hour24, int minute)- N, B& l8 T% C
  87.         {
    : k) e2 c1 \$ S6 ~3 `* f. ~  a: M
  88.             string[] diZhi = "子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥".Split('|');
    9 ]3 d0 u; R; t4 W
  89.             string extWeibo = "";, a+ d7 L4 u* `6 V4 F# e  [& ~
  90.             if (hour24 % 2 == 0 && minute == 0)
    3 V2 J/ d& P3 Q7 q2 p  x
  91.             {4 D* D  I" S4 X  ?  x$ Z+ B, y
  92.                 for (int i = 0; i < ((hour24 >= 12)? (hour24 - 12): hour24); i++)! j" d$ V& ^% b7 J
  93.                 {
    0 D5 J9 I7 E5 k* p' ~' I* [
  94.                     extWeibo += "铛~";3 Z: v8 l' h  o* Q  ?* M
  95.                 }, p% E4 e, Y5 o( y/ ?- Q: p3 t: e. {
  96.                 return "【" + diZhi[hour24 / 2] + "时】" + extWeibo;" c  d  m3 x( E1 L; l$ j
  97.             }- @. H5 p% s7 y& r
  98.             else% T: I$ X3 v+ w
  99.             {' e: x1 n9 d2 [% K
  100.                 return "";* h; q9 G0 e/ C! X0 J8 g" b
  101.             }8 B8 }3 F7 w% w
  102.         }0 L! p# C$ H5 ]  d6 u
  103.     }2 o# k0 l6 O0 x6 d$ b
  104. }
复制代码
完整程序下载:
运行前需要安装.NET Framework。 点击这里下载.NET Framework 4 并安装,这个有40多M . ?& I# ^4 y" b. z& Y
然后下载我的程序:guchengzhonglou.rar (已经内置Weico.iPhone了哦,亲)
. C$ J6 ~& p/ J4 T

( x; Y& N2 c- a+ x" q5 Z0 d




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

使用道具 举报

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

使用道具 举报

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

支付宝扫一扫打赏

微信扫一扫打赏

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