Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
.www.
wechaty
提交
f07a3c62
W
wechaty
项目概览
.www.
/
wechaty
与 Fork 源项目一致
Fork自
wechaty / wechaty
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
wechaty
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f07a3c62
编写于
8月 08, 2016
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add UUID support for io protocol
上级
9b898a5a
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
71 addition
and
41 deletion
+71
-41
src/contact.js
src/contact.js
+4
-4
src/io.js
src/io.js
+12
-3
src/message-media.js
src/message-media.js
+2
-2
src/message.js
src/message.js
+4
-4
src/puppet-web/browser.js
src/puppet-web/browser.js
+9
-3
src/room.js
src/room.js
+3
-3
src/util.js
src/util.js
+27
-16
src/wechaty.js
src/wechaty.js
+4
-0
test/util.spec.js
test/util.spec.js
+6
-6
未找到文件。
src/contact.js
浏览文件 @
f07a3c62
...
@@ -6,8 +6,8 @@
...
@@ -6,8 +6,8 @@
* https://github.com/zixia/wechaty
* https://github.com/zixia/wechaty
*
*
*/
*/
const
log
=
require
(
'
./npmlog-env
'
)
const
log
=
require
(
'
./npmlog-env
'
)
const
webUtil
=
require
(
'
./web-
util
'
)
const
Util
=
require
(
'
./
util
'
)
class
Contact
{
class
Contact
{
constructor
(
id
)
{
constructor
(
id
)
{
...
@@ -40,8 +40,8 @@ class Contact {
...
@@ -40,8 +40,8 @@ class Contact {
,
stranger
:
!!
rawObj
.
stranger
// assign by injectio.js
,
stranger
:
!!
rawObj
.
stranger
// assign by injectio.js
}
}
}
}
name
()
{
return
web
Util
.
plainText
(
this
.
obj
.
name
)
}
name
()
{
return
Util
.
plainText
(
this
.
obj
.
name
)
}
remark
()
{
return
this
.
obj
.
remark
}
remark
()
{
return
this
.
obj
.
remark
}
stranger
()
{
return
this
.
obj
.
stranger
}
stranger
()
{
return
this
.
obj
.
stranger
}
star
()
{
return
this
.
obj
.
star
}
star
()
{
return
this
.
obj
.
star
}
...
...
src/io.js
浏览文件 @
f07a3c62
...
@@ -30,8 +30,9 @@ class Io {
...
@@ -30,8 +30,9 @@ class Io {
this
.
wechaty
=
wechaty
this
.
wechaty
=
wechaty
this
.
token
=
token
this
.
token
=
token
this
.
endpoint
=
endpoint
this
.
endpoint
=
endpoint
this
.
protocol
=
protocol
log
.
verbose
(
'
Io
'
,
'
instantiated with endpoint[%s], token[%s], protocol[%s]
'
,
endpoint
,
token
,
protocol
)
this
.
protocol
=
protocol
+
'
|
'
+
wechaty
.
uuid
log
.
verbose
(
'
Io
'
,
'
instantiated with endpoint[%s], token[%s], protocol[%s], uuid[%s]
'
,
endpoint
,
token
,
protocol
,
this
.
uuid
)
}
}
toString
()
{
return
'
Class Io(
'
+
this
.
token
+
'
)
'
}
toString
()
{
return
'
Class Io(
'
+
this
.
token
+
'
)
'
}
...
@@ -74,7 +75,7 @@ class Io {
...
@@ -74,7 +75,7 @@ class Io {
const
initEvent
=
{
const
initEvent
=
{
name
:
'
sys
'
name
:
'
sys
'
,
payload
:
'
Wechaty version
'
+
this
.
wechaty
.
version
()
,
payload
:
'
Wechaty version
'
+
this
.
wechaty
.
version
()
+
` with UUID:
${
this
.
uuid
}
`
}
}
this
.
send
(
initEvent
)
this
.
send
(
initEvent
)
...
@@ -227,6 +228,13 @@ class Io {
...
@@ -227,6 +228,13 @@ class Io {
ioEvent
.
payload
=
data
.
toString
()
ioEvent
.
payload
=
data
.
toString
()
break
break
case
'
heartbeat
'
:
ioEvent
.
payload
=
{
uuid
:
this
.
uuid
,
data
:
data
}
break
default
:
default
:
break
break
}
}
...
@@ -277,6 +285,7 @@ class Io {
...
@@ -277,6 +285,7 @@ class Io {
ioMessage
(
m
)
{
ioMessage
(
m
)
{
log
.
verbose
(
'
Io
'
,
'
ioMessage() is a nop function before be overwriten from cloud
'
)
log
.
verbose
(
'
Io
'
,
'
ioMessage() is a nop function before be overwriten from cloud
'
)
}
}
}
}
/**
/**
...
...
src/message-media.js
浏览文件 @
f07a3c62
...
@@ -9,7 +9,7 @@
...
@@ -9,7 +9,7 @@
const
co
=
require
(
'
co
'
)
const
co
=
require
(
'
co
'
)
const
log
=
require
(
'
./npmlog-env
'
)
const
log
=
require
(
'
./npmlog-env
'
)
const
webUtil
=
require
(
'
./web-
util
'
)
const
Util
=
require
(
'
./
util
'
)
const
Message
=
require
(
'
./message
'
)
const
Message
=
require
(
'
./message
'
)
class
MediaMessage
extends
Message
{
class
MediaMessage
extends
Message
{
...
@@ -47,7 +47,7 @@ class MediaMessage extends Message {
...
@@ -47,7 +47,7 @@ class MediaMessage extends Message {
return
Message
.
puppet
.
browser
.
checkSession
()
return
Message
.
puppet
.
browser
.
checkSession
()
})
})
.
then
(
cookies
=>
{
.
then
(
cookies
=>
{
return
web
Util
.
downloadStream
(
this
.
obj
.
url
,
cookies
)
return
Util
.
downloadStream
(
this
.
obj
.
url
,
cookies
)
})
})
.
catch
(
e
=>
{
.
catch
(
e
=>
{
log
.
warn
(
'
MediaMessage
'
,
'
stream() exception: %s
'
,
e
.
message
)
log
.
warn
(
'
MediaMessage
'
,
'
stream() exception: %s
'
,
e
.
message
)
...
...
src/message.js
浏览文件 @
f07a3c62
...
@@ -11,8 +11,8 @@ const co = require('co')
...
@@ -11,8 +11,8 @@ const co = require('co')
const
Contact
=
require
(
'
./contact
'
)
const
Contact
=
require
(
'
./contact
'
)
const
Room
=
require
(
'
./room
'
)
const
Room
=
require
(
'
./room
'
)
const
webUtil
=
require
(
'
./web-
util
'
)
const
Util
=
require
(
'
./
util
'
)
const
log
=
require
(
'
./npmlog-env
'
)
const
log
=
require
(
'
./npmlog-env
'
)
class
Message
{
class
Message
{
constructor
(
rawObj
)
{
constructor
(
rawObj
)
{
...
@@ -52,7 +52,7 @@ class Message {
...
@@ -52,7 +52,7 @@ class Message {
return
obj
return
obj
}
}
toString
()
{
toString
()
{
return
web
Util
.
plainText
(
this
.
obj
.
content
)
return
Util
.
plainText
(
this
.
obj
.
content
)
}
}
toStringDigest
()
{
toStringDigest
()
{
const
text
=
webUtil
.
digestEmoji
(
this
.
obj
.
digest
)
const
text
=
webUtil
.
digestEmoji
(
this
.
obj
.
digest
)
...
@@ -71,7 +71,7 @@ class Message {
...
@@ -71,7 +71,7 @@ class Message {
return
'
<
'
+
name
+
(
room
?
`@
${
room
}
`
:
''
)
+
'
>
'
return
'
<
'
+
name
+
(
room
?
`@
${
room
}
`
:
''
)
+
'
>
'
}
}
getContentString
()
{
getContentString
()
{
let
content
=
web
Util
.
plainText
(
this
.
obj
.
content
)
let
content
=
Util
.
plainText
(
this
.
obj
.
content
)
if
(
content
.
length
>
20
)
{
content
=
content
.
substring
(
0
,
17
)
+
'
...
'
}
if
(
content
.
length
>
20
)
{
content
=
content
.
substring
(
0
,
17
)
+
'
...
'
}
return
'
{
'
+
this
.
type
()
+
'
}
'
+
content
return
'
{
'
+
this
.
type
()
+
'
}
'
+
content
}
}
...
...
src/puppet-web/browser.js
浏览文件 @
f07a3c62
...
@@ -92,8 +92,9 @@ class Browser extends EventEmitter {
...
@@ -92,8 +92,9 @@ class Browser extends EventEmitter {
// XXX: if no `setTimeout()` here, promise will hang forever!
// XXX: if no `setTimeout()` here, promise will hang forever!
// with a confirmed bug in selenium-webdriver v2.53.2:
// with a confirmed bug in selenium-webdriver v2.53.2:
// https://github.com/SeleniumHQ/selenium/issues/2233
// https://github.com/SeleniumHQ/selenium/issues/2233
setTimeout
(()
=>
{
resolve
(
this
.
driver
)
},
0
)
// FIXED: selenium v3 released 20160807
// resolve(this.driver)
// setTimeout(() => { resolve(this.driver) }, 0)
resolve
(
this
.
driver
)
})
})
}
}
...
@@ -135,9 +136,14 @@ class Browser extends EventEmitter {
...
@@ -135,9 +136,14 @@ class Browser extends EventEmitter {
log
.
silly
(
'
PuppetWebBrowser
'
,
'
phantomjs binary:
'
+
phantomjsExe
)
log
.
silly
(
'
PuppetWebBrowser
'
,
'
phantomjs binary:
'
+
phantomjsExe
)
log
.
silly
(
'
PuppetWebBrowser
'
,
'
phantomjs args:
'
+
phantomjsArgs
.
join
(
'
'
))
log
.
silly
(
'
PuppetWebBrowser
'
,
'
phantomjs args:
'
+
phantomjsArgs
.
join
(
'
'
))
return
new
WebDriver
.
Builder
()
const
driver
=
new
WebDriver
.
Builder
()
.
withCapabilities
(
customPhantom
)
.
withCapabilities
(
customPhantom
)
.
build
()
.
build
()
// https://github.com/detro/ghostdriver/blob/f976007a431e634a3ca981eea743a2686ebed38e/src/session.js#L233
// driver.manage().timeouts().pageLoadTimeout(2000)
return
driver
}
}
quit
()
{
quit
()
{
...
...
src/room.js
浏览文件 @
f07a3c62
...
@@ -6,8 +6,8 @@
...
@@ -6,8 +6,8 @@
* https://github.com/zixia/wechaty
* https://github.com/zixia/wechaty
*
*
*/
*/
const
log
=
require
(
'
./npmlog-env
'
)
const
log
=
require
(
'
./npmlog-env
'
)
const
webUtil
=
require
(
'
./web-
util
'
)
const
Util
=
require
(
'
./
util
'
)
class
Room
{
class
Room
{
constructor
(
id
)
{
constructor
(
id
)
{
...
@@ -46,7 +46,7 @@ class Room {
...
@@ -46,7 +46,7 @@ class Room {
})
})
}
}
name
()
{
return
web
Util
.
plainText
(
this
.
obj
.
name
)
}
name
()
{
return
Util
.
plainText
(
this
.
obj
.
name
)
}
get
(
prop
)
{
return
this
.
obj
[
prop
]
}
get
(
prop
)
{
return
this
.
obj
[
prop
]
}
parse
(
rawObj
)
{
parse
(
rawObj
)
{
...
...
src/
web-
util.js
→
src/util.js
浏览文件 @
f07a3c62
const
http
=
require
(
'
http
'
)
const
http
=
require
(
'
http
'
)
const
log
=
require
(
'
./npmlog-env
'
)
const
log
=
require
(
'
./npmlog-env
'
)
const
WebUtil
=
{
stripHtml
:
stripHtml
,
unescapeHtml
:
unescapeHtml
,
digestEmoji
:
digestEmoji
,
plainText
:
plainText
,
downloadStream
:
downloadStream
const
Util
=
{
stripHtml
,
unescapeHtml
,
digestEmoji
,
plainText
,
downloadStream
,
guid
}
}
function
stripHtml
(
html
)
{
function
stripHtml
(
html
)
{
...
@@ -44,8 +46,8 @@ function plainText(html) {
...
@@ -44,8 +46,8 @@ function plainText(html) {
}
}
function
downloadStream
(
url
,
cookies
)
{
function
downloadStream
(
url
,
cookies
)
{
// const myurl = 'http://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg?&MsgID=3080011908135131569&skey=%40crypt_c117402d_53a58f8fbb21978167a3fc7d3be7f8c9'
// const myurl = 'http://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg?&MsgID=3080011908135131569&skey=%40crypt_c117402d_53a58f8fbb21978167a3fc7d3be7f8c9'
url
=
url
.
replace
(
/^https/i
,
'
http
'
)
// use http for better performance
url
=
url
.
replace
(
/^https/i
,
'
http
'
)
// use http for better performance
const
options
=
require
(
'
url
'
).
parse
(
url
)
const
options
=
require
(
'
url
'
).
parse
(
url
)
options
.
headers
=
{
options
.
headers
=
{
...
@@ -56,13 +58,13 @@ function downloadStream(url, cookies) {
...
@@ -56,13 +58,13 @@ function downloadStream(url, cookies) {
,
'
Accept-Language
'
:
'
zh-CN,zh;q=0.8
'
,
'
Accept-Language
'
:
'
zh-CN,zh;q=0.8
'
}
}
options
.
agent
=
http
.
globalAgent
options
.
agent
=
http
.
globalAgent
// 'pgv_pvi=6639183872; pgv_si=s8359147520; webwxuvid=747895d9dac5a25dd3a78175a5e931d879e026cacaf3ac06de0bd5f071470e7182fa36f7f1f0477ae5ee9266f741999a; mm_lang=zh_CN; MM_WX_NOTIFY_STATE=1; MM_WX_SOUND_STATE=1; wxloadtime=1465928826_expired; wxpluginkey=1465901102; wxuin=1211516682; wxsid=zMT7Gb24aTQzB1rA; webwx_data_ticket=gSeBbuhX+0kFdkXbgeQwr6Ck'
// 'pgv_pvi=6639183872; pgv_si=s8359147520; webwxuvid=747895d9dac5a25dd3a78175a5e931d879e026cacaf3ac06de0bd5f071470e7182fa36f7f1f0477ae5ee9266f741999a; mm_lang=zh_CN; MM_WX_NOTIFY_STATE=1; MM_WX_SOUND_STATE=1; wxloadtime=1465928826_expired; wxpluginkey=1465901102; wxuin=1211516682; wxsid=zMT7Gb24aTQzB1rA; webwx_data_ticket=gSeBbuhX+0kFdkXbgeQwr6Ck'
options
.
headers
.
Cookie
=
cookies
.
map
(
c
=>
`
${
c
.
name
}
=
${
c
.
value
}
`
).
join
(
'
;
'
)
options
.
headers
.
Cookie
=
cookies
.
map
(
c
=>
`
${
c
.
name
}
=
${
c
.
value
}
`
).
join
(
'
;
'
)
// log.verbose('WebUtil', 'Cookie: %s', options.headers.Cookie)
// log.verbose('WebUtil', 'Cookie: %s', options.headers.Cookie)
return
new
Promise
((
resolve
,
reject
)
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
req
=
http
.
request
(
options
,
(
res
)
=>
{
const
req
=
http
.
request
(
options
,
(
res
)
=>
{
// console.log(`STATUS: ${res.statusCode}`);
// console.log(`STATUS: ${res.statusCode}`);
// console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
// console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
// res.setEncoding('utf8');
// res.setEncoding('utf8');
...
@@ -75,4 +77,13 @@ function downloadStream(url, cookies) {
...
@@ -75,4 +77,13 @@ function downloadStream(url, cookies) {
req
.
end
()
req
.
end
()
})
})
}
}
module
.
exports
=
WebUtil
// credit - http://stackoverflow.com/a/2117523/1123955
function
guid
()
{
return
'
xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
'
.
replace
(
/
[
xy
]
/g
,
function
(
c
)
{
var
r
=
Math
.
random
()
*
16
|
0
,
v
=
c
==
'
x
'
?
r
:
(
r
&
0x3
|
0x8
)
return
v
.
toString
(
16
)
})
}
module
.
exports
=
Util
src/wechaty.js
浏览文件 @
f07a3c62
...
@@ -12,6 +12,7 @@ const EventEmitter = require('events')
...
@@ -12,6 +12,7 @@ const EventEmitter = require('events')
const
co
=
require
(
'
co
'
)
const
co
=
require
(
'
co
'
)
const
log
=
require
(
'
./npmlog-env
'
)
const
log
=
require
(
'
./npmlog-env
'
)
const
Util
=
require
(
'
./util
'
)
class
Wechaty
extends
EventEmitter
{
class
Wechaty
extends
EventEmitter
{
...
@@ -36,6 +37,8 @@ class Wechaty extends EventEmitter {
...
@@ -36,6 +37,8 @@ class Wechaty extends EventEmitter {
this
.
npmVersion
=
require
(
'
../package.json
'
).
version
this
.
npmVersion
=
require
(
'
../package.json
'
).
version
this
.
uuid
=
Util
.
guid
()
this
.
inited
=
false
this
.
inited
=
false
}
}
...
@@ -226,6 +229,7 @@ class Wechaty extends EventEmitter {
...
@@ -226,6 +229,7 @@ class Wechaty extends EventEmitter {
getPort
(
okPort
=>
resolve
(
okPort
))
getPort
(
okPort
=>
resolve
(
okPort
))
})
})
}
}
}
}
const
Message
=
require
(
'
./message
'
)
const
Message
=
require
(
'
./message
'
)
...
...
test/
web-
util.spec.js
→
test/util.spec.js
浏览文件 @
f07a3c62
...
@@ -3,7 +3,7 @@
...
@@ -3,7 +3,7 @@
const
test
=
require
(
'
tape
'
)
const
test
=
require
(
'
tape
'
)
const
log
=
require
(
'
../src/npmlog-env
'
)
const
log
=
require
(
'
../src/npmlog-env
'
)
const
webUtil
=
require
(
'
../src/web-
util
'
)
const
Util
=
require
(
'
../src/
util
'
)
test
(
'
Html smoking test
'
,
t
=>
{
test
(
'
Html smoking test
'
,
t
=>
{
const
HTML_BEFORE_STRIP
=
'
Outer<html>Inner</html>
'
const
HTML_BEFORE_STRIP
=
'
Outer<html>Inner</html>
'
...
@@ -24,17 +24,17 @@ test('Html smoking test', t => {
...
@@ -24,17 +24,17 @@ test('Html smoking test', t => {
const
PLAIN_BEFORE
=
'
&<html>&</html>&<img class="emoji emoji1f4a4" text="[流汗]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />
'
const
PLAIN_BEFORE
=
'
&<html>&</html>&<img class="emoji emoji1f4a4" text="[流汗]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />
'
const
PLAIN_AFTER
=
'
&&&[流汗]
'
const
PLAIN_AFTER
=
'
&&&[流汗]
'
const
strippedHtml
=
web
Util
.
stripHtml
(
HTML_BEFORE_STRIP
)
const
strippedHtml
=
Util
.
stripHtml
(
HTML_BEFORE_STRIP
)
t
.
equal
(
strippedHtml
,
HTML_AFTER_STRIP
,
'
should strip html as expected
'
)
t
.
equal
(
strippedHtml
,
HTML_AFTER_STRIP
,
'
should strip html as expected
'
)
const
unescapedHtml
=
web
Util
.
unescapeHtml
(
HTML_BEFORE_UNESCAPE
)
const
unescapedHtml
=
Util
.
unescapeHtml
(
HTML_BEFORE_UNESCAPE
)
t
.
equal
(
unescapedHtml
,
HTML_AFTER_UNESCAPE
,
'
should unescape html as expected
'
)
t
.
equal
(
unescapedHtml
,
HTML_AFTER_UNESCAPE
,
'
should unescape html as expected
'
)
for
(
let
i
=
0
;
i
<
EMOJI_BEFORE_DIGEST
.
length
;
i
++
)
{
for
(
let
i
=
0
;
i
<
EMOJI_BEFORE_DIGEST
.
length
;
i
++
)
{
const
emojiDigest
=
web
Util
.
digestEmoji
(
EMOJI_BEFORE_DIGEST
[
i
])
const
emojiDigest
=
Util
.
digestEmoji
(
EMOJI_BEFORE_DIGEST
[
i
])
t
.
equal
(
emojiDigest
,
EMOJI_AFTER_DIGEST
[
i
],
'
should digest emoji string
'
+
i
+
'
as expected
'
)
t
.
equal
(
emojiDigest
,
EMOJI_AFTER_DIGEST
[
i
],
'
should digest emoji string
'
+
i
+
'
as expected
'
)
}
}
const
plainText
=
web
Util
.
plainText
(
PLAIN_BEFORE
)
const
plainText
=
Util
.
plainText
(
PLAIN_BEFORE
)
t
.
equal
(
plainText
,
PLAIN_AFTER
,
'
should convert plain text as expected
'
)
t
.
equal
(
plainText
,
PLAIN_AFTER
,
'
should convert plain text as expected
'
)
t
.
end
()
t
.
end
()
...
@@ -57,7 +57,7 @@ test('Media download smoking test', t => {
...
@@ -57,7 +57,7 @@ test('Media download smoking test', t => {
})
})
server
.
listen
(
8000
)
server
.
listen
(
8000
)
web
Util
.
downloadStream
(
'
http://127.0.0.1:8000/ding
'
,
[{
name
:
'
life
'
,
value
:
42
}])
Util
.
downloadStream
(
'
http://127.0.0.1:8000/ding
'
,
[{
name
:
'
life
'
,
value
:
42
}])
.
then
(
s
=>
{
.
then
(
s
=>
{
s
.
on
(
'
data
'
,
(
chunk
)
=>
{
s
.
on
(
'
data
'
,
(
chunk
)
=>
{
// console.log(`BODY: ${chunk}`)
// console.log(`BODY: ${chunk}`)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录