my_utils 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,31 @@
1
+ require "open-uri"
2
+ require "nokogiri"
3
+ require "nokogiri"
4
+
5
+ module Douban
6
+ def self.get_book title
7
+ target = URI.encode("http://book.douban.com/subject_search?search_text=#{title}")
8
+ open(target) do |http|
9
+ doc = Nokogiri::HTML(http.read)
10
+ book_list = doc.xpath('//li[@class="subject-item"]')
11
+ book_list.each_with_index do |bk, idx|
12
+ rate = bk.xpath('//div/span[@class="rating_nums"]')
13
+ puts "rate: #{rate}"
14
+ #puts "item #{idx}: #{book}"
15
+ #puts "\n"
16
+ =begin
17
+ rate = book.xpath('//span[@class="rating_nums"]')
18
+ rate_pl = book.xpath('//span[@class="pl"]')
19
+
20
+ puts "rate #{rate}"
21
+ puts ""
22
+ =end
23
+
24
+ #puts "rate_pl #{rate_pl.content}"
25
+ end
26
+ #puts http.read
27
+ end
28
+ end
29
+ end
30
+
31
+ Douban.get_book "C++ primer"
@@ -0,0 +1,95 @@
1
+ #encoding:utf-8
2
+ require './settings'
3
+ require './domain'
4
+ require 'digest/md5'
5
+ require 'httpclient'
6
+ require 'json'
7
+ require 'my_utils'
8
+
9
+
10
+ def gen_sign(params)
11
+ Digest::MD5.hexdigest(Settings.app_secret + params.sort_by { |k,v| k.to_s }.flatten.join + Settings.app_secret).upcase
12
+ end
13
+
14
+ def comment(info)
15
+ ut = UserToken.where(:taobao_user_id => info['user_id'])[0]
16
+ =begin
17
+ comment:
18
+ seller_id
19
+ tid
20
+ comment
21
+ =end
22
+ resp = TopUtil.get_top_response(
23
+ :tid => info['tid'],
24
+ :result => "good",
25
+ :role => "seller",
26
+ :session => ut.access_token,
27
+ :method => 'taobao.traderate.add',
28
+ :content => "很好啊,你妹啊")
29
+ $local_log.info(resp)
30
+ end
31
+
32
+
33
+ def taobao_stream
34
+ params={
35
+ "sign_method" => "md5",
36
+ "app_key" => Settings.app_key.to_s,
37
+ "timestamp" => Time.now.strftime("%Y-%m-%d %H:%M:%S")
38
+ }.sort_by{ |k| k.to_s }
39
+
40
+ post_params = {}
41
+ params.map { |key, value| post_params[key] = value }
42
+ post_params["sign"] = gen_sign(post_params)
43
+ c = HTTPClient.new
44
+ conn = c.post_async(Settings.stream_url, post_params)
45
+ res = conn.pop
46
+ msg = ""
47
+ while str = res.content.read(1)
48
+ unless(str == "\n")
49
+ msg = msg+str
50
+ next
51
+ end
52
+ result_json = JSON.parse(msg)
53
+
54
+ #TODO处理消息的代码,这里需要改成异步的,消息量大的时候,会产生消息积压
55
+ code=result_json["packet"]["code"]
56
+ case code
57
+ when 200
58
+ puts "服务器连接成功,连接的服务器为#{result_json["packet"]["msg"]}"
59
+ $msg_log.info("服务器连接成功,连接的服务器为#{result_json["packet"]["msg"]}")
60
+ when 201
61
+ puts "接收到心跳包"
62
+ $hb_log.info("接收到心跳包")
63
+ when 202
64
+ puts "有业务消息推送到,消息为#{result_json["packet"]["msg"]}"
65
+ $msg_log.info("有业务消息推送到,消息为#{result_json["packet"]["msg"]}")
66
+ info = result_json['packet']['msg']['notify_trade']
67
+ comment(info) if info != nil and info['status'] == 'TradeRated'
68
+ # TODO
69
+ #TaobaoTrade.stream_trade_msg result_json["packet"]["msg"]
70
+ when 203
71
+ puts "有消息丢失,丢失的消息#{result_json["packet"]["msg"]}"
72
+ $msg_log.info("有消息丢失,丢失的消息#{result_json["packet"]["msg"]}")
73
+ when 101
74
+ puts "需要重新连接,已到达最长的服务器连接时间"
75
+ $msg_log.info("需要重新连接,已到达最长的服务器连接时间")
76
+ when 102
77
+ puts "服务器正在升级,需要#{result_json["packet"]["msg"]}秒后进行重连"
78
+ $msg_log.info("服务器正在升级,需要#{result_json["packet"]["msg"]}秒后进行重连")
79
+ when 103
80
+ puts "服务器主动断开连接,需要#{result_json["packet"]["msg"]}秒后进行重连"
81
+ $msg_log.info("服务器主动断开连接,需要#{result_json["packet"]["msg"]}秒后进行重连")
82
+ when 104
83
+ puts "发起了新连接,服务器将要断开此连接,以新连接处理消息请求"
84
+ $msg_log.info("发起了新连接,服务器将要断开此连接,以新连接处理消息请求")
85
+ when 105
86
+ puts "产生大量的消息积压,服务器需要断开连接"
87
+ else
88
+ puts "服务器返回了未知的应答码:code=#{result_json["packet"]["code"]},msg=#{result_json["packet"]["msg"]}"
89
+ $msg_log.info("服务器返回了未知的应答码:code=#{result_json["packet"]["code"]},msg=#{result_json["packet"]["msg"]}")
90
+ end
91
+ msg=""
92
+ end
93
+ end
94
+
95
+ taobao_stream
@@ -0,0 +1,45 @@
1
+ module Strutils
2
+
3
+ def self.reg_iter text, s, e
4
+ rstart = 0; rend = 0
5
+ rst_arry = []
6
+
7
+ while true
8
+ rstart = text.index(s, rend)
9
+ break if rstart == nil
10
+
11
+ rend = text.index(e, rstart)
12
+ len = text.match(e, rstart)[0].length
13
+ rend = rend + len - 1
14
+ rst_arry.push(text[rstart..rend])
15
+ rstart = rend
16
+ end
17
+
18
+ rst_arry
19
+ end
20
+
21
+ def self.strcmp a, b
22
+ return 0 if a == nil || b == nil
23
+
24
+ a = a.strip; b = b.strip
25
+
26
+ return 0 if a == "" || b == ""
27
+
28
+ len = a.length > b.length ? a.length : b.length
29
+ dp = Array.new(a.length) {Array.new(b.length, 0)}
30
+
31
+ for i in 0..a.length-1
32
+ for j in 0..b.length-1
33
+ (dp[i][j] = (a[0] == b[0]? 1 : 0); next) if i == 0 and j == 0
34
+ (dp[i][j] = (dp[i][j-1] == 1 || a[0] == b[j]? 1 : 0); next) if i == 0
35
+ (dp[i][j] = (dp[i-1][j] == 1 || a[i] == b[0]? 1 : 0); next) if j == 0
36
+
37
+ dp[i][j] = (dp[i-1][j-1] + (a[i] == b[j]? 1 : 0))
38
+ dp[i][j] = dp[i-1][j] if dp[i][j] < dp[i-1][j]
39
+ dp[i][j] = dp[i][j-1] if dp[i][j] < dp[i][j-1]
40
+ end
41
+ end
42
+
43
+ return dp[a.length-1][b.length-1] / (len * 1.0)
44
+ end
45
+ end
@@ -0,0 +1,106 @@
1
+ #encoding:utf-8
2
+
3
+ require 'digest/md5'
4
+ require 'httpclient'
5
+ require 'net/http'
6
+ require 'json'
7
+ require 'uri'
8
+
9
+ module Toputils
10
+
11
+ TOP_URL = 'http://gw.api.taobao.com/router/rest?'
12
+ STREAM_URL = "http://stream.api.taobao.com/stream"
13
+ SB_STREAM_URL = "http://stream.api.tbsandbox.com/stream"
14
+
15
+ def self.top_get(app_key, app_secret, opts = {})
16
+ params = {
17
+ app_key: app_key,
18
+ format: 'json',
19
+ v: '2.0',
20
+ timestamp: (Time.now).strftime("%Y-%m-%d %H:%M:%S"),
21
+ sign_method: 'md5'
22
+ }
23
+ params.merge!(opts)
24
+
25
+ sign_str = app_secret + params.sort.flatten.join + app_secret
26
+
27
+ params[:sign] = Digest::MD5.hexdigest(sign_str).upcase
28
+
29
+ params_str = ''
30
+ params.each {
31
+ |k, v| params_str << k.to_s<<'='<<URI.escape(v.to_s).to_s<<'&'
32
+ }
33
+ target = TOP_URL + params_str
34
+
35
+ res = Net::HTTP.get_response(URI.parse(target))
36
+ res.body
37
+ end
38
+
39
+ def self.top_stream(options = {})
40
+ params={
41
+ "sign_method" => "md5",
42
+ "app_key" => options[:app_key],
43
+ "timestamp" => Time.now.strftime("%Y-%m-%d %H:%M:%S")
44
+ }.sort_by{ |k| k.to_s }
45
+
46
+ post_params = {}
47
+ params.map { |key, value| post_params[key] = value }
48
+ post_params["sign"] = Digest::MD5.hexdigest(options[:app_secret] + params.sort_by { |k,v| k.to_s }.flatten.join + options[:app_secret]).upcase
49
+ c = HTTPClient.new
50
+
51
+ conn = c.post_async(options[:app_secret].start_with?("sandbox")? SB_STREAM_URL: STREAM_URL, post_params)
52
+ res = conn.pop
53
+ msg = ""
54
+
55
+ while str = res.content.read(1)
56
+ unless(str == "\n")
57
+ msg = msg+str
58
+ next
59
+ end
60
+ result_json = JSON.parse(msg)
61
+
62
+ #TODO处理消息的代码,这里需要改成异步的,消息量大的时候,会产生消息积压
63
+ code=result_json["packet"]["code"]
64
+ case code
65
+ when 200
66
+ puts "服务器连接成功,连接的服务器为#{result_json["packet"]["msg"]}"
67
+ #$msg_log.info("服务器连接成功,连接的服务器为#{result_json["packet"]["msg"]}")
68
+ when 201
69
+ puts "接收到心跳包"
70
+ #$hb_log.info("接收到心跳包")
71
+ when 202
72
+ puts "有业务消息推送到,消息为#{result_json["packet"]["msg"]}"
73
+ #$msg_log.info("有业务消息推送到,消息为#{result_json["packet"]["msg"]}")
74
+ info = result_json['packet']['msg']['notify_trade']
75
+ yield info
76
+ #comment(info) if info != nil and info['status'] == 'TradeRated'
77
+ # TODO
78
+ #TaobaoTrade.stream_trade_msg result_json["packet"]["msg"]
79
+ when 203
80
+ puts "有消息丢失,丢失的消息#{result_json["packet"]["msg"]}"
81
+ #$msg_log.info("有消息丢失,丢失的消息#{result_json["packet"]["msg"]}")
82
+ when 101
83
+ puts "需要重新连接,已到达最长的服务器连接时间"
84
+ #$msg_log.info("需要重新连接,已到达最长的服务器连接时间")
85
+ when 102
86
+ puts "服务器正在升级,需要#{result_json["packet"]["msg"]}秒后进行重连"
87
+ #$msg_log.info("服务器正在升级,需要#{result_json["packet"]["msg"]}秒后进行重连")
88
+ when 103
89
+ puts "服务器主动断开连接,需要#{result_json["packet"]["msg"]}秒后进行重连"
90
+ #$msg_log.info("服务器主动断开连接,需要#{result_json["packet"]["msg"]}秒后进行重连")
91
+ when 104
92
+ puts "发起了新连接,服务器将要断开此连接,以新连接处理消息请求"
93
+ #$msg_log.info("发起了新连接,服务器将要断开此连接,以新连接处理消息请求")
94
+ when 105
95
+ puts "产生大量的消息积压,服务器需要断开连接"
96
+ else
97
+ puts "服务器返回了未知的应答码:code=#{result_json["packet"]["code"]},msg=#{result_json["packet"]["msg"]}"
98
+ #$msg_log.info("服务器返回了未知的应答码:code=#{result_json["packet"]["code"]},msg=#{result_json["packet"]["msg"]}")
99
+ end
100
+
101
+ yield result_json
102
+
103
+ msg=""
104
+ end
105
+ end
106
+ end
data/lib/my_utils.rb CHANGED
@@ -1,5 +1,2 @@
1
- class MyUtils
2
- def self.hi
3
- puts "hello world!"
4
- end
5
- end
1
+ require 'my_utils/toputils'
2
+ require 'my_utils/strutils'
metadata CHANGED
@@ -5,11 +5,11 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 0
9
- version: 0.0.0
8
+ - 1
9
+ version: 0.0.1
10
10
  platform: ruby
11
11
  authors:
12
- - fzuwwl
12
+ - Nick Quaranto
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
@@ -18,9 +18,8 @@ date: 2010-04-28 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
21
- description: Simple hello world gem
22
- email:
23
- - fzuwwl@gmail.com
21
+ description: A simple hello world gem
22
+ email: nick@quaran.to
24
23
  executables: []
25
24
 
26
25
  extensions: []
@@ -29,6 +28,10 @@ extra_rdoc_files: []
29
28
 
30
29
  files:
31
30
  - lib/my_utils.rb
31
+ - lib/my_utils/douban.rb
32
+ - lib/my_utils/dsysmas.rb
33
+ - lib/my_utils/strutils.rb
34
+ - lib/my_utils/toputils.rb
32
35
  has_rdoc: true
33
36
  homepage: http://rubygems.org/gems/my_utils
34
37
  licenses: []
@@ -60,6 +63,6 @@ rubyforge_project:
60
63
  rubygems_version: 1.3.7.1
61
64
  signing_key:
62
65
  specification_version: 3
63
- summary: Hola
66
+ summary: Hola!
64
67
  test_files: []
65
68