galerts 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -1
- data/lib/galerts.rb +18 -6
- data/lib/galerts/alert.rb +19 -6
- data/lib/galerts/manager.rb +56 -35
- data/lib/galerts/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd102625ff1208036ed7fe580b2974eb990e4df9
|
4
|
+
data.tar.gz: 2814bb92803b40ad5077457320de8a419a0922e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 719e10254d3fa63e0fa1e2abd56c9f814c1198dc7b08d8ed5f119d23f1a90a1daf0a4ede2f7a4ac20817197f591d382a8cb51a778ab4d9a995429e6f11a8a4b8
|
7
|
+
data.tar.gz: b576338dbc8a9ad8b0ff0786d1b2ad069c726804ac1e0c0ede4c54bcab7c41e1793528c231a32344475df9f7808923abf76f66443fe9879af6114bbc7b435297
|
data/README.md
CHANGED
@@ -6,7 +6,10 @@ alerts webpage.
|
|
6
6
|
## Features
|
7
7
|
|
8
8
|
- List all alerts associated with account.
|
9
|
-
- Create new
|
9
|
+
- Create new alert for any google domain.
|
10
|
+
- Delete an alert
|
11
|
+
- Find alerts by query, id, data_id, feed_url, domain, language, how_many,
|
12
|
+
region, delivery
|
10
13
|
|
11
14
|
## Example
|
12
15
|
|
@@ -30,6 +33,14 @@ manager.create("my keywords", {
|
|
30
33
|
:delivery => Galerts::RSS
|
31
34
|
}
|
32
35
|
)
|
36
|
+
|
37
|
+
# Delete an alert with alerts data_id
|
38
|
+
manager.delete("alerts data_id")
|
39
|
+
|
40
|
+
# Find examples
|
41
|
+
manager.find_by_query("keyword")
|
42
|
+
manager.find_by_delivery(Galerts::RSS)
|
43
|
+
manager.find({query: "keyword", delivery: Galerts::RSS})
|
33
44
|
```
|
34
45
|
|
35
46
|
## Contribute
|
data/lib/galerts.rb
CHANGED
@@ -1,9 +1,7 @@
|
|
1
|
-
require File.expand_path('../galerts/alert', __FILE__)
|
2
|
-
require File.expand_path('../galerts/manager', __FILE__)
|
3
|
-
|
4
1
|
module Galerts
|
5
2
|
# URLs
|
6
3
|
CREATE_ALERT_URL = 'https://www.google.com/alerts/create?'
|
4
|
+
DELETE_ALERT_URL = 'https://www.google.com/alerts/delete?'
|
7
5
|
GOOGLE_LOGIN_URL = 'https://accounts.google.com/ServiceLogin?'
|
8
6
|
ALERTS_URL = 'https://www.google.com/alerts'
|
9
7
|
LOGIN_URL = "#{GOOGLE_LOGIN_URL}service=alerts&continue=#{ALERTS_URL}"
|
@@ -11,7 +9,7 @@ module Galerts
|
|
11
9
|
# Google Return HTML Definitions
|
12
10
|
ALERT_EXIST = "[null,11,null,\"\"]"
|
13
11
|
ALERT_SOMETHING_WENT_WRONG = "[null,7,null,\"\"]"
|
14
|
-
|
12
|
+
ALERT_NOT_EXIST = "[null,5,null,\"\"]"
|
15
13
|
# Google Value
|
16
14
|
BEST_RESULTS = 'Only the best results'
|
17
15
|
ALL_RESULTS = 'All results'
|
@@ -24,13 +22,21 @@ module Galerts
|
|
24
22
|
RSS = 'rss'
|
25
23
|
EMAIL = 'email'
|
26
24
|
|
27
|
-
DELIVERY_TYPES =
|
25
|
+
DELIVERY_TYPES = {
|
26
|
+
EMAIL => 1,
|
27
|
+
RSS => 2
|
28
|
+
}
|
29
|
+
|
28
30
|
|
29
31
|
RT = 'As it happens'
|
30
32
|
DAILY = 'Once a day'
|
31
33
|
WEEKLY = 'Once a week'
|
32
34
|
|
33
|
-
FREQ_TYPES =
|
35
|
+
FREQ_TYPES = {
|
36
|
+
RT => 1,
|
37
|
+
DAILY => 2,
|
38
|
+
WEEKLY => 3
|
39
|
+
}
|
34
40
|
|
35
41
|
|
36
42
|
BLOGS = 'Blogs'
|
@@ -49,3 +55,9 @@ module Galerts
|
|
49
55
|
DISCUSSIONS => 7
|
50
56
|
}
|
51
57
|
end
|
58
|
+
|
59
|
+
require File.expand_path('../galerts/alert', __FILE__)
|
60
|
+
require File.expand_path('../galerts/manager', __FILE__)
|
61
|
+
require File.expand_path('../galerts/version', __FILE__)
|
62
|
+
|
63
|
+
|
data/lib/galerts/alert.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
module Galerts
|
2
2
|
class Alert
|
3
|
-
attr_accessor :
|
4
|
-
def initialize(
|
3
|
+
attr_accessor :query, :id, :data_id, :domain, :frequency, :sources, :language, :how_many, :region, :delivery, :feed_url
|
4
|
+
def initialize(query, options = {})
|
5
5
|
|
6
6
|
default_options = {
|
7
7
|
id: 0,
|
8
8
|
data_id: 0,
|
9
9
|
domain: 'com',
|
10
|
-
frequency:
|
10
|
+
frequency: RT,
|
11
11
|
sources: '',
|
12
12
|
language: 'tr',
|
13
|
-
how_many:
|
13
|
+
how_many: ALL_RESULTS,
|
14
14
|
region: 'TR',
|
15
|
-
delivery:
|
15
|
+
delivery: RSS,
|
16
16
|
feed_url: nil
|
17
17
|
}
|
18
18
|
|
@@ -20,7 +20,20 @@ module Galerts
|
|
20
20
|
options[key] ||= value
|
21
21
|
end
|
22
22
|
|
23
|
-
|
23
|
+
# check options type
|
24
|
+
raise "Unknown alert how many" unless HOW_MANY_TYPES.has_key?(options[:how_many])
|
25
|
+
raise "Unknown alert delivery type" unless DELIVERY_TYPES.has_key?(options[:delivery])
|
26
|
+
raise "Unknown alert frequency type" unless FREQ_TYPES.has_key?(options[:frequency])
|
27
|
+
|
28
|
+
if options[:sources].kind_of?(Array)
|
29
|
+
options[:sources].collect do |source|
|
30
|
+
raise "Unknown alert source" unless SOURCES_TYPES.invert.has_key?(source)
|
31
|
+
end
|
32
|
+
elsif !options[:sources].empty?
|
33
|
+
raise "Unknown alert source"
|
34
|
+
end
|
35
|
+
|
36
|
+
@query = query
|
24
37
|
@id = options[:id]
|
25
38
|
@data_id = options[:data_id]
|
26
39
|
@domain = options[:domain]
|
data/lib/galerts/manager.rb
CHANGED
@@ -49,58 +49,59 @@ module Galerts
|
|
49
49
|
def alerts
|
50
50
|
result = []
|
51
51
|
contents = alerts_page.css('div#gb-main div.main-page script').text
|
52
|
-
|
53
52
|
contents = contents.gsub('null', 'nil')
|
54
|
-
|
55
53
|
contents = eval(contents.gsub("window.STATE = ", ""))
|
56
54
|
|
57
|
-
# only 'id, search_query, feed_url, data_id' variables have true value,
|
58
|
-
# other variables have default Alert class values.
|
59
55
|
contents[1][1].each do |alert|
|
60
56
|
result << Alert.new(alert[2][3][1], {
|
61
57
|
id: alert[2].last.last.last,
|
62
|
-
|
58
|
+
query: alert[2][3][1],
|
63
59
|
feed_url: "/alerts/feeds/#{alert.last}/#{alert[2].last.last.last}",
|
64
60
|
data_id: alert[1],
|
65
|
-
domain:
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
delivery:
|
61
|
+
domain: alert[2][3][2],
|
62
|
+
language: alert[2][3][3][1],
|
63
|
+
region: alert[2][3][3][2],
|
64
|
+
frequency: FREQ_TYPES.invert[alert[2][6][0][4]],
|
65
|
+
sources: SOURCES_TYPES.invert[alert[2][4]],
|
66
|
+
how_many: HOW_MANY_TYPES.invert[alert[2][5]],
|
67
|
+
delivery: DELIVERY_TYPES.invert[alert[2][6][0][1]]
|
72
68
|
}
|
73
69
|
)
|
74
70
|
end
|
75
71
|
result
|
76
72
|
end
|
77
73
|
|
78
|
-
def
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
74
|
+
def find(attrs = {})
|
75
|
+
alerts.select{|a| attrs.keys.inject(true) {|memo,k| memo = memo && attrs[k] == a.send(k) }}
|
76
|
+
end
|
77
|
+
|
78
|
+
# Metaprogramming for find_by commands
|
79
|
+
variables = Galerts::Alert.new("").instance_variables.map {|m| m.to_s.delete('@')}
|
80
|
+
variables.each do |variable|
|
81
|
+
define_method("find_by_#{variable}") do |argument|
|
82
|
+
find({variable.to_sym => argument})
|
83
|
+
end
|
84
|
+
end
|
83
85
|
|
86
|
+
def build_create_params(alert)
|
84
87
|
# set delivery and frequency parameters
|
85
|
-
if
|
86
|
-
if
|
87
|
-
delivery_and_frequency = @email
|
88
|
-
elsif
|
89
|
-
delivery_and_frequency = @email
|
90
|
-
elsif
|
91
|
-
delivery_and_frequency = "
|
88
|
+
if alert.delivery == EMAIL
|
89
|
+
if alert.frequency == DAILY
|
90
|
+
delivery_and_frequency = "#{DELIVERY_TYPES[EMAIL]},\"#{@email}\",[null,null,11],#{FREQ_TYPES[DAILY]}"
|
91
|
+
elsif alert.frequency == WEEKLY
|
92
|
+
delivery_and_frequency = "#{DELIVERY_TYPES[EMAIL]},\"#{@email}\",[null,null,11,1],#{FREQ_TYPES[WEEKLY]}"
|
93
|
+
elsif alert.frequency == RT
|
94
|
+
delivery_and_frequency = "#{DELIVERY_TYPES[EMAIL]},\"#{@email}\",[],#{FREQ_TYPES[RT]}"
|
92
95
|
end
|
93
|
-
elsif
|
94
|
-
delivery_and_frequency = "
|
96
|
+
elsif alert.delivery == RSS
|
97
|
+
delivery_and_frequency = "#{DELIVERY_TYPES[RSS]},\"\",[],#{FREQ_TYPES[RT]}"
|
95
98
|
end
|
96
99
|
|
97
|
-
|
98
|
-
|
99
|
-
if options[:sources].nil?
|
100
|
+
if alert.sources.empty?
|
100
101
|
sources_text = 'null'
|
101
102
|
else
|
102
103
|
sources_text = "["
|
103
|
-
|
104
|
+
alert.sources.collect do |source|
|
104
105
|
raise "Unknown alert source" unless SOURCES_TYPES.has_key?(source)
|
105
106
|
sources_text += SOURCES_TYPES[source].to_s + ','
|
106
107
|
end
|
@@ -109,15 +110,17 @@ module Galerts
|
|
109
110
|
|
110
111
|
# TODO: need more readable
|
111
112
|
params = {
|
112
|
-
'params' => "[null,[null,null,null,[null,\"#{
|
113
|
+
'params' => "[null,[null,null,null,[null,\"#{alert.query}\",\"#{alert.domain}\",[null,\"#{alert.language}\",\"#{alert.region}\"],null,null,null,#{alert.region == "" ? 1 : 0},1],#{sources_text},#{HOW_MANY_TYPES[alert.how_many]},[[null,#{delivery_and_frequency},\"#{alert.language + '-' + alert.region.upcase}\",null,null,null,null,null,'0']]]]"
|
113
114
|
}
|
114
115
|
|
115
116
|
params = URI.encode_www_form(params)
|
116
117
|
end
|
117
118
|
|
118
|
-
def create(
|
119
|
+
def create(query, options = {})
|
120
|
+
alert = Alert.new(query, options)
|
121
|
+
|
119
122
|
x = alerts_page.css('div#gb-main div.main-page script').text.split(',').last[1..-4]
|
120
|
-
response = @agent.post("#{CREATE_ALERT_URL}x=#{x}", build_create_params(
|
123
|
+
response = @agent.post("#{CREATE_ALERT_URL}x=#{x}", build_create_params(alert), {'Content-Type' => 'application/x-www-form-urlencoded'})
|
121
124
|
|
122
125
|
if response.body == ALERT_EXIST
|
123
126
|
raise "Alert exist!"
|
@@ -127,8 +130,6 @@ module Galerts
|
|
127
130
|
response_body = response.body.gsub('null', 'nil')
|
128
131
|
created_alert = Nokogiri::HTML(eval(response_body)[4][0][2], nil, 'utf-8')
|
129
132
|
|
130
|
-
alert = Alert.new(search_query, options)
|
131
|
-
|
132
133
|
if options[:delivery] == RSS
|
133
134
|
alert.id = created_alert.css('a')[0]['href'].split('/').last if options[:delivery] == RSS
|
134
135
|
alert.feed_url = created_alert.css('a')[0]['href']
|
@@ -137,5 +138,25 @@ module Galerts
|
|
137
138
|
alert
|
138
139
|
end
|
139
140
|
end
|
141
|
+
|
142
|
+
def build_delete_params(data_id)
|
143
|
+
params = {
|
144
|
+
'params' => "[null,\"#{data_id}\"]"
|
145
|
+
}
|
146
|
+
|
147
|
+
params = URI.encode_www_form(params)
|
148
|
+
end
|
149
|
+
|
150
|
+
def delete(data_id)
|
151
|
+
x = alerts_page.css('div#gb-main div.main-page script').text.split(',').last[1..-4]
|
152
|
+
response = @agent.post("#{DELETE_ALERT_URL}x=#{x}", build_delete_params(data_id), {'Content-Type' => 'application/x-www-form-urlencoded'})
|
153
|
+
|
154
|
+
if response.body == ALERT_NOT_EXIST
|
155
|
+
raise "Alert not exist!"
|
156
|
+
elsif response.body == ALERT_SOMETHING_WENT_WRONG
|
157
|
+
raise "Something went wrong!" # internal error, html changed maybe
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
140
161
|
end
|
141
162
|
end
|
data/lib/galerts/version.rb
CHANGED