How I forced myself to code daily, using a pizza API.

sriram ananthakrishnan
HackerNoon.com
4 min readJan 17, 2019

--

Monday evening, traveling back home from college , a long bus ride ahead of me and I started craving for pizza. Hmm, I thought to myself, “How neat would it be if there was an API for it ?”. Yes you’re probably wondering why couldn’t I use one of the umpteen applications out there, one which even allows you to order pizza on a single click, shout out to push for pizza !, but here’s the thing, I am a student and I can’t afford to buy pizza too often. Unless, I earn it.

I need to code, but I want pizza. So my solution was to simply apply positive feedback when I code, by treating myself to pizza. Pretty neat right ? Now y’all can say that y’all know reinforcement learning using a feedback loop.

So I need two things.

1.A way to track my work and check if I am regularly posting code, and yes this has to be a third party entity, to keep us honest.

Throwback

2. A way to order pizza ONLY if point 1 passes.

3. Skip all this and just order a pizza already.

If you held out on point 3, this article’s for you. If not this article is even more important for you.

So, to track our work we can use www.github.com. I would have preferred www.hackerrank.com but their API was deprecated and GitHub makes it more generic.

To order the pizza I forked this github repository www.github.com/Magicjarvis/pizzapi by Jarvis Johnson and made a few changes to it.

Step 1. Implement the API to order the pizza.

The API requires the following information, Customer Details like name, email id, address and phone number. It uses this information to create a Customer object.

customer = Customer('Barack', 'Obama', 'barack@whitehouse.gov', '2024561111', '700 Pennsylvania Avenue NW, Washington, DC, 20408')closest_dominos = StoreLocator.find_closest_store_to_customer(customer)

Once the customer object is created, we find the store closest to us, using the store locator.

We then search for an item in the menu, which in my case was “Coke” and we get a list of items branded “Coke” and their respective order IDs.

menu = closest_dominos.get_menu()print("search the menu for coke, because you're cheap :)")menu.search(Name='Coke')order = Order.begin_customer_order(customer, closest_dominos)order.add_item('20BCOKE')

Once we have our order ready, we enter our credit card information and then place the order to our local store :)

card = CreditCard('XXXXXXXXXXX', 'XXXX', 'XXX', 'XXXXX')order.place(card)closest_dominos.place_order(order, card)

With me so far ? Great ! Now we move on to step 2, which is to allow us to order a pizza only if I have checked in code into GitHub within the last 24 hours. You can change this number as you see fit.

Step 2. We use PyGithub which is a python library for github.

We first create a user object using our credentials.

g = Github("g_eazy","!GreatestRapperAlive")user = g.get_user()

Now using the user object we obtain the repository we require by simply calling the get_repo() function on the user object.

repo = g.get_user().get_repo("Daily_Code")print("last modified at",repo.last_modified)

Here we compare the last modified date with our current date and check the number of days it’s been since we last checked in some code.

last_updated_date = repo.updated_atcurrent_time = datetime.now()diff_days = (current_time-last_updated_date).daysprint("Number of days without a code checkin",diff_days)if diff_days > 0:    print("Sorry NO CODE NO PIZZA !!")else:    print("Enjoy thy offering")    order_pizza()

It really is that simple !

Optional

I went an extra step and used Twilio to send an SMS alert to my phone when the order has been placed.

Sample credentials, please change it before you order a pizza.

Did It Work ?

The code worked, but sadly my willpower did not :(. I caved in and ordered the pizza without a code check-in. I hate myself for it and hopefully the next time I will find a better solution to not get around this. HackerRank please bring back your APIs, we can do wonderful things together, like solve 2 company contests and the next pizza is on them.

For the entire code, please check out www.github.com/n00b007/pizzapi. Thanks to Jarvis Johnson for his awesome tutorial.

--

--

sriram ananthakrishnan
HackerNoon.com

Masters student at Georgia State University. I love building products and eating pizza.