galerts 0.0.1 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ade27844d1c41508bbacd7b030c2ed4654a45e4
4
- data.tar.gz: 6e1f27c679e63c21e1d238158397e2442dea11c6
3
+ metadata.gz: dd102625ff1208036ed7fe580b2974eb990e4df9
4
+ data.tar.gz: 2814bb92803b40ad5077457320de8a419a0922e3
5
5
  SHA512:
6
- metadata.gz: f8b27acbde41ba7bd76d0d34b7404260989d3aced4d22a697057b5ba483646bd17368c46b9f17e7ad55e3545a79f514da39ca4b4f1719258119d7750e24455eb
7
- data.tar.gz: 219056aebeb3d6e3834012f4fd98d7a521df6d7931579c7798fb1782077d81d6dc4631850ffc3c9db7afead1d65dca15cd58922e710e6532551338678d86867a
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 alerts for any google domain.
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 = [RSS, EMAIL]
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 = [RT, DAILY, WEEKLY]
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 :search_query, :id, :data_id, :domain, :frequency, :sources, :language, :how_many, :region, :delivery, :feed_url
4
- def initialize(search_query, options = {})
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: 'rt',
10
+ frequency: RT,
11
11
  sources: '',
12
12
  language: 'tr',
13
- how_many: 'all_results',
13
+ how_many: ALL_RESULTS,
14
14
  region: 'TR',
15
- delivery: 'rss',
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
- @search_query = search_query
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]
@@ -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
- search_query: alert[2][3][1],
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: 'Unknown',
66
- frequency: 'Unknown',
67
- sources: 'Unknown',
68
- language: 'Unknown',
69
- how_many: 'Unknown',
70
- region: 'Unknown',
71
- delivery: 'Unknown'
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 build_create_params(search_query, options)
79
- # check parameters
80
- raise "Unknown alert how_many" unless HOW_MANY_TYPES.has_key?(options[:how_many])
81
- raise "Unknown alert delivery type" unless DELIVERY_TYPES.include?(options[:delivery])
82
- raise "Unknown alert frequency type" unless FREQ_TYPES.include?(options[:frequency])
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 options[:delivery] == EMAIL
86
- if options[:frequency] == DAILY
87
- delivery_and_frequency = @email + ',[null,null,11],2'
88
- elsif options[:frequency] == WEEKLY
89
- delivery_and_frequency = @email + ',[null,null,11,1],3'
90
- elsif options[:frequency] == RT
91
- delivery_and_frequency = "1,\"#{@email}\",[],1"
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 options[:delivery] == RSS
94
- delivery_and_frequency = "2,\"\",[],1"
96
+ elsif alert.delivery == RSS
97
+ delivery_and_frequency = "#{DELIVERY_TYPES[RSS]},\"\",[],#{FREQ_TYPES[RT]}"
95
98
  end
96
99
 
97
- # options[:sources] ? sources = options[:sources] : sources = ""
98
-
99
- if options[:sources].nil?
100
+ if alert.sources.empty?
100
101
  sources_text = 'null'
101
102
  else
102
103
  sources_text = "["
103
- options[:sources].collect do |source|
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,\"#{search_query}\",\"#{options[:domain]}\",[null,\"#{options[:language]}\",\"#{options[:region]}\"],null,null,null,#{options[:region] == "" ? 1 : 0},1],#{sources_text},#{HOW_MANY_TYPES[options[:how_many]]},[[null,#{delivery_and_frequency},\"#{options[:language] + '-' + options[:region].upcase}\",null,null,null,null,null,'0']]]]"
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(search_query, options = {})
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(search_query, options), {'Content-Type' => 'application/x-www-form-urlencoded'})
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
@@ -1,3 +1,3 @@
1
1
  module Galerts
2
- VERSION = '0.0.1'.freeze unless defined?(::Galerts::VERSION)
2
+ VERSION = '0.0.3'.freeze unless defined?(::Galerts::VERSION)
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: galerts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emre Can Yılmaz