Sunday, June 22, 2014

Python + MongoDB

The opportunity to brush up on Python and MongoDB presented itself, so I'm spending some time doing just that.  I played with Python a few years back, in my GE days, but haven't made my way back to it.  I love REPL based languages because you can just fire it up and go!

So I have Mongo running on my machine, and I need to pump some data into it.  Since it wants JSON, I figured I'd just hit some web api out there, grab a bunch of JSON data, and then pump it into Mongo.  I already had an api key for imgur, because I was messing around with uploading camera images to it from an Android app.

So I poked around a little bit on the web api documentation, and pretty quickly I had a legit response.  It looks like the 'requests' library (http://docs.python-requests.org) is pretty popular.  So first thing I had to learn to do was import a 3rd party library.  Looks like this is easier in the Unix world because they have command line retrieval built in.  Windows appears to have it too, but of course you have to download it.

Anyway, I learned the "manual" way.  Basically, fetch the zip file, extract it, cd into the folder and run a command and it pulls it into your site-packages folder and you're good to go.

I do have to say, 5 lines is pretty terse.  And 2 of those are import statements.  I love terse.

 import json  
 import requests  
 url = 'https://api.imgur.com/3/gallery/g/memes/'  
 header = {'Authorization': 'Client-ID 31d84e7e126befd'}  #not my real api key!
 r = requests.get(url, headers=header)  

Here's an example of one item returned from the above call:

{u'account_id': 11545168,
 u'account_url': u'PCard',
 u'animated': False,
 u'bandwidth': 76530096,
 u'datetime': 1403397798,
 u'description': None,
 u'downs': 0,
 u'favorite': False,
 u'height': 820,
 u'id': u'1DZ31Oy',
 u'is_album': False,
 u'link': u'http://i.imgur.com/1DZ31Oy.png',
 u'nsfw': False,
 u'score': 2,
 u'section': None,
 u'size': 708612,
 u'subtype': u'Good Fireman Greg',
 u'title': u'Good Fireman Greg',
 u'type': u'image/png',
 u'ups': 2,
 u'views': 108,
 u'vote': None,
 u'width': 610}


I am working with memes here, because I think they are hilarious.  Good Guy Greg, Bad Luck Brian, etc.

Here's how we add all of the items to the Mongo database.  Note that we can pass the list, and it will be treated as a bulk insert.

 import pymongo  
 from pymongo import MongoClient  
 client = MongoClient()   
 db = client.imgur  
 db.memes.insert(r['data'])  

Now that we've got some data in our DB, we can query.  Let's say that I wanted to get all of the "Scumbag Steve" memes:


 c = db.memes.find({"subtype": "Scumbag Steve"}) #c is a db cursor  
 #print them to screen  
 for i in c:  
   pprint(i)  

There you have it!  Using Python to hit the Imgur Web Api, creating and populating a MongoDB with that data, and querying back from MongoDB.


Sunday, March 30, 2014

Android Accessibility

Well, I was browsing reddit.com/r/android the other day, and someone asked "How does LastPass read and inject into fields of other apps?"  I thought to myself, "yeah, how do they do that"?

When you install LastPass on Android, it asks for these permissions:
http://imgur.com/H9zm95D

That got me thinking, maybe I can read the screen from the William Hill app without requiring the user to take a screenshot and then send the photo to my app.  That would be a way nicer user experience.

So, that led me to discover the AccessibilityService, and today I'm going to see if I can figure out how to make this thing work.

It's March Madness right now, probably the 2nd best thing to football, which got me thinking about my app again.  I've also been giving more thought to taking the image recognition logic off of the phone and putting it on a server somewhere.

Anyway, just thought I'd post an update here.  That's all for now.

Thursday, January 9, 2014

Dormant

Well, I think it's pretty clear from the inactivity on this blog that I lost much momentum.  I haven't looked at the code for the Trakker project in months.  The NFL football season is coming to an end, and my Bengals suffered an embarrassing loss to the Chargers for another 1st round exit from the playoffs.

My son Benny is doing great.  Being a father has been amazing so far, and I know it's only the beginning of a long adventure.  It has proven much more difficult to get long stretches of uninterrupted time at home however, but I think that's just the way it goes.  I certainly don't want to be the kind of father who is always absorbed in something other than his children.

The tech space is so fun to be in.  There are so many options for things to learn and ideas to build.  I have made an effort recently to shore up my weaknesses on the web side and try to learn javascript and some frameworks.  I like Durandal so far, and I backed it on Kickstarter because the author of the framework seemed genuine and he's kind of doing a David & Goliath thing battling Google and AngularJS.

My tech goals as we kick off 2014:


  • Get an app on the Google Play store.  Even if it's trivial
  • Get more of a presence on GitHub so that I have a functional resume, rather than just the traditional one
  • Come up with more ideas that appeal to me because I am passionate about the domain and the problem being solved.  If it involves my interests like golf, guitar, sports, etc I am much more inclined to work these projects rather than contrived sample applications.
I don't know if I'll continue this blog or not, but I enjoy getting my thoughts out so I will continue to author posts like these in one form or another.

Here's to a great 2014!