4

For my web app, I need to get a preview of URL given by the user. For example, in Facebook, if the user copy-paste an URL,it automatically fetches the content of the URL and displays a preview of the page. How does it work? Is it a feature of Ajax? I need to do it in Django. Any tutorials or demos?

arulmr
  • 8,620
  • 9
  • 54
  • 69
88dax
  • 307
  • 1
  • 6
  • 14

1 Answers1

6

For Facebook, there is a standard way to make your site "preview" compatible, using open graph tags. The most common ones are:

<meta property="og:title" content="The Rock" />
<meta property="og:type" content="video.movie" />
<meta property="og:url" content="http://www.imdb.com/title/tt0117500/" />
<meta property="og:image" content="http://ia.media-imdb.com/images/rock.jpg" />

The complete list is available at the open graph protocol site. You can use PyOpenGraph to parse a URL for its open graph tags. Here is the example from the readme:

>>> import PyOpenGraph
>>> og = PyOpenGraph('http://www.rottentomatoes.com/m/10011268-oceans/')
>>> print og.metadata
{'url': 'http://www.rottentomatoes.com/m/10011268-oceans/',
 'site_name': 'Rotten Tomatoes',
 'image': 'http://images.rottentomatoes.com/images/movie/custom/68/10011268.jpg',
 'type': 'movie',
 'title': 'Oceans'}
>>> print og.metadata['title']
Oceans
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • 1
    I have just used 'Facebook' as example. But PyOpenGraph seems to be used for turning webpages into graph objects. All that I'm asking is, if an user enters an URL in the text box,the web app should display a preview of that webpage instead of displaying just the URL as entered by the user. – 88dax Apr 03 '13 at 13:48
  • If I get it correctly, you ant to make a screen capture of some webpage in your Django view before displaying it in your template. Have you seen: http://stackoverflow.com/questions/1197172/how-to-get-a-website-screenshot-in-python ? – niconoe Apr 03 '13 at 13:53
  • No. its not a screenshot. I just want a preview of the webpage instead of the URL. If you post an Youtube link, you should be able to see the youtube video as preview of the URL. If you As how you do in Facebook. – 88dax Apr 03 '13 at 14:29
  • @GobinathEm that's not what `PyOpenGraph` does; it reads the graph tags, so that you can display the appropriate preview _like what facebook does_. If the site doesn't have any open graph tags, facebook won't display any preview. – Burhan Khalid Apr 03 '13 at 15:25