来源:自学PHP网 时间:2015-04-14 12:58 作者: 阅读:次
[导读] 古城钟楼 微博 铛古城钟楼微博地支 报时自动 一天20万粉丝 程序作者:http: txw1958 cnblogs com 原文:http: www cnblogs com txw1958 archive 2013 01 09 guchengzhonglou-weibo-timer html大家最近都...
|
大家最近都在热议古城钟楼的红火。不是加V号、不是官方号、不是皇冠会员。没有拟人化,没有段子、没有运营。1天之内从1万不到的粉丝新增了20多万并且还在持续传播和增长粉丝。我不研究它是怎么红起来的,但写个类似的程序实现它却很容易,程序才100行左右:
先来普及一下地支时间与北京时间的对应关系:
然后我们来分析一下它昨天发布的内容
根据上面,得出它有以下规律:
西安钟楼里的钟就是这样敲的吗?去过钟楼一次,没登上去,也没注意。
好了,开始了: static string AppKey = "12345678"; static string AppSecret = "7234545228a7626f7767778b1e249e6a"; static string CallbackUrl = "https://www.doucube.com/oauth2/default.html";
我们使用C#来实现这个程序,利用新浪微博官方推荐的Weibo API OAuth2 C#版 第一步,就是用API登录,下面是API中的源码,去掉了一些冗余的东东,
static OAuth Authorize()
{
OAuth o = new OAuth(AppKey, AppSecret, CallbackUrl);
while (!ClientLogin(o))
{
Console.WriteLine("登录失败,请重试。");
}
return o;
}
在这里实现手动填写微博账号和登录密码,同时登录密码不显示(前景色和背景色相同)
private static bool ClientLogin(OAuth o)
{
Console.Write("微博账号:");
string account = Console.ReadLine();
Console.Write("登录密码:");
ConsoleColor originColor = Console.ForegroundColor;
Console.ForegroundColor = Console.BackgroundColor; //知道这里是在干啥不?其实是为了不让你们看到我的密码^_^
string password = Console.ReadLine();
Console.ForegroundColor = originColor;
return o.ClientLogin(account, password);
}
登录成功后,返回自己的uid,昵称,证明是自己登上去了
Sina = new Client(oauth);
string uid = Sina.API.Entity.Account.GetUID();
var entity_userInfo = Sina.API.Entity.Users.Show(uid);
Console.WriteLine("昵称:{0},微博地址:http://weibo.com/{1}", entity_userInfo.ScreenName, entity_userInfo.ProfileUrl);
接下来是一个比较重要的的定时任务, 我们使用一个定时器,因为要精确到分钟,所以程序需要每分钟执行一次,
try
{
t.Interval = 1000 * 60; //设置间隔时间为1分钟
t.Elapsed += new ElapsedEventHandler(TimerElapsedEvent); //到达时间的时候执行事件;
t.AutoReset = true; //设置是执行一次(false)还是一直执行(true);
t.Start(); //启动Timer对象;
}
catch (Exception ex)
{
Console.WriteLine("定时任务异常:" + ex.Message);
t.Stop();
}
再接下来就是根据小时和分钟判断当前要不要“铛~”一下,根据上面的分析,写一个函数,传入24小时制的时间和分钟,如果需要“铛~”,就返回要发布的微博内容:
static string makeContent(int hour24, int minute)
{
//地支时间做成数组
string[] diZhi = "子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥".Split('|');
string extWeibo = "";
//当前是偶数小时且分钟为0
if (hour24 % 2 == 0 && minute == 0)
{
//根据小时来数敲多少钟
for (int i = 0; i < ((hour24 >= 12)? (hour24 - 12): hour24); i++)
{
extWeibo += "铛~";
}
return "【" + diZhi[hour24 / 2] + "时】" + extWeibo;
}
else
{
return "";
}
}
最后,如果当前需要敲钟,就把微博发布上去,如果发现发布时间和微博时间不同步怎么办?
if (!string.IsNullOrEmpty(content))
{
var statusInfo = Sina.API.Entity.Statuses.Update(content);
DateTime dtCreate = DateTime.ParseExact(statusInfo.CreatedAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
Console.WriteLine("本机时间:{0}, 微博时间:{1}, 发布内容:{2}", string.Format("{0:G}", iNow), string.Format("{0:G}", dtCreate), statusInfo.Text);
}
程序测试时运行界面如下:
完整代码如下: 需要自行修改App相关值:
using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using NetDimension.Weibo;
5 using System.Timers;
6 using System.Globalization;
7
8 namespace WeiboTimer
9 {
10 class Program
11 {
12 static string AppKey = "********";
13 static string AppSecret = "*******";
14 static string CallbackUrl = "****************";
15 static Timer t = new Timer();
16 static OAuth oauth = null;
17 static Client Sina = null;
18
19 static void Main(string[] args)
20 {
21 oauth = Authorize();
22 if (!string.IsNullOrEmpty(oauth.AccessToken)){Console.Write("登录成功!");}
23
24 Sina = new Client(oauth);
25 string uid = Sina.API.Entity.Account.GetUID();
26 var entity_userInfo = Sina.API.Entity.Users.Show(uid);
27 Console.WriteLine("昵称:{0},微博地址:http://weibo.com/{1}", entity_userInfo.ScreenName, entity_userInfo.ProfileUrl);
28
29 try
30 {
31 t.Interval = 1000 * 60; //设置间隔时间为1分钟
32 t.Elapsed += new ElapsedEventHandler(TimerElapsedEvent); //到达时间的时候执行事件;
33 t.AutoReset = true; //设置是执行一次(false)还是一直执行(true);
34 t.Start(); //启动Timer对象;
35 }
36 catch (Exception ex)
37 {
38 Console.WriteLine("定时任务异常:" + ex.Message);
39 t.Stop();
40 }
41 Console.WriteLine("定时任务开始工作。。。");
42 Console.ReadKey();
43 }
44
45 static OAuth Authorize()
46 {
47 OAuth o = new OAuth(AppKey, AppSecret, CallbackUrl);
48 while (!ClientLogin(o))
49 {
50 Console.WriteLine("登录失败,请重试。");
51 }
52 return o;
53 }
54
55 private static bool ClientLogin(OAuth o)
56 {
57 Console.Write("微博账号:");
58 string account = Console.ReadLine();
59 Console.Write("登录密码:");
60 ConsoleColor originColor = Console.ForegroundColor;
61 Console.ForegroundColor = Console.BackgroundColor; //知道这里是在干啥不?其实是为了不让你们看到我的密码^_^
62 string password = Console.ReadLine();
63 Console.ForegroundColor = originColor; //恢复前景颜色。
64 return o.ClientLogin(account, password);
65 }
66
67 static void TimerElapsedEvent(object sender, ElapsedEventArgs e)
68 {
69 DateTime iNow = DateTime.Now;
70 try
71 {
72 string content = makeContent(iNow.Hour, iNow.Minute);
73 if (!string.IsNullOrEmpty(content))
74 {
75 var statusInfo = Sina.API.Entity.Statuses.Update(content);
76 DateTime dtCreate = DateTime.ParseExact(statusInfo.CreatedAt, "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
77 Console.WriteLine("本机时间:{0}, 微博时间:{1}, 发布内容:{2}", string.Format("{0:G}", iNow), string.Format("{0:G}", dtCreate), statusInfo.Text);
78 }
79 }
80 catch (Exception ex)
81 {
82 Console.WriteLine("定时任务异常:" + ex.Message);
83 }
84 }
85
86 static string makeContent(int hour24, int minute)
87 {
88 string[] diZhi = "子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥".Split('|');
89 string extWeibo = "";
90 if (hour24 % 2 == 0 && minute == 0)
91 {
92 for (int i = 0; i < ((hour24 >= 12)? (hour24 - 12): hour24); i++)
93 {
94 extWeibo += "铛~";
95 }
96 return "【" + diZhi[hour24 / 2] + "时】" + extWeibo;
97 }
98 else
99 {
100 return "";
101 }
102 }
103 }
104 }
完整程序下载: 运行前需要安装.NET Framework。 点击这里下载.NET Framework 4 并安装,这个有40多M (已经内置Weico.iPhone了哦,亲)
|
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com