Skip to content

Commit 762ac5f

Browse files
committed
Merge pull request #45 from thenextweb/master
Added attribute publishedTime to the response
2 parents 7aee0a7 + 293ca7a commit 762ac5f

File tree

9 files changed

+123
-2
lines changed

9 files changed

+123
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ $info->providerName; //The provider name of the page (youtube, twitter, instagra
5151
$info->providerUrl; //The provider url
5252
$info->providerIcons; //All provider icons found in the page
5353
$info->providerIcon; //The icon choosen as main icon
54+
55+
$info->publishedTime; //The publication date of the article, if present
5456
```
5557

5658
Available options

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "embed/embed",
2+
"name": "thenextweb/embed",
33
"type": "library",
44
"description": "PHP library to retrieve page info using oembed, opengraph, etc",
55
"keywords": ["oembed", "opengraph", "twitter cards", "embed", "embedly"],

src/Adapters/Adapter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,12 @@ public function getHeight()
364364
{
365365
return $this->getFromProviders('height');
366366
}
367+
368+
/**
369+
* {@inheritDoc}
370+
*/
371+
public function getPublishedTime()
372+
{
373+
return $this->getFromProviders('publishedTime');
374+
}
367375
}

src/Adapters/AdapterInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,11 @@ public function getHeight();
163163
* @return float|null
164164
*/
165165
public function getAspectRatio();
166+
167+
/**
168+
* Gets the published time, if the webpage is an article
169+
*
170+
* @return string|null
171+
*/
172+
public function getPublishedTime ();
166173
}

src/Adapters/Webpage.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ protected function initProviders(Request $request)
2828
'Html' => new Providers\Html($request),
2929
'TwitterCards' => new Providers\TwitterCards($request),
3030
'OpenGraph' => new Providers\OpenGraph($request),
31+
'Sailthru' => new Providers\Sailthru($request),
3132
);
3233

3334
if ($this->options['facebookProvider']) {

src/Providers/Html.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public function __construct(Request $request)
9191
$icons[] = $meta->getAttribute('content');
9292
continue 2;
9393

94+
case 'pub_date':
95+
case 'date':
96+
$this->set('datepublished', $meta->getAttribute('content'));
97+
continue 2;
98+
9499
default:
95100
if ($meta->hasAttribute('content')) {
96101
$this->set($name, $meta->getAttribute('content'));
@@ -99,12 +104,27 @@ public function __construct(Request $request)
99104
}
100105
}
101106

107+
if ($meta->hasAttribute('itemprop')) {
108+
$name = strtolower($meta->getAttribute('itemprop'));
109+
$this->set($name, $meta->getAttribute('content'));
110+
}
111+
102112
if ($meta->hasAttribute('http-equiv') && $meta->hasAttribute('content')) {
103113
$name = strtolower($meta->getAttribute('http-equiv'));
104114
$this->set($name, $meta->getAttribute('content'));
105115
}
106116
}
107117

118+
//Time
119+
foreach($html->getElementsByTagName('time') as $time) {
120+
if ($time->hasAttribute('itemprop')) {
121+
$name = strtolower($time->getAttribute('itemprop'));
122+
$this->set($name, $time->getAttribute('datetime'));
123+
} else if ($time->hasAttribute('datetime')) {
124+
$this->set('datepublished', $time->getAttribute('datetime'));
125+
}
126+
}
127+
108128
//Search the main element:
109129
$content = $html->getElementsByTagName('main'); //<main> element
110130

@@ -280,4 +300,14 @@ public function getHeight()
280300
{
281301
return $this->get('video_height');
282302
}
303+
304+
/**
305+
* Gets the article publication date
306+
*
307+
* @return string|null
308+
*/
309+
public function getPublishedTime()
310+
{
311+
return $this->get('datepublished');
312+
}
283313
}

src/Providers/OpenGraph.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@ public function __construct(Request $request)
2525

2626
foreach ($html->getElementsByTagName('meta') as $meta) {
2727
if (strpos($meta->getAttribute('property'), 'og:') === 0) {
28-
$name = substr($meta->getAttribute('property'), 3);
28+
if (strpos($meta->getAttribute('property'), 'og:article:') === 0) {
29+
$name = substr($meta->getAttribute('property'), 11);
30+
} else {
31+
$name = substr($meta->getAttribute('property'), 3);
32+
}
2933
} elseif (strpos($meta->getAttribute('name'), 'og:') === 0) {
3034
$name = substr($meta->getAttribute('name'), 3);
35+
} elseif (strpos($meta->getAttribute('property'), 'article:') === 0) {
36+
$name = substr($meta->getAttribute('property'), 8);
3137
} else {
3238
continue;
3339
}
@@ -179,4 +185,14 @@ public function getHeight()
179185
{
180186
return $this->get('image:height') ?: $this->get('video:height');
181187
}
188+
189+
/**
190+
* Gets the article publication date
191+
*
192+
* @return string|null
193+
*/
194+
public function getPublishedTime()
195+
{
196+
return $this->get('published_time');
197+
}
182198
}

src/Providers/Sailthru.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Generic Salithru provider.
4+
* Load the Salithru data of an url and store it
5+
*/
6+
namespace Embed\Providers;
7+
8+
use Embed\Request;
9+
10+
class Sailthru extends Provider
11+
{
12+
/**
13+
* Constructor
14+
*
15+
* @param Request $request
16+
*/
17+
public function __construct(Request $request)
18+
{
19+
if (!($html = $request->getHtmlContent())) {
20+
return false;
21+
}
22+
23+
foreach ($html->getElementsByTagName('meta') as $meta) {
24+
if ($meta->hasAttribute('name') && (stripos($meta->getAttribute('name'), 'sailthru.') === 0)) {
25+
$this->set(strtolower(substr($meta->getAttribute('name'), 9)), $meta->getAttribute('content'));
26+
}
27+
}
28+
}
29+
30+
/**
31+
* Gets the article publication date
32+
*
33+
* @return string|null
34+
*/
35+
public function getPublishedTime()
36+
{
37+
return $this->get('date');
38+
}
39+
}

tests/ArticleTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
include_once dirname(__DIR__).'/src/autoloader.php';
3+
4+
class ArticleTest extends PHPUnit_Framework_TestCase
5+
{
6+
public function testOne()
7+
{
8+
$info = Embed\Embed::create('http://mashable.com/2015/01/15/booking-now-app/');
9+
10+
$this->assertEquals($info->title, 'Booking Now app offers hotel reservations in \'just two taps\'');
11+
$this->assertEquals($info->description, 'Last-minute accommodations are all the rage in 2015.');
12+
$this->assertEquals($info->type, 'link');
13+
$this->assertEquals($info->image, 'http://rack.2.mshcdn.com/media/ZgkyMDE1LzAxLzE1LzRlL2hvdGVscm9vbS42YmIyOC5qcGcKcAl0aHVtYgkxMjAweDYyNyMKZQlqcGc/6d24e110/098/hotel-room.jpg');
14+
$this->assertEquals($info->providerName, 'Mashable');
15+
$this->assertEquals($info->providerUrl, 'http://mashable.com');
16+
$this->assertEquals($info->publishedTime, '2015-01-15T13:31:13Z');
17+
}
18+
}

0 commit comments

Comments
 (0)