Permalink
Browse files

NEW: Implement complete new API.

  • Loading branch information...
lbdremy committed Oct 21, 2012
1 parent 3600e7e commit aacb1b0c2c0660ca084250cb09d001a8bdd4e05c
View
@@ -1,2 +1,3 @@
node_modules
-old_test
+old_test
+old_examples
View
@@ -1,106 +0,0 @@
-# Scrapinode
-Modular Scraper
-
-[![](http://travis-ci.org/lbdremy/scrapinode.png)](http://travis-ci.org/#!/lbdremy/scrapinode)
-
-##Features
-###Clients
- - Implicit routing using domain name and content-type name.
- - Possibility to add extensions named scrapules for a better scrapping.
- - Lazy loading of these extensions
-
-###Developers
- - Create your own scrap function for specific domain name and specific content.
-
-##Install
-
-```
-npm install scrapinode
-```
-##Get Started
-
-```js
- var scrapinode = require('scrapinode');
- // Load the default `Scrapule` into the set of scrapules available for scrapinode.
- // Look at the default scrapule contained in /lib/scrapules/default.js to know more about this scrapule.
- scrapinode.init();
-
- // Load a set of `Scrapule` into scrapinode contained in the scrapules folder (a scrapule is a set of extractors)
- // An absolute path is required in argument.
- scrapinode.use( __dirname + '/path/to/my/personal/scrapules/');
-
- // Scrap the awesome video page of Norman on YouTube
- var url = 'http://www.youtube.com/user/NormanFaitDesVideos';
- scrapinode.createScraper(url ,function(err,scraper){
- if(err){
- console.log(err);
- }else{
- var images = scraper.get('images');
- var videos = scraper.get('videos');
- var title = scraper.get('title');
- var description = scraper.get('description');
- }
- });
-
- // Scrap an amazon product
- var url = 'http://www.amazon.co.uk/gp/product/B005PVOMTO/ref=s9_simh_gw_p63_d0_g63_i3?pf_rd_m=A3P5ROKL5A1OLE&pf_rd_s=center-2&pf_rd_r=1AR79J0XVRX6B58D60Y1&pf_rd_t=101&pf_rd_p=467128533&pf_rd_i=468294';
- scrapinode.createScraper(url ,function(err,scraper){
- if(err){
- console.log(err);
- }else{
- var images = scraper.get('images');
- var videos = scraper.get('videos');
- var title = scraper.get('title');
- var description = scraper.get('description');
- }
- });
-```
-##Create your own scrap[mod]ule
-A `Scrapule` is an `Object` more particularly an `Array` implementing the following interface:
-
-```js
-// Create your scrapule
-var scrapule = [
- {
- route : 'domain#contentType1',
- operation : function(url,$){ }
- },
- {
- route : 'domain#contentType2',
- operation : function(url,$){ }
- },
- {
- route : 'domain#contentType3',
- operation : function(url,$){ }
- },
- {
- route : 'domain#contentType4',
- operation : function(url,$){ }
- }
-];
-
-// Expose it
-module.exports = scrapule;
-
-```
-
-Have a look to the default scrapule in the file [`/lib/scrapules/default.js`](https://github.com/lbdremy/scrapinode/blob/master/lib/scrapules/default.js) if you want to know more.
-
-##Test
-
-```
-npm test
-```
-##Contributions
-Contributions, suggestions, comments, issues are welcome.
-
-##Licence
-(The MIT License)
-
-Copyright (c) 2010-2011 HipSnip Limited
-
-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.
View
@@ -0,0 +1,51 @@
+# scrapinode - content driven and route based scraper
+
+[![](http://travis-ci.org/lbdremy/scrapinode.png)](http://travis-ci.org/#!/lbdremy/scrapinode)
+
+## Install
+
+```sh
+npm install scrapinode
+```
+
+## Usage
+
+```js
+var scrapinode = require('scrapinode');
+
+// Define an operation for a specific route and content
+scrapinode.use('society6.com','title',function(window,next){
+ var $ = window.$;
+ var url = window.url; // maybe you want to check for some reasons
+ var title = $('h1[itemprop="name"]').text();
+ if(!title) return next();
+ return title;
+});
+
+// Use default operations for content like "title", "descriptions", "images", "videos"
+scrapinode.useAll(scrapinode.defaults());
+
+scrapinode.createScraper('http://society6.com/product/Sounds-Good-Dude_T-shirt',function(err,scraper){
+ if(err) return console.error(err);
+ var title = scraper.get('title');
+ console.log(title); // "Sound Good dude"
+});
+```
+
+## Test
+
+```sh
+npm test
+```
+
+## Licence
+
+(The MIT License)
+
+Copyright (c) 2012 Rémy Loubradou
+
+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.
View
@@ -0,0 +1,23 @@
+/**
+ * Module dependencies
+ */
+
+var scrapinode = require('./../');
+
+// Define an operation for a specific route and content
+scrapinode.use('society6.com','title',function(window,next){
+ var $ = window.$;
+ var url = window.url; // maybe you want to check for some reasons
+ var title = $('h1[itemprop="name"]').text();
+ if(!title) return next();
+ return title;
+});
+
+// Use default operations for content like "title", "descriptions", "images", "videos"
+scrapinode.useAll(scrapinode.defaults());
+
+scrapinode.createScraper('http://society6.com/product/Sounds-Good-Dude_T-shirt',function(err,scraper){
+ if(err) return console.error(err);
+ var title = scraper.get('title');
+ console.log(title); // "Sound Good dude"
+});
@@ -1,21 +0,0 @@
-var scrapinode = require('./../main.js')();
-
-// Issue with this page similar to https://github.com/tmpvar/jsdom/issues/290
-// Fix https://github.com/tmpvar/jsdom/pull/387
-// Works great with cheerio
-var url = 'http://www.dell.com/uk/p/xps-15z/pd?oc=n0015z01epp&model_id=xps-15z&';
-
-scrapinode.createScraper(url,'jsdom',function(err,scraper){
- if(err){
- console.log(err);
- }else{
- var title = scraper.get('title');
- var description = scraper.get('description');
- var images = scraper.get('images');
- console.log('IMAGES');
- console.log(images);
- console.log('TITLE:'+title);
- console.log('DESCRIPTION');
- console.log(description);
- }
-});
View
@@ -1,22 +0,0 @@
-var scrapinode = require('./../main.js')();
-
-// Issue with this page similar to https://github.com/tmpvar/jsdom/issues/290
-//var url = 'http://www.dell.com/uk/p/xps-15z/pd?oc=n0015z01epp&model_id=xps-15z&';
-// But Works great with cheerio
-
-var url = 'http://www.amazon.co.uk/gp/product/B004NNU9YM/ref=s9_simh_gw_p23_d1_g23_i5?pf_rd_m=A3P5ROKL5A1OLE&pf_rd_s=center-3&pf_rd_r=147TGMN0P7SCYD1BCNKC&pf_rd_t=101&pf_rd_p=467128473&pf_rd_i=468294';
-
-scrapinode.createScraper(url,'cheerio',function(err,scraper){
- if(err){
- console.log(err);
- }else{
- var title = scraper.get('title');
- var description = scraper.get('description');
- var images = scraper.get('images');
- console.log('IMAGES');
- console.log(images);
- console.log('TITLE:'+title);
- console.log('DESCRIPTION');
- console.log(description);
- }
-});
View
@@ -1,22 +0,0 @@
-var scrapinode = require('./../main.js')();
-
-var url = 'http://ecx.images-amazon.com/images/I/51RRbTkbfBL._SL500_AA300_.jpg';
-
-scrapinode.createScraper(url,'jsdom',function(err,scraper){
- if(err){
- console.log(err);
- }else{
- var title = scraper.get('title');
- var description = scraper.get('description');
- var images = scraper.get('images');
- var video = scraper.get('video');
- console.log('IMAGES');
- console.log(images);
- console.log('TITLE:'+title);
- console.log('DESCRIPTION');
- console.log(description);
- console.log('VIDEO');
- console.log(video);
- }
-});
-
View
File renamed without changes.
View
@@ -1,18 +0,0 @@
-// Modules dependencies
-
-// Expose Extractor
-module.exports = exports = Extractor;
-
-function Extractor(route,operation){
- this._parse(route);
- this.operation = operation;
-}
-
-Extractor.prototype._parse = function(rt){
- this.route = {};
- routeSplit = rt.split('#');
- if(routeSplit.length === 2 ){
- this.route.site = routeSplit[0];
- this.route.content = routeSplit[1];
- }
-}
View
@@ -1,73 +0,0 @@
-// Modules dependencies
-
-
-// Expose Router
-module.exports = exports = Router;
-
-// The Router object is responsible for drive a route to the specific extractor
-function Router(){
- this.extractors = []; // Array of Extractors
- this.scrapules = [];
-}
-
-Router.prototype.addExtractor = function(extractor){
- var self = this;
- this.extractors.push(extractor);
- // Allow chainable call
- return self;
-}
-
-Router.prototype.addExtractors = function(extractors){
- var self = this;
- this.extractors = this.extractors.concat(extractors);
- // Allow chainable call
- return self;
-}
-
-Router.prototype.addScrapule = function(scrapule){
- var self = this;
- this.scrapules.push(scrapule);
- // Allow chainable call
- return self;
-}
-
-Router.prototype.resolve = function(route){
- var route = parseRoute(route);
- // Check scrapules
- for(var i = 0; i < this.scrapules.length; i++){
- if(route.site === this.scrapules[i].site || this.scrapules[i].site === 'default'){ // Always load default scrapules on the first scrap
- this.addExtractors(this.scrapules[i].loadExtractors());
- this.scrapules.splice(i,1); // Removed the scrapule already loaded
- i--;
- }
- }
- // Check extractors
- var extractor;
- for(var i = 0; i < this.extractors.length ; i++){
- if(matchRoute(this.extractors[i],route)){
- extractor = this.extractors[i];
- if(extractor.route.site !== 'default') break; // Break the loop if the specific extractor has been found
- }
- }
- return extractor;
-}
-
-function matchRoute(extractor,route){
- var pass = false;
- if(extractor.route.site === route.site && extractor.route.content === route.content){
- pass = true;
- }else if(extractor.route.site === 'default' && extractor.route.content === route.content){
- pass = true;
- }
- return pass;
-}
-
-function parseRoute(rt){
- var route = {};
- routeSplit = rt.split('#');
- if(routeSplit.length === 2 ){
- route.site = routeSplit[0];
- route.content = routeSplit[1];
- }
- return route;
-}
View
@@ -1,36 +0,0 @@
-// Dependencies
-var urltools = require('./../util/urltools');
-
-
-// Expose Scraper
-
-module.exports = exports = Scraper;
-
-// Scraper contructor
-
-function Scraper(url,$){
- this.url = url;
- this.$ = $;
- this.webname = urltools.webname(url);
-}
-
-
-// Prototype
-
-Scraper.prototype.setRouter = function(router){
- this.router = router;
-}
-
-/**
- * Get a type of content from the page just downloaded that can be title, images, videos, description by default can add more if you add scrapule to scrapinode
- *
- */
-Scraper.prototype.get = function(content,options){
- var route = this.webname + '#' + content;
- var extractor = this.router.resolve(route);
- var result;
- if(extractor){
- result = extractor.operation(this.$,this.url,options);
- }
- return result;
-}
Oops, something went wrong.

0 comments on commit aacb1b0

Please sign in to comment.