您的位置:首页 > 其它

郑州158公交提醒

2012-12-14 15:34 204 查看
整体思路:

1.判断当前时间是否为下班时间

2.去公交公司网站上获取公交实时信息

3.判断候车信息 是否有必要发送,(小于一定站数就不提醒了,要不跑到公交站,车也走了。)

4.自动登录QQ

5.获取在线好友列表(隐身、离线的不发送)

6.发送提醒信息

7.将此功能做成 系统服务 ,开机自动运行。

上代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using DotNet.Common;
using System.Text.RegularExpressions;
using System.IO;
namespace ThinkWang.BusTipService
{
partial class ButTipService : ServiceBase
{
Timer timer = null;

public ButTipService()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
timer = new Timer();
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Interval = 10 * 1000;//10s
timer.Enabled = true;
timer.Start();
}

protected override void OnStop()
{
timer.Stop();
}

protected override void OnPause()
{
timer.Enabled = false;
}

protected override void OnContinue()
{
timer.Enabled = true;
}

protected void timer_Elapsed(object sender, ElapsedEventArgs e)
{
#region 指定时间段 才提醒
DateTime now = DateTime.Now;
PushMsg("\r\n**************************" + now.ToString("yyyy-MM-dd HH:mm:ss") + "****************************\r\n", false);

if (now.Hour < 17)
{
int min = (17 - now.Hour) * 60 + (30 - now.Minute);//分钟数
timer.Interval = min * 60 * 1000;//min分钟
}
else if (now.Hour == 17)
{
if (now.Minute < 30)
{
int min = 30 - now.Minute;//分钟数
timer.Interval = min * 60 * 1000;//min分钟
}
else
{
timer.Interval = 60 * 1000;//1分钟
}

}
else if (now.Hour <= 20)
{
timer.Interval = 60 * 1000;//1分钟
}
else
{
timer.Interval = 12 * 60 * 60 * 1000;//12小时
}
if (timer.Interval != 60 * 1000)//不等于1分钟
{
PushMsg(string.Format(timer.Interval / 60000 + "分钟后再执行!\r\n"));
return;
}
#endregion

#region 获取158路 候车信息
Spider spider = new Spider();

int siteNum = 2;//剩余多少站 提醒
string tipMsg158 = "";//158路的提醒信息。
string html = spider.LoadUrl("http://218.28.136.21:8081/gps.asp?xl=158%E8%B7%AF&ud=1&fx=%E7%94%B5%E8%BD%A6%E5%85%AC%E5%8F%B8&sno=8&hczd=%E5%86%9C%E4%B8%9A%E5%8D%97%E8%B7%AF%E9%87%91%E6%B0%B4%E4%B8%9C%E8%B7%AF&ref=4", "get", "", "utf-8");
Regex regMsg = new Regex("候车信息:(.*?)当前查询时间", RegexOptions.IgnoreCase);
Match m = regMsg.Match(html);
if (m.Success)
{
tipMsg158 = m.Groups[1].Value;
tipMsg158 = DotNet.Common.Text.LoseHtml(tipMsg158).Trim();
tipMsg158 = "158路:" + tipMsg158.Replace("本站", "金水东路站");
tipMsg158 += "\r\n[隐身收不到提醒,自动发送,切勿回复]";
}
if (string.IsNullOrEmpty(tipMsg158))
{
PushMsg("获取158候车信息失败!\r\n");
return;
}
else
{
PushMsg("获取158候车信息成功!\r\n");
}
#endregion

#region 判断是否需要发送 候车信息
regMsg = new Regex(@"金水东路站(\d+)站", RegexOptions.IgnoreCase);
m = regMsg.Match(tipMsg158);
if (m.Success)
{
int havSiteNum = Convert.ToInt32(m.Groups[1].Value);
if (havSiteNum >= siteNum)
{
timer.Interval = 60 * 1000;//1分钟后再去获取公交信息。
}
else
{
timer.Interval = havSiteNum * 60 * 1000;//havSiteNum 分钟后再去获取公交信息。
PushMsg("158距金水东路站小于" + siteNum + "站不发送QQ提醒!\r\n");
return;
}
}
else
{
timer.Interval = 60 * 1000;//1分钟后再去获取公交信息。
PushMsg("未检测到可提醒信息,不发送QQ提醒!\r\n");
return;
}
#endregion

#region 登录QQ
string userName = "2364995974";
string passWord = "*********";
passWord = DotNet.Common.Text.Md5(passWord, 32, "");
string arg = string.Format("VER=1.1&CMD=Login&SEQ={2}&UIN={0}&PS={1}&M5=1&LC=9326B87B234E7235", userName, passWord, DateTime.Now.Ticks.ToString().Substring(7, 7));
html = spider.LoadUrl("http://tqq.tencent.com:8000/", "post", arg);
if (html.Contains("&RS=0&"))
{
PushMsg("登录成功!\r\n");
}
else
{
PushMsg("登录失败!" + html.Substring(html.IndexOf("RA=") + 3) + "\r\n");
return;
}
#endregion

#region 获取在线好友列表
arg = string.Format("VER=1.1&CMD=Query_Stat&SEQ={1}&UIN={0}&TN=50&UN=0", userName, DateTime.Now.Ticks.ToString().Substring(7, 7));
html = spider.LoadUrl("http://tqq.tencent.com:8000/", "post", arg);
string uids = "";//好友qq号
string[] uid = null;
string unicks = "";//好友昵称
string[] unick = null;
foreach (var item in html.Split('&'))
{
if (item.Contains("UN="))
{
uids = item.Substring(3);
}
else if (item.Contains("NK="))
{
unicks = item.Substring(3);
}
}

if (uids.Trim(',').Length < 1)
{
PushMsg("发送失败!获取好友列表失败,或好友列表为空\r\n");
return;
}
#endregion

#region 在线好友发送提醒
uid = uids.Trim(',').Split(',');
unick = unicks.Trim(',').Split(',');
PushMsg(string.Format("本次发送{0}个提示信息。\r\n", uid.Length));
for (int i = 0; i < unick.Length; i++)
{
arg = string.Format("VER=1.1&CMD=CLTMSG&SEQ={3}&UIN={0}&UN={1}&MG={2}", userName, uid[i], tipMsg158, DateTime.Now.Ticks.ToString().Substring(7, 7));
html = spider.LoadUrl("http://tqq.tencent.com:8000/", "post", arg);
PushMsg((i + 1) + ". " + unick[i]);
if (html.Contains("&RES=0"))
{
PushMsg(" 发送成功!\r\n");
}
else
{
PushMsg(" 发送失败!");
if (html.Contains("&RES=3"))
{
PushMsg("发送过快!\r\n");
}
else if (html.Contains("&RES=20"))
{
PushMsg("登录超时!\r\n");
}

}
System.Threading.Thread.Sleep(new Random().Next(400, 1100));
}
PushMsg(" 全部发送完毕!\r\n");
#endregion

#region 退出QQ
//arg = string.Format("VER=1.1&CMD=Logout&SEQ={1}&UIN={0}", userName, DateTime.Now.Ticks.ToString().Substring(7, 7));
//html = spider.LoadUrl("http://tqq.tencent.com:8000/", "post", arg);
//if (html.Contains("&RES=0"))
//{
//    PushMsg("退出成功!\r\n");
//}
//else
//{
//    PushMsg("退出失败!");
//    if (html.Contains("&RES=20"))
//    {
//        PushMsg("未登录!\r\n");
//    }
//}
#endregion
}

/// <summary>
/// 记录日志
/// </summary>
/// <param name="s"></param>
protected void PushMsg(string s, bool isNeedTime = true)
{
string path = "E:/ButTip.log";
if (isNeedTime)
s = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "   " + s;
File.AppendAllText(path, s);
}
}
}


本来想发送完信息后,退出QQ的,后来怕频繁登录退出,会被屏蔽发送信息,所以把那些代码注释了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: