1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Daily program to fetch and parse the Web site, update the database and email subscribers
28 # Return a `String` without any HTML tags (such as `<br />`) from `recv`
29 fun strip_tags
: String
36 var at
= str
.index_of_from
('<', from
)
37 if at
== -1 then break
39 new_str
+= str
.substring
(from
, at-from
)
41 at
= str
.index_of_from
('>', at
)
50 # Return an `Array` of the non-empty lines in `self`
52 # assert ["a", "asdf", "", " ", " ", "b"].join("\n").to_clean_lines == ["a", "asdf", "b"]
53 fun to_clean_lines
: Array[String]
55 var orig_lines
= split_with
("\n")
56 var new_lines
= new Array[String]
58 for line
in orig_lines
do
62 if line
== " " then continue
63 if line
.is_empty
then continue
65 new_lines
.add line
.to_s
72 # Main logic of the program to be executed daily
74 # The street on which is the Benelux
77 # The url of this precise Benelux
78 var url
= "www.brasseriebenelux.com/{street}" is lazy
80 # Path to the database
81 var db_path
= "benitlux_{street}.db" is lazy
83 # Where to save the sample email
84 var sample_email_path
= "benitlux_{street}.email" is lazy
86 # Execute the main program logic
87 fun run
(send_emails
: Bool)
90 var body
= download_html_page
92 if opts
.verbose
.value
> 1 then
97 # Parse the Web page and get the available beers
98 var beers
= parse_beers_from_html
(body
)
100 if opts
.verbose
.value
> 0 then
105 var db
= new DB.open
(db_path
)
107 # Update the database with the beers of the day
108 db
.insert_beers_of_the_day beers
110 # Query the beer-related events of today
111 var beer_events
= db
.beer_events_today
113 # Generate the email title and content, store them in attributes
114 generate_email
(beer_events
)
116 # Save as sample email to file
117 var f
= new FileWriter.open
(sample_email_path
)
118 f
.write email_title
+ "\n"
119 for line
in email_content
do f
.write line
+ "\n"
122 # Set the email if desired
124 var subs
= db
.subscribers
125 if opts
.verbose
.value
> 0 then
126 print
" # Subscribers"
135 # Fetch the Web page at `url`
136 fun download_html_page
: String
138 var request
= new CurlHTTPRequest(url
)
139 var response
= request
.execute
141 if response
isa CurlResponseSuccess then
142 var body
= response
.body_str
144 else if response
isa CurlResponseFailed then
145 print
"Failed downloading URL '{url}' with: {response.error_msg} ({response.error_code})"
151 # Extract the beers of the day information from the HTML if `body`
152 fun parse_beers_from_html
(body
: String): HashSet[Beer]
154 # Parts of the HTML page expected to encapsulate the interesting section
155 var header
= "<h1>Bières<br /></h1>"
156 var ender
= "</div></div></div>"
158 var match
= body
.search
(header
)
159 assert match
!= null else print body
160 var start
= match
.after
162 match
= body
.search_from
(ender
, start
)
164 var finish
= match
.from
166 var of_interest
= body
.substring
(start
, finish-start
)
167 var lines
= of_interest
.strip_tags
.to_clean_lines
169 if opts
.verbose
.value
> 0 then
174 var beers
= new HashSet[Beer]
176 var parts
= line
.split
(" - ")
177 if parts
.length
>= 2 then
178 beers
.add
new Beer(parts
[0].trim
, parts
[1].trim
)
184 # Content lines of the email
185 var email_content
: Array[String] is noautoinit
188 var email_title
: String is noautoinit
190 # Generate email and fill the attributes `email_content` and `email_title`
191 fun generate_email
(beer_events
: BeerEvents)
193 email_title
= "Benitlux {street.capitalized}{beer_events.to_email_title}"
194 email_content
= beer_events
.to_email_content
197 # Send the email to all the addresses in `subs`
198 fun send_emails_to
(subs
: Array[String])
201 var unsub_link
= "http://benitlux.xymus.net/?unsub=&email={email}"
203 {{{email_content.join("<br />\n")}}}
205 To unsubscribe, go to <a href="{{{unsub_link}}}">{{{unsub_link}}}</a>
208 var mail
= new Mail("Benitlux <benitlux@xymus.net>", email_title
, content
)
210 mail
.header
["Content-Type"] = "text/html; charset=\"UTF-8\
""
211 mail
.header
["List-Unsubscribe"] = unsub_link
212 mail
.header
["Precedence"] = "bulk"
213 mail
.encrypt_with_base64
220 redef class OptionContext
221 # Shall we mail the mailing list?
222 var send_emails
= new OptionBool("Send emails to subscribers", "-e", "--email")
224 # Display more debug messages
225 var verbose
= new OptionCount("Display extra debug messages", "-v")
227 # Print the usage message
228 var help
= new OptionBool("Print this help message", "-h", "--help")
230 redef init do add_option
(send_emails
, verbose
, help
)
234 # Command line options
235 var opts
= new OptionContext
238 # Avoid executing when running tests
239 if "NIT_TESTING".environ
== "true" then exit
0
242 if not opts
.errors
.is_empty
or opts
.help
.value
== true then
243 print opts
.errors
.join
("\n")
244 print
"Usage: benitlux_daily [Options]"
249 var ben
= new Benitlux("sherbrooke")
250 ben
.run
(opts
.send_emails
.value
)
252 # The parsing logic for the wellington location is active (to gather data)
253 # but the web interface do not allow to subscribe to its mailing list.
255 # TODO revamp mailing list Web interface
256 ben
= new Benitlux("wellington")
257 ben
.run
(opts
.send_emails
.value
)