You Down With OGP?

Sharing links via social media is a pretty mundane task, but have you ever wondered how Google & Facebook always know the exact title, summary, and image to display?

Sharing a link on Google+

 

There’s some neat tech at work there and it’s based on the Open Graph Protocol (OGP). OGP meta tags enable graph-aware apps to see what’s behind a link - if you know how to parse them.

Of course, the last thing you want to do is write yet another HTML parser. Luckily, you don’t have to!

Erik Rivera already did the work for you with his OpenGraph Python module. In three lines of code, OpenGraph can tell you if a URL has valid OGP tags and output them as HTML or JSON. QED and on to your next challenge!

```python import opengraph

url = opengraph.OpenGraph(url=”http://goo.gl/b1CTb3”) url.is_valid() # returns true since OGP tags are valid

print url.to_json() # JSON at your service! ‘’’ { “site_name”: “Gizmodo”, “description”: “Google just announced its first real foray into the frenzied fray of the smartwatch world: Android Wear. That’s an accurate name for a wearable, Android-powered device, sure, but it’s also a little misleading. This is a Google Now watch. And that’s excellent news.”, “title”: “Android Wear Is Just a Google Now Watch (And That’s Great)”, “url”: “http:\/\/gizmodo.com\/android-wear-is-just-a-google-now-watch-and-thats-gre-1546476502”, “image”: “http:\/\/i.kinja-img.com\/gawker-media\/image\/upload\/t_k-bigpic\/tdugvzikcz8dhs4wj0ju.png”, “scrape”: false, “locale”: “en_US”, “_url”: “http:\/\/goo.gl\/b1CTb3”, “type”: “article” } ‘’’ ```

Join the discussion

2017 Neal Shyam