Commit faac9b0d by 石璀亮

加入服务器端开发说明以及代码

1 parent 0ba16b92
# BirdNotice
![](https://www.toscl.com/i/uploads/149999838281.png)
通过对 nbbsw.com 进行模拟登入,服务器对页面解析生成邮件信息 JSON 接口, 随后服务器根据邮件更新时间判断邮是否更新,如果更新,
对 Android 端进行邮件提醒,服务器端 20 秒更新一次,收到新邮件发送通知到Android.
## 服务器模拟登入获取邮件信息生成接口 - PHP
```
server_parse -> Login.php
```
## 服务器新邮件判断以及发送 - python
```
server_push -> xinge.py
```
<resources>
<string name="app_name">XGDemo</string>
<string name="app_name">BirdNotice</string>
<string name="action_search">搜索</string>
<string name="action_device_token">设备Token</string>
<string name="action_help_center">帮助中心</string>
......
<?php
require("simple_html_dom.php");
class Login
{
public function curl_Login($login_url, $post_fields, $id){
//cookie文件存放在网站根目录的temp文件夹下
$cookie_file = tempnam('./temp','cookie'.$id);
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
// 用CURL的抓取cookies的方法
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); //在CURL请求中发送cookies值
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); //在CURL请求中,把head头的cooked存入到指定的文件中
//
curl_exec($ch);
curl_close($ch);
return $cookie_file;
}
public function login($id, $idcard, $runUrl, $post_fields=""){
$url = "http://nbbsw.com//names.nsf?Login";
$post['username'] = $id;
$post['password'] = $idcard;
$cookie_file = $this->curl_Login($url,$post, $id);
$this->run($runUrl, $cookie_file);
}
function run($send_url,$cookie_file, $post_fields=""){
header('Content-type: text/json;charset=utf-8');
$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); //在CURL请求中发送cookies值
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); //在CURL请求中,把head头的cooked存入到指定的文件中
if ($post_fields!="") {
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
}
$contents = curl_exec($ch);
curl_close($ch);
//清理cookie文件
unlink($cookie_file);
$html = str_get_html($contents);
$tbody = $html->find("table", 1);
$i = -1;
$info = array(array());
foreach($tbody->find("tr") as $tr){
if($i != -1){
$info[$i]["name"] = $tr->find("td", 4)->plaintext;
$info[$i]["time"] = $tr->find("td", 6)->plaintext." ".$tr->find("td", 7)->plaintext;
$info[$i]["msg"] = $tr->find("td", 10)->plaintext;
}
$i++;
}
#echo json_encode($info, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
echo json_encode($info);
}
}
if (!@$_GET['id']) {
$idcard = ''; # password
$id = ''; # your email
$runUrl = "http://nbbsw.com/mail/cuiliangshi.nsf/3017aba72c3fec9248256c5200444537?ReadForm&Count=30&ResortDescending=4&TemplateType=2&TargetUNID=F642F2D152FB45B248256C52004478D5&Seq=4&AutoFramed"; # your newest emial url
}
$run = new Login($id, $idcard, $runUrl);
import xinge_push
import json
import urllib2
import sys
reload(sys)
sys.setdefaultencoding('utf8')
def wrapper_message(current_info, token):
xinge = xinge_push.XingeApp("", '') # TODO your token
#build your message
msg = xinge_push.Message()
msg.type = xinge_push.MESSAGE_TYPE_ANDROID_NOTIFICATION
msg.title = current_info['name']
msg.content = current_info['msg'] + "," + current_info['time']
msg_style = xinge_push.Style(0,1,1,0)
msg.style = msg_style
#call restful API
ret_code, error_msg = xinge.PushSingleDevice(token, msg)
if ret_code:
print "push failed!"
else:
print "push successfully!:" + msg.title + msg.content
def send_message(send_id, token):
json_body = urllib2.urlopen("https://www.toscl.com/i/push_message/login.php?id=" + send_id).read()
#json_body = json_body.replace('[','{').replace(']',']')
json_handle = json.loads(json_body)
len_json = len(json_handle)
latest_info = json_handle[0]
latest_info_time = latest_info["time"]
input = open('/root/python/len_' + send_id + '.txt','r')
file_time = input.read()
current_info = latest_info
current_index = 0
current_info_time = latest_info_time
write_file = open('/root/python/len_' + send_id + '.txt','w')
write_file.write(current_info_time)
try:
while current_info_time != file_time:
print current_info_time
print " = Ready - " + send_id
print file_time
current_info = json_handle[current_index]
wrapper_message(current_info, token)
current_index += 1
current_info_time = json_handle[current_index]["time"]
except Exception as e:
print e
send_message("file_enfix", "device token"); # TODO
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!