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

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

+ _. z- e- }" d2 r  s! 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时)。5 t& E6 p5 k/ Z

) M9 W+ e% Y( C9 a6 }" 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 【亥时】铛~铛~铛~铛~铛~铛~铛~铛~铛~铛~
    ' o9 p! S* c' R5 I% V2 v# [
9 o! r( d; |) z3 F/ H
根据上面,得出它有以下规律:
  • 每两个小时发布一条微博,
  • 发布时间是偶数小时的0分,这个时间并不是地支时间的开始时间,而是正中间的那个时间点
  • 内容是相应的地支时间,加上根据时间而变化的“铛~”的次数,
  • “铛~”的次数与时有关,12小时以前与小时相同,12小时以后与[小时减12]相同。
    & {& y8 j% r8 z0 e; J
西安钟楼里的钟就是这样敲的吗?去过钟楼一次,没登上去,也没注意。
. a9 V; X8 E) `
完整代码如下:
需要自行修改App相关值:
! T- F( R+ `1 Q1 _

  1. 7 m* F% o) I) r8 V. a7 s& k
  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;! o& i. k8 E9 [9 l$ S; m
  2. using System.Collections.Generic;
    9 a# }. ~! w- t6 N) o
  3. using System.Text;  l* [5 H2 A  Z
  4. using NetDimension.Weibo;) o+ T7 l* f$ Z6 W
  5. using System.Timers;; }) v: G8 e3 ]' `  D- F. s$ H$ i
  6. using System.Globalization;1 ]5 v% _- N- b( m! Z

  7. / @1 [8 C5 S0 [6 C, }' g
  8. namespace WeiboTimer. {# }: t5 b/ h) k+ z. r7 j
  9. {8 j( U4 Q& @( {8 G
  10.     class Program
    2 U0 m: K3 g( I7 v1 |2 |
  11.     {
    2 R% [2 Y3 }* {  m: R" W
  12.         static string AppKey = "********";
    0 y- h: z2 f- z0 c" R
  13.         static string AppSecret = "*******";- T# E6 ?' n% b/ h$ |' S; J
  14.         static string CallbackUrl = "****************";
    + t" E+ O7 Z# F. l2 Z8 W6 \8 t0 b  _
  15.         static Timer t = new Timer();' H' u% l' l% ~- w3 r
  16.         static OAuth oauth = null;4 G, U7 |& O$ o" [4 G
  17.         static Client Sina = null;% \6 o% y0 }9 ^7 f  z
  18. . X3 Y& Y/ }+ @" Z0 \
  19.         static void Main(string[] args)5 {. _" }+ c4 R2 ]4 o- z1 r
  20.         {            
    + E# w3 M/ d6 X4 ?4 C
  21.             oauth = Authorize();3 l. j. E$ u2 R* B( a' h7 Z. j  _
  22.             if (!string.IsNullOrEmpty(oauth.AccessToken)){Console.Write("登录成功!");}
    $ O9 X7 c' d" p
  23. 5 \5 m- u  n+ J, L
  24.             Sina = new Client(oauth);
    / b+ W. m8 Y, U
  25.             string uid = Sina.API.Entity.Account.GetUID();/ w% ], k! ~6 Q) ]1 S$ w$ f+ a
  26.             var entity_userInfo = Sina.API.Entity.Users.Show(uid);" D0 n. A/ |8 [, m) t
  27.             Console.WriteLine("昵称:{0},微博地址:http://weibo.com/{1}", entity_userInfo.ScreenName, entity_userInfo.ProfileUrl);' `5 h/ r# h& }' O  C

  28. " E! [. U( j4 ^
  29.             try* b3 g7 G; o% v8 ]" {9 u
  30.             {8 m( j( o" T6 |) i/ ^: S& m' [
  31.                 t.Interval = 1000 * 60;     //设置间隔时间为1分钟1 i4 }# n; z. ~) X+ X4 m
  32.                 t.Elapsed += new ElapsedEventHandler(TimerElapsedEvent); //到达时间的时候执行事件;
    5 q8 U. M$ w, W8 H- p4 G, F
  33.                 t.AutoReset = true;         //设置是执行一次(false)还是一直执行(true);
    1 r0 t; ~8 n5 l2 Z. L* c! X5 O; `+ p
  34.                 t.Start();                  //启动Timer对象;
    7 X" a5 ?) U. J1 P; U
  35.             }
    + }* ]9 Z+ s- ^2 z+ j
  36.             catch (Exception ex)
    , Z5 D0 Q. l9 d" f  M
  37.             {5 a! N9 u" p. F) y. f
  38.                 Console.WriteLine("定时任务异常:" + ex.Message);
    / o4 L1 h6 O/ c8 L; P1 |
  39.                 t.Stop();
    2 W9 r5 Y6 h6 W: G  [
  40.             }
    0 h3 V/ m; f$ [6 a3 e
  41.             Console.WriteLine("定时任务开始工作。。。");
    5 c4 F, ?# y. s9 `; d: y  G
  42.             Console.ReadKey();# {6 D4 q3 T! Y  p
  43.         }* w: l. R/ p- M4 K1 K, K
  44. , ?( b* m! _4 C
  45.         static OAuth Authorize()6 B5 O3 `# B6 _$ L& q
  46.         {" I: m# P/ h) E. ^' |0 b! W
  47.             OAuth o = new OAuth(AppKey, AppSecret, CallbackUrl);% b8 f2 ?  @5 W; z2 h
  48.             while (!ClientLogin(o))
    - Z+ p! f1 a2 a" ?
  49.             {- v8 {5 x. w4 f& }$ Z9 I9 Q
  50.                 Console.WriteLine("登录失败,请重试。");  ~) b8 m* r9 a
  51.             }1 c& M8 |: c/ m. x% _9 n  f* q, u
  52.             return o;
    : h3 X2 ^- h$ K8 O; i) S
  53.         }
    ! V9 u4 d, T' H: B! H% \# `

  54. $ v! Y3 D7 i9 g2 m5 A
  55.         private static bool ClientLogin(OAuth o)
    - D) V* ^% Y* _. [
  56.         {2 k# _/ |0 o7 o
  57.             Console.Write("微博账号:");9 [0 r- e4 ]" u5 _* n- n
  58.             string account = Console.ReadLine();  u9 g# Z) s, F8 G8 ?3 J
  59.             Console.Write("登录密码:");
    5 U5 T0 s  e! x
  60.             ConsoleColor originColor = Console.ForegroundColor;
    3 u5 f! ]0 |6 U4 r, i
  61.             Console.ForegroundColor = Console.BackgroundColor; //知道这里是在干啥不?其实是为了不让你们看到我的密码^_^
    0 Z/ Z6 I8 C( S
  62.             string password = Console.ReadLine();
    . B  ^( Y; h* Q. d& N; [
  63.             Console.ForegroundColor = originColor; //恢复前景颜色。- Z& @) A; E4 n. N5 r+ w; B
  64.             return o.ClientLogin(account, password);) P8 H* L9 D. o- S5 p
  65.         }$ V& t4 b- ~' l" m
  66. , n7 z0 [  J7 s) N1 U% |% W1 r
  67.         static void TimerElapsedEvent(object sender, ElapsedEventArgs e)" g3 P. V4 N0 C+ j! x
  68.         {
    2 T* m% ^9 d0 m: r9 o
  69.             DateTime iNow = DateTime.Now;
    5 Y3 O$ [$ D6 z. A0 B# Y* M
  70.             try
    , _+ l  e+ n- @7 X+ e5 k* e
  71.             {
    ) O1 d% n5 |3 z, m' k9 J1 K* L+ P
  72.                 string content = makeContent(iNow.Hour, iNow.Minute);
    4 k  v8 z4 ^; ~8 L* N# j
  73.                 if (!string.IsNullOrEmpty(content))
    , u* \. s: \# y" V& q
  74.                 {/ w8 `( M$ S0 Z+ a/ g9 ?3 u' {
  75.                     var statusInfo = Sina.API.Entity.Statuses.Update(content);0 G& q, J) X, `! U5 i( _; K0 m$ `
  76.                     DateTime dtCreate = DateTime.ParseExact(statusInfo.CreatedAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
    $ d+ m2 i$ U; e; s  q
  77.                     Console.WriteLine("本机时间:{0}, 微博时间:{1}, 发布内容:{2}", string.Format("{0:G}", iNow), string.Format("{0:G}", dtCreate), statusInfo.Text);/ d! ?2 q* {  y, A) ?: d$ {
  78.                 }
    5 J/ r) ~. U$ d+ M; g, \) D
  79.             }" L0 B# Q# B1 g8 n. u3 t9 \; ^% J
  80.             catch (Exception ex)
    ! w* f) i2 S$ }. I
  81.             {
    ' Z$ R0 A$ W! k/ D3 v. C/ D$ o
  82.                 Console.WriteLine("定时任务异常:" + ex.Message);/ z% \/ D9 h: l) O" l
  83.             }
    # b/ w! _# _- K7 I1 z) X
  84.         }
    3 V6 ^8 y8 u% h
  85. / |; s7 E5 C  ~3 c
  86.         static string makeContent(int hour24, int minute)% V' z# _/ O  Y  `
  87.         {" T( q9 g/ w) M* F$ R
  88.             string[] diZhi = "子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥".Split('|');
    ( \# I1 E1 n! ?  ?  [% }" l
  89.             string extWeibo = "";
    + |2 k% D( N( J, ?( P' e
  90.             if (hour24 % 2 == 0 && minute == 0)* k9 u( u" w' h/ m6 u3 A+ o
  91.             {' F2 R# W9 t4 P* }% k
  92.                 for (int i = 0; i < ((hour24 >= 12)? (hour24 - 12): hour24); i++)+ v5 r6 K( H; {
  93.                 {
      ]1 O& I& y: H+ G
  94.                     extWeibo += "铛~";
    ; D. ?( W. ], v7 F
  95.                 }4 n! Y3 q2 B* g4 ^$ B
  96.                 return "【" + diZhi[hour24 / 2] + "时】" + extWeibo;+ o. n4 x2 s% F0 d4 [
  97.             }
    8 Q- q3 k; s3 n2 e5 `: K
  98.             else8 V, M, G, R# w' Y# M: {) V7 y
  99.             {7 }; |- C7 d7 z
  100.                 return "";
    # J) B3 O5 {/ r! `
  101.             }
    + B' D3 r% [) k
  102.         }$ [  m+ G7 D) P3 q) X4 V0 G  y
  103.     }9 _9 B/ ]* v0 j# _/ _# I
  104. }
复制代码
完整程序下载:
运行前需要安装.NET Framework。 点击这里下载.NET Framework 4 并安装,这个有40多M # B5 N+ e: Z4 ]1 F1 \: N
然后下载我的程序:guchengzhonglou.rar (已经内置Weico.iPhone了哦,亲)+ H  |- t7 j9 q& q6 ~% \# O3 r1 v

& B( ~' g* W: g: @5 Z  G) k# D3 Y




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

使用道具 举报

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

使用道具 举报

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

支付宝扫一扫打赏

微信扫一扫打赏

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