contrib/benitlux: add `Beer::id`
[nit.git] / contrib / benitlux / src / benitlux_daily.nit
index f4a3f25..27b8169 100644 (file)
@@ -89,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
@@ -112,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
 
@@ -152,11 +166,17 @@ 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
@@ -171,7 +191,7 @@ class Benitlux
        # 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
 
@@ -189,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
@@ -200,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")