Finding Free Food with Python
jamesbvaughan.com
jamesbvaughan.com
[1] Dartmouth is a weird place and for the longest time ran a unique homegrown emailish/im protocol called BlitzMail, with an entire vocabulary and culture associated with it.
I miss the days of those old IM systems that I could write my own processing pipelines and UIs for. It's sad that in these walled garden days of Whatsapp, Wechat et al. that there is no way for me to programatically access my own incoming messages.
What did you use? I'm assuming a CLI-based thing, since those seem to be the only ones that are truly flexible, but I haven't found any I actually like.
That's the main thing, the rest is just gravy: returning the event serialized as JSON/CSV (event name, date/time, URL) to be used in a web app or notification system -- so a simple web scraper can lead to exploring web dev (even just a simple Flask app) or fun APIs like AWS SNS and Twilio. You could even fit in a good "cache invalidation is a hard problem" lesson.
I never get around to assigning it because most people don't think of it as "serious work". Also, I'm afraid the CS dept. will obfuscate their calendar if random people start showing to things for free food. But I keep telling students, the only/best way to learn coding is to do something that directly affects your life or your bottom-line. It's the best way to put constraints on a project, i.e. think of things as the MVP that improves your life.
I learned Ruby and Ruby on Rails much faster than I had any right to, when my new job in NYC required it. I practiced not by writing Ruby on the job, but writing Ruby to scrape Craigslist apartment listings and feeding them into a spreadsheet.
I've thought about creating "personal data/programming projects"...in which the data comes from the student. Such as the SQLite that stores their Chrome/Firefox/Safari history. Or the parseable HTML dump that Facebook gives you when you request your records. Or your Twitter data dump. Or your Google search history.
But I've been reticent to do this. Partly because it's not a guarantee that every student uses Google or Facebook or has an Instagram. And partly because I'm deeply paranoid students (especially those who are novices about programming and operating systems) will accidentally upload or otherwise expose this sensitive data dump.
When I was at University, there was a public directory of employees and students available internally. You could opt-out, but very few people did. You could search by last name, but you had to use at least two letters and the responses cut off after 50 or something like that, although those 50 were in order. When I was in my senior year, somebody breathlessly ran to the student newspaper to loudly proclaim that they had scraped it and had a full copy of the database. I took a little impromptu survey at my next computer science class... "Who here hasn't at least mentally designed how to scrape the student database?" The answer was, nobody.
The student body at large was at least modestly impressed but that guy won no points with the computer science students that day.
Oh, and nobody had to "suggest" this to us.
On a related note, PadMapper [1] is an amazing scraper / browser for saving time apartment hunting on Craigslist.
> I never get around to assigning it because most people don't think of it as "serious work". Also, I'm afraid the CS dept. will obfuscate their calendar if random people start showing to things for free food.
It's a practical problem to solve, and the calendar obfuscation angle is a real world issue. If the information itself is valuable and you're trying to aggregate it, it's naturally going to become a moving target. I think it's a solid lesson.
I started teaching myself computer programming in 2011/2012: I bought a bunch of Head First books published by O'Reilly, watched the entry-level CS video lectures that Stanford provided for free, participated in the first few Udacity courses, and I tried a couple Coursera classes. But I think I learned the most (in the shortest amount of time) when I went through your online book, 'The Bastards Book of Ruby'[0]
I loved the real-world examples you used: fetching Tweets through the Twitter API, web scraping with Nokogiri, and manipulating images with ImageMagick/RMagick are a few that stick out to me.
I'm sure you're doing great things at Stanford but I'm also confident that you could come out with a new book (covering the topics you spoke of above) that would help motivate people who are on the fence on whether or not they should start/continue learning computer programming.
I'd be happy to collaborate on something but I would argue that Python would be a better language to use than Ruby. Here are some of the topics I'd like to share with people (which I've found to be useful in my career/side-projects): reading data from CSV/Excel files (xlrd[1]), fetching data from APIs (requests[2]), web scraping (BeautifulSoup[3], Selenium[4]), connecting to a SQL database within a Python script (psycopg[4]), complex mathematical computations (numpy), and downloading videos/metadata from YouTube (youtube-dl[6]) are a few that come to mind.
[0] http://ruby.bastardsbook.com/ [1] https://github.com/python-excel/xlrd [2] http://docs.python-requests.org/en/master/ [3] https://www.crummy.com/software/BeautifulSoup/ [4] http://selenium-python.readthedocs.io/ [5] http://initd.org/psycopg/ [6] http://www.numpy.org/ [7] https://rg3.github.io/youtube-dl/
Mind you, I haven't done the math on this, and presumably neither have the participating restaurants. However, the logic is sound, and becomes even sounder if you treat a restaurant owner to a couple of glasses of wine.
However, doesn't this script either send out the same text very often, or potentially send it out too late (by e.g. only letting the cronjob run every 6 hours)?
I assume that time is of the essence in this situation. Some sort of log on sent texts would surely be helpful.
Tiny detail - you can do the regex match case insensitive and avoid the call to lower() for every string.
re.match(..., re.I)There's a lot of junk that gets put up there, but also some gems. Unfortunately it seems like the gems are always on the days I don't check it...
free_food = [s for s in soup.stripped_strings if "free" in s.lower()]
works free_food = any(s for s in soup.stripped_strings if ...) free_food = 'free' in soup.get_text().lower()Also, lxml is over 20x faster than BeautifulSoup, although it doesn't really matter with such a low frequency of requests, it's something to note if you had another project that required parsing e.g. 1,000's of html pages.
There are some sites that allegedly publish coupons, but I feel like a dummy only scrolling through those, it's full of ads and crap.
What would be the proper channels to scrape for promo codes of the cloud providers? Twitter feeds, something else?
Affiliate programs.
Disclaimer: I helped building this.
cloudflare captcha is blocking me to test this.
I write these kind of mini-scripts all the time
I teach for a living (music technology), and it's incredible how badly a lot of things are explained (in all fields). Clear explanation and worked examples, combined with appropriate progression in difficulty is what makes for good learning, and I think this is a good example.
I spend a lot of time here, mostly in awe of the expertise of others and hoping to learn something from them.