Skip to content

Commit 397ebb2

Browse files
feat(hackathon): Add Scraping Functionality
1 parent 7e4cd35 commit 397ebb2

File tree

5 files changed

+371
-112
lines changed

5 files changed

+371
-112
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
7+
use App\Http\Requests;
8+
use Goutte\Client;
9+
use App\Http\Controllers\Controller;
10+
11+
class WebScrapingController extends Controller
12+
{
13+
protected $crawler;
14+
15+
/**
16+
* [__construct description]
17+
*/
18+
public function __construct()
19+
{
20+
$this->client = new Client();
21+
}
22+
23+
/**
24+
* Return all data to the Stripe API dashboard
25+
* @return mixed
26+
*/
27+
public function getPage()
28+
{
29+
$links = $this->getData('https://news.ycombinator.com/');
30+
31+
return view('api.scraping')->withLinks($links);
32+
}
33+
34+
/**
35+
* Scrape the Links
36+
* @param $siteToCrawl
37+
* @return array
38+
*/
39+
public function getData($siteToCrawl)
40+
{
41+
$crawler = $this->client->request('GET', $siteToCrawl);
42+
43+
$arr = $crawler->filter('.title a[href^="http"], a[href^="https"]')->each(function($element) {
44+
$links = [];
45+
46+
array_push($links, $element->text());
47+
48+
return $links;
49+
});
50+
51+
return $arr;
52+
}
53+
}

app/Http/routes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@
8686
'middleware' => ['auth']
8787
]);
8888

89+
Route::get('/api/scraping', [
90+
'uses' => 'WebScrapingController@getPage',
91+
'as' => 'api.scraping',
92+
'middleware' => ['auth']
93+
]);
94+
8995
Route::post('/tweet/new', [
9096
'uses' => 'TwitterController@sendTweet',
9197
'as' => 'tweet.new',

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"graham-campbell/github": "^4.2",
1515
"thujohn/twitter": "^2.1",
1616
"metalmatze/lastfm-api-wrapper": "dev-master",
17-
"aloha/twilio": "^2.0"
17+
"aloha/twilio": "^2.0",
18+
"fabpot/goutte": "^3.1",
19+
"codeguy/arachnid": "^1.0"
1820
},
1921
"require-dev": {
2022
"fzaninotto/faker": "~1.4",

0 commit comments

Comments
 (0)