大家最近都在热议古城钟楼的红火。不是加V号、不是官方号、不是皇冠会员。没有拟人化,没有段子、没有运营。1天之内从1万不到的粉丝新增了20多万并且还在持续传播和增长粉丝。我不研究它是怎么红起来的,但写个类似的程序实现它却很容易,程序才100行左右: & R( J) I3 O7 n( O% z& w
先来普及一下地支时间与北京时间的对应关系: - 【子时】夜半,又名子夜、中夜:十二时辰的第一个时辰。(北京时间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 ` g6 v5 J- P- d
. {8 R1 k/ h. N然后我们来分析一下它昨天发布的内容 - 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 【亥时】铛~铛~铛~铛~铛~铛~铛~铛~铛~铛~) E8 d3 p8 p9 J' F# ]
( T0 j3 Q+ [3 L8 R. N9 @- @) u根据上面,得出它有以下规律: - 每两个小时发布一条微博,
- 发布时间是偶数小时的0分,这个时间并不是地支时间的开始时间,而是正中间的那个时间点
- 内容是相应的地支时间,加上根据时间而变化的“铛~”的次数,
- “铛~”的次数与时有关,12小时以前与小时相同,12小时以后与[小时减12]相同。
; r% T8 n/ B# p0 p7 B
西安钟楼里的钟就是这样敲的吗?去过钟楼一次,没登上去,也没注意。
9 z- H* B# B! Q1 H( t2 @8 N3 k完整代码如下: 需要自行修改App相关值: & s0 H" T) j, K3 {' Z
- - H4 C3 U7 A2 H0 S! W8 ?, A
- <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>
复制代码- using System;0 x7 ?. l/ W3 G/ L: }$ h0 R
- using System.Collections.Generic;
, j, x9 Q( K6 ] - using System.Text;9 w8 s8 a$ J2 H J, c. E
- using NetDimension.Weibo;
( _* d- s' i6 b: f - using System.Timers;
! k, R4 ~/ W2 g# N0 j - using System.Globalization;
* p% k1 {# x8 q9 ~8 _. A( A9 p - # c/ K# N# B! E! f! C, p; o
- namespace WeiboTimer
3 [! n. t4 J8 |+ Q! k - {
* Q8 s% z0 I( l* h' ^ - class Program9 v: f, t# E, k. W6 Q' X* U
- {
2 S8 z' n" S7 B! R# s% M/ G" ^ - static string AppKey = "********";
; D0 d! J- b$ t g6 v2 b - static string AppSecret = "*******";& C' Z& z5 M% F% p. v$ @( R+ t0 o: J
- static string CallbackUrl = "****************";
, `7 f( ?1 |8 B& N, O - static Timer t = new Timer();
' s+ Q& x4 k, @" J; \7 L - static OAuth oauth = null;( y* ?8 r8 ]8 q8 C% c
- static Client Sina = null;
; D3 Y( W4 | F8 O# b - / Y: d8 B5 _, T7 d [( Z7 L) Q
- static void Main(string[] args)' m( s5 l, N- D' }9 V6 W
- {
; @. s8 @" s6 o% g7 I3 L - oauth = Authorize();: }4 W& J b* A, `, P. B7 T
- if (!string.IsNullOrEmpty(oauth.AccessToken)){Console.Write("登录成功!");}& E, q) X% Z6 f# X5 Z y5 y ~
- ' u$ a' b( S* y0 N2 X2 m
- Sina = new Client(oauth);7 o9 |5 c3 w* \6 I+ l2 B
- string uid = Sina.API.Entity.Account.GetUID();! b+ I/ O& A" s
- var entity_userInfo = Sina.API.Entity.Users.Show(uid);
$ j: A6 {* R$ j& t - Console.WriteLine("昵称:{0},微博地址:http://weibo.com/{1}", entity_userInfo.ScreenName, entity_userInfo.ProfileUrl);
& o& m7 q$ Z ~ o& n6 ?
1 @2 } ?' K& {+ ?' t0 t- try5 x4 J; \, a% Q
- {+ C" g7 K0 G' j( Y: B
- t.Interval = 1000 * 60; //设置间隔时间为1分钟
+ B# _) R: [& h3 r& `4 D) i - t.Elapsed += new ElapsedEventHandler(TimerElapsedEvent); //到达时间的时候执行事件; # C3 `" Q9 _+ b7 W% {% V* c$ R7 E, s
- t.AutoReset = true; //设置是执行一次(false)还是一直执行(true);
0 D+ R5 S! U" n - t.Start(); //启动Timer对象;
3 [" v' K' @. \3 N# t* c I0 L - }
$ L% I' ?0 g B f9 N - catch (Exception ex)
! j4 s7 k% x* V: ~) Y1 r - {0 {+ }8 O/ H# d
- Console.WriteLine("定时任务异常:" + ex.Message);
+ C+ r* L! @* t# d; W/ b% D - t.Stop();
# T% m2 r, N# H; V0 D* o0 X - }
0 W! y' U, F- t6 B# G - Console.WriteLine("定时任务开始工作。。。");8 E8 ?) H1 M. d; O6 {
- Console.ReadKey(); v# [/ r- g& ?' j
- }# M) A. S0 t- @5 n, l
9 W' b, p. s# u: Q4 U# k" [4 f- static OAuth Authorize()
7 v# n& L, w/ G3 p2 s* N8 e& \8 u - {
; ^3 I4 Y9 V# l% B/ D - OAuth o = new OAuth(AppKey, AppSecret, CallbackUrl);2 V2 H7 A/ E, S* _. G
- while (!ClientLogin(o))
+ K" ]4 K! T. Z3 \8 s2 ^# [ - {
+ i- x; K& Y+ |# \- H- R - Console.WriteLine("登录失败,请重试。");
0 Z/ j! E5 K/ C - }
0 k) f9 U7 G' \3 f, p: j - return o;
. |3 s) h2 I) R! Z- z+ q; t1 C - }* x" ^* x6 ]2 _ R! B
7 D& P! k( o/ Z& e& f/ U- private static bool ClientLogin(OAuth o)+ R1 l( w: \- E) ~" G1 g
- {
& S1 [, _. `% d) h8 u9 D - Console.Write("微博账号:");
: R& }4 w2 R( A - string account = Console.ReadLine();
6 M7 Y% U: I/ K+ P+ y9 p - Console.Write("登录密码:");
$ X/ M/ E3 W) k0 l; g6 V - ConsoleColor originColor = Console.ForegroundColor;8 Y1 n. x" }9 G
- Console.ForegroundColor = Console.BackgroundColor; //知道这里是在干啥不?其实是为了不让你们看到我的密码^_^
5 L" R( S( @- r* e' q - string password = Console.ReadLine();: Z, a8 O; ^ P% }5 F2 {3 i
- Console.ForegroundColor = originColor; //恢复前景颜色。
+ [2 A7 |: V# t( z5 l3 f& c& J [ - return o.ClientLogin(account, password);
% n, `! q+ q0 m$ `) Z& a$ k - }
- l$ q y5 M, i4 n* Q - & h, x7 _6 z: r) r
- static void TimerElapsedEvent(object sender, ElapsedEventArgs e)
, b! f5 G) t/ S$ d8 g - {
( _$ U, M |* ~) G" R. C - DateTime iNow = DateTime.Now;- d9 f2 ?' i4 d$ Z
- try) E1 `# w: @2 {4 E, z4 k
- {1 d6 i4 L, E. g! a- o
- string content = makeContent(iNow.Hour, iNow.Minute);
. f) I( w2 n& [) R - if (!string.IsNullOrEmpty(content)) a$ E" j3 ?" { [9 J
- {+ j% r( D' g4 j K
- var statusInfo = Sina.API.Entity.Statuses.Update(content);
/ q( V( ]! |2 E7 l: ~ - DateTime dtCreate = DateTime.ParseExact(statusInfo.CreatedAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);& X! J- _2 Q9 s3 O3 i
- Console.WriteLine("本机时间:{0}, 微博时间:{1}, 发布内容:{2}", string.Format("{0:G}", iNow), string.Format("{0:G}", dtCreate), statusInfo.Text);
" [+ Y/ r, w7 W1 @" \ - }+ m; k$ w* W$ A- x9 L
- }
/ G. O& F: q2 K/ o - catch (Exception ex)9 E5 A# n9 b' C5 m% [! h
- {
) } b2 z7 K/ z4 J& X9 q - Console.WriteLine("定时任务异常:" + ex.Message);# ^3 C, v/ w$ k6 m$ c+ G! |
- }
% @; c$ k5 Q- B% H7 [/ y& T' ~ - }
% j- w9 S8 x: ?- @; R- W) l5 ~4 @
* ?% `" n% Q6 Y. c$ R! U- static string makeContent(int hour24, int minute)
! S6 L& t1 `8 i. l* p1 V; q - {
: n4 n9 f4 `' x* O7 w. ` - string[] diZhi = "子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥".Split('|');: e. E A* H* F' z8 b: S3 w
- string extWeibo = "";
3 W' J1 f; [( S - if (hour24 % 2 == 0 && minute == 0)
' I5 `7 M) ?. `8 ?( j% S - {- O$ p4 w5 R0 Z" i
- for (int i = 0; i < ((hour24 >= 12)? (hour24 - 12): hour24); i++)
6 c" s# g8 S( `4 g8 @ - {8 g8 y8 q2 c4 {; w- f4 S
- extWeibo += "铛~";
: l( l; ]! Y: q4 b* b - }
8 I$ J# j0 \4 G% N Q* { - return "【" + diZhi[hour24 / 2] + "时】" + extWeibo;
6 {9 Z4 ]7 p5 T" O7 ?4 N& C - }/ u6 ^/ K9 E6 B e7 R
- else$ N7 x6 ^5 f$ v( c
- {* X6 {/ [* `- H8 Q( F& V' V
- return "";
6 D7 n& C2 T$ h - }; w5 F* T) T" t8 J% b
- }2 h1 k3 M8 C; c7 T
- }( R+ n% U: e- T& ]1 T8 U" k
- }
复制代码完整程序下载:
8 E1 \" \# \; Q4 |. G- A, C! e |
上一篇:微信支付下一篇:微信公众平台开发(66)人品计算
|