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

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

( H0 a% S2 C/ L# x
先来普及一下地支时间与北京时间的对应关系:
  • 【子时】夜半,又名子夜、中夜:十二时辰的第一个时辰。(北京时间23时至次日01时)。
  • 【丑时】鸡鸣,又名荒鸡:十二时辰的第二个时辰。(北京时间01时至03时)。
  • 【寅时】平旦,又称黎明、早晨、日旦等:时是夜与日的交替之际。(北京时间03时至05时)。
  • 【卯时】日出,又名日始、破晓、旭日等:指太阳刚刚露脸,冉冉初升的那段时间。(北京时间05时至07时)。
  • 【辰时】食时,又名早食等:古人“朝食”之时也就是吃早饭时间,(北京时间07时至09时)。
  • 【巳时】隅中,又名日禺等:临近中午的时候称为隅中。(北京时间09时至11时)。
  • 【午时】日中,又名日正、中午等:(北京时间11时至13时)。
  • 【未时】日昳,又名日跌、日央等:太阳偏西为日跌。(北京时间13时至15时)。
  • 【申时】哺时,又名日铺、夕食等:(北京时间15时至17时)。
  • 【酉时】日入,又名日落、日沉、傍晚:意为太阳落山的时候。(北京时间17时至19时)。
  • 【戌时】黄昏,又名日夕、日暮、日晚等:此时太阳已经落山,天将黑未黑。天地昏黄,万物朦胧,故称黄昏。(北京时间19时至21时)。
  • 【亥时】人定,又名定昏等:此时夜色已深,人们也已经停止活动,安歇睡眠了。人定也就是人静。(北京时间21时至23时)。: H* y+ ]7 w" l5 L* |$ D
