Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Syl committed Feb 4, 2015
0 parents commit 80e1fe3
Show file tree
Hide file tree
Showing 14 changed files with 510 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
.DS_Store
composer.lock
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- hhvm

matrix:
allow_failures:
- php: hhvm

before_script:
- composer self-update
- composer install

script:
- phpunit

notifications:
email: false
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 Syl

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.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Marmiton Crawler

[![Build Status](http://img.shields.io/travis/sylouuu/marmiton-crawler.svg?style=flat)](https://travis-ci.org/sylouuu/marmiton-crawler)
[![Version](http://img.shields.io/packagist/v/sylouuu/marmiton-crawler.svg?style=flat)](https://packagist.org/packages/sylouuu/marmiton-crawler)
[![CodeClimate](http://img.shields.io/codeclimate/github/sylouuu/marmiton-crawler.svg?style=flat)](https://codeclimate.com/github/sylouuu/marmiton-crawler)

## Requirements

* PHP >= 5.4
* [cURL](http://php.net/manual/fr/book.curl.php/) library enabled

## Install

### Composer

```js
{
"require": {
"sylouuu/marmiton-crawler": "0.1.*"
}
}
```

```php
require_once './vendor/autoload.php';
```

## Usage

```php
use sylouuu\MarmitonCrawler\Recipe\Recipe;

// Fetch the recipe
$recipe = new Recipe('http://www.marmiton.org/recettes/recette_salade-de-papayes-epicee_333809.aspx');

// JSON format
$recipe = $recipe->getRecipe();
```

## Tests

On project directory:

* `composer install` to retrieve `phpunit`
* `phpunit` to run tests

## Changelog

2014-02-04 - **0.1.0**

* Initial release
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "sylouuu/marmiton-crawler",
"description": "Crawl Marmiton recipes",
"type": "library",
"keywords": ["marmiton", "recipe"],
"minimum-stability": "stable",
"homepage": "https://github.com/sylouuu/marmiton-crawler",
"license": "MIT",
"support": {
"issues": "https://github.com/sylouuu/marmiton-crawler/issues"
},
"require": {
"paquettg/php-html-parser": "1.6.*",
"sylouuu/php-curl": "0.7.*"
},
"require-dev": {
"phpunit/phpunit": "4.0.*"
},
"autoload": {
"psr-4": {
"sylouuu\\MarmitonCrawler\\": "src/"
}
}
}
8 changes: 8 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" strict="true" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Test Suite">
<directory suffix=".php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
32 changes: 32 additions & 0 deletions src/Recipe/Parser/CookTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace sylouuu\MarmitonCrawler\Recipe\Parser;

use PHPHtmlParser\Dom;

/**
* CookTime Parser
*
* @author sylouuu
* @link https://github.com/sylouuu/marmiton-crawler
* @version 0.1.0
* @license MIT
*/
class CookTime
{
/**
* Extract from DOM object
*
* @param object $dom
* @return int
*/
public static function parse ($dom)
{
$element = $dom->find('.m_content_recette_info span.cooktime');

if ($element->text !== null) {
return intval(trim($element->text));
} else {
return null;
}
}
}
36 changes: 36 additions & 0 deletions src/Recipe/Parser/GuestsNumber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace sylouuu\MarmitonCrawler\Recipe\Parser;

use PHPHtmlParser\Dom;

/**
* GuestsNumber Parser
*
* @author sylouuu
* @link https://github.com/sylouuu/marmiton-crawler
* @version 0.1.0
* @license MIT
*/
class GuestsNumber
{
/**
* Extract from DOM object
*
* @param object $dom
* @return int $guests_number
*/
public static function parse ($dom)
{
$element = $dom->find('.m_content_recette_ingredients span')[0];

if ($element !== null) {
$guests_number = trim($element->text);
preg_match_all('/\d+/', $guests_number, $matches);
$guests_number = intval($matches[0][0]);

return $guests_number;
} else {
return null;
}
}
}
40 changes: 40 additions & 0 deletions src/Recipe/Parser/Ingredients.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
namespace sylouuu\MarmitonCrawler\Recipe\Parser;

use PHPHtmlParser\Dom;

/**
* Ingredients Parser
*
* @author sylouuu
* @link https://github.com/sylouuu/marmiton-crawler
* @version 0.1.0
* @license MIT
*/
class Ingredients
{
/**
* Extract from DOM object
*
* @param object $dom
* @return array $ingredients
*/
public static function parse ($dom)
{
$element = $dom->find('.m_content_recette_ingredients');

if ($element->innerHtml !== null) {
$ingredients = trim($element->innerHtml);
$ingredients = utf8_encode(strip_tags($ingredients));
$ingredients = explode(':', $ingredients);
$ingredients = trim($ingredients[1]);
$ingredients = explode('-', $ingredients);
array_shift($ingredients);
$ingredients = array_map('trim', $ingredients);

return $ingredients;
} else {
return null;
}
}
}
38 changes: 38 additions & 0 deletions src/Recipe/Parser/Instructions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace sylouuu\MarmitonCrawler\Recipe\Parser;

use PHPHtmlParser\Dom;

/**
* Instructions Parser
*
* @author sylouuu
* @link https://github.com/sylouuu/marmiton-crawler
* @version 0.1.0
* @license MIT
*/
class Instructions
{
/**
* Extract from DOM object
*
* @param object $dom
* @return string $instructions
*/
public static function parse ($dom)
{
$element = $dom->find('.m_content_recette_todo');

if ($element->innerHtml !== null) {
$instructions = $element->innerHtml;
$instructions = preg_replace('/<br(\s+)?\/?>/i', "\n", $instructions);
$instructions = strip_tags($instructions);
$instructions = explode(':', $instructions, 2);
$instructions = utf8_encode(trim($instructions[1]));

return $instructions;
} else {
return null;
}
}
}
32 changes: 32 additions & 0 deletions src/Recipe/Parser/PreparationTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace sylouuu\MarmitonCrawler\Recipe\Parser;

use PHPHtmlParser\Dom;

/**
* PreparationTime Parser
*
* @author sylouuu
* @link https://github.com/sylouuu/marmiton-crawler
* @version 0.1.0
* @license MIT
*/
class PreparationTime
{
/**
* Extract from DOM object
*
* @param object $dom
* @return int
*/
public static function parse ($dom)
{
$element = $dom->find('.m_content_recette_info span.preptime');

if ($element->text !== null) {
return intval(trim($element->text));
} else {
return null;
}
}
}
32 changes: 32 additions & 0 deletions src/Recipe/Parser/RecipeName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace sylouuu\MarmitonCrawler\Recipe\Parser;

use PHPHtmlParser\Dom;

/**
* RecipeName Parser
*
* @author sylouuu
* @link https://github.com/sylouuu/marmiton-crawler
* @version 0.1.0
* @license MIT
*/
class RecipeName
{
/**
* Extract from DOM object
*
* @param object $dom
* @return string
*/
public static function parse ($dom)
{
$element = $dom->find('h1.m_title span.item span.fn');

if ($element->text !== null) {
return utf8_encode(trim($element->text));
} else {
return null;
}
}
}
Loading

0 comments on commit 80e1fe3

Please sign in to comment.