contrib/benitlux: add `Beer::id`
[nit.git] / contrib / benitlux / src / benitlux_daily.nit
index 35aa64c..27b8169 100644 (file)
@@ -75,21 +75,13 @@ class Benitlux
        var street: String
 
        # The url of this precise Benelux
-       var url: String
+       var url = "www.brasseriebenelux.com/{street}" is lazy
 
        # Path to the database
-       var db_path: String
+       var db_path = "benitlux_{street}.db" is lazy
 
        # Where to save the sample email
-       var sample_email_path: String
-
-       init(street: String)
-       do
-               self.street = street
-               self.url = "www.brasseriebenelux.com/{street}"
-               self.db_path = "benitlux_{street}.db"
-               self.sample_email_path = "benitlux_{street}.email"
-       end
+       var sample_email_path = "benitlux_{street}.email" is lazy
 
        # Execute the main program logic
        fun run(send_emails: Bool)
@@ -97,9 +89,19 @@ class Benitlux
                # Get the web page
                var body = download_html_page
 
+               if opts.verbose.value > 1 then
+                       print " # Body"
+                       print body
+               end
+
                # Parse the Web page and get the available beers
                var beers = parse_beers_from_html(body)
 
+               if opts.verbose.value > 0 then
+                       print " # Beers"
+                       print beers
+               end
+
                var db = new DB.open(db_path)
 
                # Update the database with the beers of the day
@@ -120,6 +122,10 @@ class Benitlux
                # Set the email if desired
                if send_emails then
                        var subs = db.subscribers
+                       if opts.verbose.value > 0 then
+                               print " # Subscribers"
+                               print subs
+                       end
                        send_emails_to subs
                end
 
@@ -129,14 +135,11 @@ class Benitlux
        # Fetch the Web page at `url`
        fun download_html_page: String
        do
-               var curl = new Curl
-
-               var request = new CurlHTTPRequest(url, curl)
+               var request = new CurlHTTPRequest(url)
                var response = request.execute
 
                if response isa CurlResponseSuccess then
                        var body = response.body_str
-                       curl.destroy
                        return body
                else if response isa CurlResponseFailed then
                        print "Failed downloading URL '{url}' with: {response.error_msg} ({response.error_code})"
@@ -163,26 +166,32 @@ class Benitlux
                var of_interest = body.substring(start, finish-start)
                var lines = of_interest.strip_tags.to_clean_lines
 
+               if opts.verbose.value > 0 then
+                       print " # Lines"
+                       print lines
+               end
+
                var beers = new HashSet[Beer]
                for line in lines do
-                       var parts = line.split(" - ")
+                       var parts = line.split("- ")
                        if parts.length >= 2 then
-                               beers.add new Beer(parts[0].trim, parts[1].trim)
+                               # Let the DB set the id, use 0 temporary
+                               beers.add new Beer(0, parts[0].trim, parts[1].trim)
                        end
                end
                return beers
        end
 
        # Content lines of the email
-       var email_content: Array[String]
+       var email_content: Array[String] is noautoinit
 
        # Title of the email
-       var email_title: String
+       var email_title: String is noautoinit
 
        # Generate email and fill the attributes `email_content` and `email_title`
        fun generate_email(beer_events: BeerEvents)
        do
-               email_title = beer_events.to_email_title
+               email_title = "Benitlux {street.capitalized}{beer_events.to_email_title}"
                email_content = beer_events.to_email_content
        end
 
@@ -200,6 +209,8 @@ To unsubscribe, go to <a href="{{{unsub_link}}}">{{{unsub_link}}}</a>
                        var mail = new Mail("Benitlux <benitlux@xymus.net>", email_title, content)
                        mail.to.add email
                        mail.header["Content-Type"] = "text/html; charset=\"UTF-8\""
+                       mail.header["List-Unsubscribe"] = unsub_link
+                       mail.header["Precedence"] = "bulk"
                        mail.encrypt_with_base64
 
                        mail.send
@@ -211,16 +222,23 @@ redef class OptionContext
        # Shall we mail the mailing list?
        var send_emails = new OptionBool("Send emails to subscribers", "-e", "--email")
 
+       # Display more debug messages
+       var verbose = new OptionCount("Display extra debug messages", "-v")
+
        # Print the usage message
        var help = new OptionBool("Print this help message", "-h", "--help")
 
-       redef init do add_option(send_emails, help)
+       redef init do add_option(send_emails, verbose, help)
+end
+
+redef class Sys
+       # Command line options
+       var opts = new OptionContext
 end
 
 # Avoid executing when running tests
 if "NIT_TESTING".environ == "true" then exit 0
 
-var opts = new OptionContext
 opts.parse args
 if not opts.errors.is_empty or opts.help.value == true then
        print opts.errors.join("\n")
@@ -230,11 +248,11 @@ if not opts.errors.is_empty or opts.help.value == true then
 end
 
 var ben = new Benitlux("sherbrooke")
-ben.run(opts.send_emails.value or else false)
+ben.run(opts.send_emails.value)
 
-# The parsing logic for the wellington locaiton is active (to gather data)
+# The parsing logic for the wellington location is active (to gather data)
 # but the web interface do not allow to subscribe to its mailing list.
 #
 # TODO revamp mailing list Web interface
 ben = new Benitlux("wellington")
-ben.run(opts.send_emails.value or else false)
+ben.run(opts.send_emails.value)