& t, P8 u; V9 E+ B1 m
然后我们来分析一下它昨天发布的内容
  • 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 g$ I; {. w: r$ i3 z
0 Y* A: A3 O8 ?; U0 t
根据上面,得出它有以下规律:
  • 每两个小时发布一条微博,
  • 发布时间是偶数小时的0分,这个时间并不是地支时间的开始时间,而是正中间的那个时间点
  • 内容是相应的地支时间,加上根据时间而变化的“铛~”的次数,
  • “铛~”的次数与时有关,12小时以前与小时相同,12小时以后与[小时减12]相同。
    9 ?1 D* K2 l$ g/ c0 j
西安钟楼里的钟就是这样敲的吗?去过钟楼一次,没登上去,也没注意。
; A5 A/ V/ t" o8 O9 q( o0 Y
完整代码如下:
需要自行修改App相关值:

: ?& P4 T# u0 b( q: g  s2 v3 B

  1. / E  X2 d  ^- s% c
  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;
    . [" ~* g. C& L7 w; A& b
  2. using System.Collections.Generic;; f) c& V* `* M5 w
  3. using System.Text;
    - G% x# b( |$ y! o: [( T( i
  4. using NetDimension.Weibo;
      i# P  Y6 k/ f( n4 x  h+ i9 M& \
  5. using System.Timers;
    + P2 N/ }. P6 o8 s3 }3 A3 y
  6. using System.Globalization;' _7 b+ G, O, O  I& C  p. [
  7. 7 d/ |" V5 s' l) D, t( ~# ?; _
  8. namespace WeiboTimer9 w0 x- X% j! n5 g( K
  9. {
    # B% c. ]) D( q0 r7 O! @1 y0 |; g, @
  10.     class Program
    ) j5 z6 P2 }" W3 O
  11.     {
    5 u0 C+ G; k. q: J# N! m4 b* _
  12.         static string AppKey = "********";
    4 t; A0 G0 l! V% f( y2 T; V
  13.         static string AppSecret = "*******";
    6 ]8 P7 A5 g& b0 {+ J5 S
  14.         static string CallbackUrl = "****************";
    : Z, T8 G5 }6 L. w& S. }
  15.         static Timer t = new Timer();, |; ^2 t/ d' C
  16.         static OAuth oauth = null;
    0 t' K# b; `* q
  17.         static Client Sina = null;
    0 a! T4 G: J% v( w, d

  18. 4 A# c1 H+ e+ m  j$ M5 g3 p
  19.         static void Main(string[] args)
    * @% i& K, ~1 s) S: w% ?
  20.         {            
    - e; _8 a3 R" j/ c3 N* p
  21.             oauth = Authorize();9 |) k8 M! b3 s/ Q5 {/ `& J
  22.             if (!string.IsNullOrEmpty(oauth.AccessToken)){Console.Write("登录成功!");}
    / s1 J: a" d" ^7 w; A/ P

  23. * H. v2 k& S4 n6 p  O7 h$ E
  24.             Sina = new Client(oauth);0 C4 ~, Q# r/ p1 t5 l
  25.             string uid = Sina.API.Entity.Account.GetUID();
    " Q/ J) L0 }9 c& |6 [
  26.             var entity_userInfo = Sina.API.Entity.Users.Show(uid);
    ; Z* N3 \& j9 W' G- W
  27.             Console.WriteLine("昵称:{0},微博地址:http://weibo.com/{1}", entity_userInfo.ScreenName, entity_userInfo.ProfileUrl);- L# T/ V9 e2 L3 I$ w
  28. ( c4 b3 C3 Y) ?1 }1 b% d3 O
  29.             try
    ' \. [  @- `9 q4 V& z% x# ]
  30.             {+ z& S  }4 n, y
  31.                 t.Interval = 1000 * 60;     //设置间隔时间为1分钟1 z" I+ U) f+ l  y- T4 T/ y! c
  32.                 t.Elapsed += new ElapsedEventHandler(TimerElapsedEvent); //到达时间的时候执行事件; - _" S5 D5 u% ~9 {* i  }
  33.                 t.AutoReset = true;         //设置是执行一次(false)还是一直执行(true);
    3 [: w2 a& e! [( Q
  34.                 t.Start();                  //启动Timer对象; / \! E- e( _4 S* d
  35.             }
    5 Y, j$ _( {! l& `7 Z
  36.             catch (Exception ex)$ P. }/ e" Z( ]  c
  37.             {% z$ }, J! F1 q# ~  p9 y
  38.                 Console.WriteLine("定时任务异常:" + ex.Message);
    ! I& o4 w# Z2 V: @) ?! q
  39.                 t.Stop();
      d! H, R, q+ h2 |0 a
  40.             }
    , d- ^1 Q. J' u! l0 m& v
  41.             Console.WriteLine("定时任务开始工作。。。");- S! M/ M* ^# M% ^
  42.             Console.ReadKey();
    & D- S+ S$ z/ K' q
  43.         }- F7 }% C5 A+ e* T! s2 N+ v4 w

  44. / [1 @2 l. l/ l6 A
  45.         static OAuth Authorize()9 T' _* [: I: h6 l7 E  d. n
  46.         {/ B9 d; |) G* l/ Y' L4 H+ M$ v
  47.             OAuth o = new OAuth(AppKey, AppSecret, CallbackUrl);: X* H% [  M; ]1 V, b7 A7 I
  48.             while (!ClientLogin(o))# Y! c: ]+ o2 e6 A, d  G
  49.             {, ~, U) b, A3 H/ K# h5 s. h
  50.                 Console.WriteLine("登录失败,请重试。");5 B, M: [' m$ S0 ^$ I! t) C
  51.             }& [; k% l6 i6 I/ b- |7 \$ K# H
  52.             return o;2 X, G2 e0 T! c9 @7 z
  53.         }
    8 d5 F1 D2 _, ~4 J

  54. $ E6 g- w, ^5 d6 B# r/ ^% J
  55.         private static bool ClientLogin(OAuth o)! `0 V3 x& `) I
  56.         {- D! ~* R& t) L8 o& c9 h/ [" R
  57.             Console.Write("微博账号:");6 Q, v+ D, M5 q% D
  58.             string account = Console.ReadLine();( B1 y0 ]  }( [7 F6 d
  59.             Console.Write("登录密码:");
    " v+ r( G" e3 [
  60.             ConsoleColor originColor = Console.ForegroundColor;5 e9 X% i$ t: _: I  q. E; Q: }0 w' F
  61.             Console.ForegroundColor = Console.BackgroundColor; //知道这里是在干啥不?其实是为了不让你们看到我的密码^_^$ Q) P* `$ u) N* D$ `
  62.             string password = Console.ReadLine();
    ' ?8 l1 N8 _3 h. D& R# r, }
  63.             Console.ForegroundColor = originColor; //恢复前景颜色。* z8 _! E+ x6 G* j& V
  64.             return o.ClientLogin(account, password);
    ! p* D( ]8 z8 n: X
  65.         }
    , q! \# o; S7 G+ g! p* x% Y
  66. ' M4 v4 ?- w: W; B! u6 N+ [
  67.         static void TimerElapsedEvent(object sender, ElapsedEventArgs e)
    $ p2 O6 j/ Q1 c+ ?" ?5 S
  68.         {
    + _4 {* d5 A) q1 t( E# N3 R
  69.             DateTime iNow = DateTime.Now;8 C" z7 U0 n1 `* z9 l! c
  70.             try
    " W( B: N% T: j1 [
  71.             {, F" K1 j4 }' T6 v' @
  72.                 string content = makeContent(iNow.Hour, iNow.Minute);+ i& g! `" h- N% S: l& P
  73.                 if (!string.IsNullOrEmpty(content))& q6 Y; t( j2 T) q) B; |& @" d0 I
  74.                 {( _/ D* i1 f4 G% y; Y% U7 V  v: d
  75.                     var statusInfo = Sina.API.Entity.Statuses.Update(content);* m& |' F/ W9 \* k  o
  76.                     DateTime dtCreate = DateTime.ParseExact(statusInfo.CreatedAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
    ) z; W" K. e' a: Q1 T. Z
  77.                     Console.WriteLine("本机时间:{0}, 微博时间:{1}, 发布内容:{2}", string.Format("{0:G}", iNow), string.Format("{0:G}", dtCreate), statusInfo.Text);% B7 `& i# S/ X2 O) Y* A
  78.                 }
    / [. G9 o4 P2 [
  79.             }
    ' C6 e% d# P& U7 N3 V. }  `
  80.             catch (Exception ex)" M9 D' r9 h- r' W  R
  81.             {
    9 [* ^/ m2 z# h
  82.                 Console.WriteLine("定时任务异常:" + ex.Message);9 y: v7 y, V: [; _  H, S
  83.             }1 x/ V( M. @8 L6 `, d
  84.         }+ x8 D$ E9 H2 ]; Q. R% D

  85. : Z. t- |2 J. S
  86.         static string makeContent(int hour24, int minute)7 p8 R7 C- v# K
  87.         {
    : C4 H9 ?- N( F2 g5 J. W
  88.             string[] diZhi = "子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥".Split('|');0 J$ f8 f: i1 }1 e7 i/ m2 T5 l
  89.             string extWeibo = "";
    ) K8 O  e4 {: i- i) }
  90.             if (hour24 % 2 == 0 && minute == 0)
    $ B4 P* ]9 |% G
  91.             {
    : `* \, s7 n" [/ l, T2 L
  92.                 for (int i = 0; i < ((hour24 >= 12)? (hour24 - 12): hour24); i++): P& x, z( p- I8 p
  93.                 {
    6 `7 S. x, E  t7 }) ^) ?! n
  94.                     extWeibo += "铛~";
    + V, v9 P4 H; |1 s1 k2 L8 m
  95.                 }
    : D- N; L& j; O4 e
  96.                 return "【" + diZhi[hour24 / 2] + "时】" + extWeibo;
    % N) b; y5 ~' b/ j: a" \
  97.             }
    * |4 b" E$ U- b+ `* d8 f9 i
  98.             else  S+ j) H1 G2 ]; w4 n
  99.             {2 v, N; F) I* \3 t& T
  100.                 return "";
    ; p& }% D: p% }4 R
  101.             }
    . O: f2 M  B- I
  102.         }& W( Z# B( F4 d8 Q: W+ @/ ~! A
  103.     }
    ( s. r" F9 U; f0 O5 f
  104. }
复制代码
完整程序下载:
运行前需要安装.NET Framework。 点击这里下载.NET Framework 4 并安装,这个有40多M
5 z% ~, C; l/ C% n# r/ u然后下载我的程序:guchengzhonglou.rar (已经内置Weico.iPhone了哦,亲)  X+ \" C4 v6 f6 X5 u( X
/ w& F# i, d; X; u




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

使用道具 举报

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

使用道具 举报

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

支付宝扫一扫打赏

微信扫一扫打赏

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