Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SpeCT committed Apr 5, 2012
0 parents commit afd8199
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2011 Yury Proshchenko <spect.man@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
59 changes: 59 additions & 0 deletions README.md
@@ -0,0 +1,59 @@
#node-ogp
Easy way to access OpenGraph meta tags from jsdom parsed web page

## Installation
Via [npm](http://search.npmjs.org/#/ogp):

$ npm install ogp

As a submodule of your project

$ git submodule add http://github.com/SpeCT/node-ogp.git ogp
$ git submodule update --init


## Usage

```javascript
// 1. load node-ogp
var ogp = require('ogp')

// 2. parse DOM using jsdom
var jsdom = require('jsdom'),
url = 'http://www.imdb.com/title/tt0068646/'

jsdom.env({ html: url, done: function(error, window) {

// 3. pass window as the only argument to ogp.parse method
var ogData = ogp.parse(window)

// 5. Profit!
console.log('Open Graph data', ogData)
}})
```

This will put next structure into stdout:

```
{ url: 'http://www.imdb.com/title/tt0068646/',
title: 'The Godfather (1972)',
type: 'video.movie',
image: 'http://ia.media-imdb.com/images/M/MV5BMTIyMTIxNjI5NF5BMl5BanBnXkFtZTcwNzQzNDM5MQ@@._V1._SX97_SY140_.jpg',
site_name: 'IMDb' }
```

## Credits

Written and maintained by [Yury Proshchenko](mailto:spect.man@gmail.com).

## License

The MIT License

Copyright (c) 2011 Yury Proshchenko (spect.man@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 changes: 8 additions & 0 deletions example/app.js
@@ -0,0 +1,8 @@
var ogp = require('ogp'),
jsdom = require('jsdom'),
url = 'http://www.imdb.com/title/tt0068646/'

jsdom.env({ html: url, done: function(error, window) {
var ogData = ogp.parse(window)
console.log('Open Graph data', ogData)
}})
18 changes: 18 additions & 0 deletions example/package.json
@@ -0,0 +1,18 @@
{
"name": "ogp-example",
"description": "node-ogp library usage example",
"private": true,
"version": "1.0.0",
"author": "Yury Proshchenko <spect.man@gmail.com>",
"contributors": [
{ "name": "Yury Proshchenko", "email": "spect.man@gmail.com" }
],
"keywords": ["open graph", "opengraph", "ogp"],
"main": "./app.js",
"engines": { "node": ">= 0.2.0" },

"dependencies": {
"jsdom": "*",
"ogp": "0.0.x"
}
}
30 changes: 30 additions & 0 deletions lib/ogp.js
@@ -0,0 +1,30 @@
exports.parse = function(window) {
var ns
for (var i = 0; i < window.document.documentElement.attributes.length; ++i) {
var attr = window.document.documentElement.attributes[i]
if (attr.nodeValue.toLowerCase() !== 'http://opengraphprotocol.org/schema/') continue

ns = attr.nodeName.substring(6)
if (ns) break
}

if (!ns) return {}

var result = {},
metaTags = window.document.getElementsByTagName('meta')

for (var i = 0; i < metaTags.length; ++i) {
var tag = metaTags[i],
propertyAttr = tag.attributes['property']

if (!propertyAttr || propertyAttr.nodeValue.substring(0, ns.length) !== ns)
continue

var property = tag.attributes['property'].nodeValue.substring(ns.length+1),
content = tag.attributes['content'].nodeValue

result[property] = content
}

return result
}
22 changes: 22 additions & 0 deletions package.json
@@ -0,0 +1,22 @@
{
"name": "ogp",
"description": "Parse Open Graph meta tags from jsdom generated DOM",
"version": "0.0.1",
"author": "Yury Proshchenko <spect.man@gmail.com>",
"contributors": [
{ "name": "Yury Proshchenko", "email": "spect.man@gmail.com" }
],
"keywords": ["open graph", "opengraph", "ogp"],
"main": "./lib/ogp.js",
"directories": {
"lib": "./lib",
"example": "./example"
},
"repository": {
"type": "git",
"url": "http://github.com/SpeCT/node-ogp.git",
"web" : "http://github.com/SpeCT/node-ogp"
},
"bugs": "http://github.com/SpeCT/node-ogp/issues",
"engines": { "node": ">= 0.2.0" }
}

0 comments on commit afd8199

Please sign in to comment.