> ## Content Index
> Fetch the complete content index at: https://www.alt-counsel.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Get your daily dose of Sudoku with a little bit of Python
- URL: https://www.alt-counsel.com/get-your-daily-dose-of-sudoku-with-a-little-bit-of-python/
- Published: 2020-06-16T13:44:00.000Z
- Updated: 2022-05-28T02:54:32.000Z
- Author: Houfu Ang
- Tags: Programming, Docker, Python

I’m always trying my best to be a good husband. Unfortunately, compared to knitting, cooking and painting, computer programming does not *look* essential. You sit in front of a computer, furiously punching on your keyboard. Something works, but most people can’t see it. Sure, your phone works, but how did your sitting around contribute to that? Geez. It’s time to contribute around the house by automating with Python!

The goal is to create a script that will download a sudoku puzzle from [daily sudoku](http://www.dailysudoku.com/sudoku/?ref=alt-counsel.com) and print it in at home. My wife likes doing these puzzles, so I was sure that she would appreciate having one waiting for her to conquer in the printer tray.

You can check out the code in [its repository ](https://github.com/houfu/daily%5Fsudoku?ref=alt-counsel.com)and the [docker hub](https://hub.docker.com/r/houfu/daily%5Fsudoku?ref=alt-counsel.com) for the image. An outline of the solution is provided below:

1. Write a Python script that does the following things: (1) Set up a schedule to get the daily sudoku and download it, (2) sends an email to my HP Printer email address.
2. HP ePrint prints the puzzle.
3. Dockerize the container so that it can be run on my home server.
4. Wait patiently around 9:25 am every day at the printer for my puzzle.

## Coding highlights

This code is pretty short since I cobbled it together in about 1 night. You can read it on your own, but here are some highlights.

### Download a file by constructing its path

As I mentioned before, you have to [study your quarry carefully ](https://www.alt-counsel.com/automate-boring-stuff-get-python-and-your-web-browser-to-download-your-judgements/)in order to use it. For the [daily sudoku](http://www.dailysudoku.com/sudoku/?ref=alt-counsel.com) website, I found a few possibilities to automate the process of getting your daily sudoku.

- Visit the [main web page](http://www.dailysudoku.com/sudoku/today.shtml?ref=alt-counsel.com) and “click” on the using an automated web browser.
- Parse the [RSS feed](http://www.dailysudoku.com/sudoku/rss.xml?ref=alt-counsel.com) and get the puzzle you would like.
- “Construct” the URL to the PDF of the puzzle of the day

I went with the last option because it was the easiest to implement without needing to download additional packages or pursue extra steps.

```python
now = datetime.now()   
r = requests.get(
        f"http://www.dailysudoku.com/sudoku//pdf/{now.year}/"
        f"{now.strftime('%m')}/{now.strftime('%Y-%m-%d')}_S1_N1.pdf",
        timeout=5)
```

Notice that the python code using f-strings and `strftime`, which provides a text format for you to fill your URL.

This method ain’t exactly foolproof. If [the structure of the website is changed](https://www.alt-counsel.com/back-to-the-drawing-board/), the whole code is useless.

### Network printing — a real PITA

My original idea was to send a PDF directly to a network printer. However, it was far more complicated than I expected. You could send a file through the Internet Print Protocol, Line Printer Daemon or even HP’s apparently proprietary port 9100\. First, though, you might need to convert the PDF file to a Postscript file. Then locate or open a socket… You could install CUPS in your container…

Errm never mind.

### Sending an email to print

Luckily for me, HP can print PDF attachments sent by email. It turns out that sending a simple email using Python is quite straightforward.

```python
msg = EmailMessage()
msg['To'] = print_email
msg['From'] = smtp_user
msg['Subject'] = 'Daily sudoku'
msg.add_attachment(r.content, maintype='application', subtype='pdf', filename='sudoku.pdf')
with SMTP(smtp_server, 587) as s:
    s.starttls()
    s.login(smtp_user, smtp_password) 
    s.send_message(msg)
```

However, HP’s requirements for sending a valid email to their HP ePrint servers is kind of quirky. For example, your attachment will not print if:

- There is no subject in the email.
- The attachment has no filename (stating the MIME type of the file is not enough)
- The person who emails the message must be a permitted user. You have to go to the HP Connected website to set these allowed senders manually.

### Setting the local timezone for the docker container

The `schedule` package does not deal with time zones. To be fair, if you are not really serving an international audience, that’s not too bad. However, for a time-sensitive application like this, there’s a big difference between waiting for your puzzle at 9:30 am local time and 9:30 am UTC (that’s nearly time to knock off work in Singapore!).

Setting your time zone in a docker container depends on the base image of the Operating System you used. I used Debian for this container, so the code is as follows.

```bash
RUN ln -sf /usr/share/zoneinfo/Asia/Singapore /etc/localtime
```

Note that the script sleeps for 3 hours before executing pending jobs. This means that while the job is submitted at 9:30 am, it may be quite sometime later before it is executed.

### Environment variables

The code does not make it very obvious, but you must set environment variables in order to use the script. (Oh read the README for crying out loud) This can be done in a cinch with a docker-compose file.

```json
sudoku:
    image: "houfu/daily_sudoku:latest"
    hostname: sudoku
    container_name:  daily_sudoku
    environment:
      - PRINT_EMAIL=ABCDEFG@hpeprint.com
      - SMTP_SERVER=smtp.gmail.com
      - SMTP_USER=my_email@gmail.com
      - SMTP-PWD=blah blah blah
```

**Update 17/6/2020:** There was a typo in the address of Gmail’s SMTP server and this is now rectified.

## Conclusion

I hastily put this together. It’s more important to have a puzzle in your hand that configuration variables and plausibly smaller containers. Since the project is personal, I don’t think I will be updating it much unless it stops working for me. I’ll be happy to hear if you’re using it and you may have some suggestions.

In the meantime, do visit [www.dailysudoku.com](http://www.dailysudoku.com/?ref=alt-counsel.com) for more puzzles, solutions, hints, books and other resources!