-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8631073
Showing
9 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"require": { | ||
"facebook/graph-sdk": "^5.6" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"DieSchittigs\\StarScraper\\": "src/" | ||
} | ||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use DieSchittigs\StarScraper\StarRating; | ||
use DieSchittigs\StarScraper\GooglePlaceProvider; | ||
use DieSchittigs\StarScraper\FacebookPageProvider; | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
$starRating = new StarRating(); | ||
$starRating->addProvider( | ||
new GooglePlaceProvider( | ||
'{{GoogleApiKey}}', | ||
'{{GoogleMapsPlaceID}}' | ||
) | ||
); | ||
$starRating->addProvider( | ||
new FacebookPageProvider( | ||
'{{FacebookAppID}}', | ||
'{{FacebookAppSecret}}', | ||
'{{FacebookPage}}' | ||
) | ||
); | ||
|
||
$rating = $starRating->getRating(); | ||
|
||
echo ' | ||
"aggregateRating": { | ||
"@type": "AggregateRating", | ||
"bestRating": "'. $rating->bestRating .'", | ||
"ratingCount": "'. $rating->ratingCount .'", | ||
"ratingValue": "'. $rating->ratingValue .'" | ||
} | ||
'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace DieSchittigs\StarScraper; | ||
|
||
use Facebook\Facebook; | ||
|
||
class FacebookPageProvider implements iRatingProvider{ | ||
private $BEST_RATING = 5; | ||
private $result; | ||
|
||
public function __construct($appID, $appSecret, $pageId){ | ||
$fb = new Facebook([ | ||
'app_id' => $appID, | ||
'app_secret' => $appSecret, | ||
'default_graph_version' => 'v2.10', | ||
'default_access_token' => "$appID|$appSecret" | ||
]); | ||
try { | ||
$response = $fb->get("$pageId?fields=overall_star_rating,rating_count"); | ||
$this->result = $response->getDecodedBody(); | ||
} catch(\Facebook\Exceptions\FacebookResponseException $e) { | ||
echo 'Graph returned an error: ' . $e->getMessage(); | ||
} catch(\Facebook\Exceptions\FacebookSDKException $e) { | ||
echo 'Facebook SDK returned an error: ' . $e->getMessage(); | ||
} | ||
} | ||
public function getRating(){ | ||
if( | ||
!$this->result || | ||
!$this->result['id'] || | ||
!$this->result['rating_count'] || | ||
!$this->result['overall_star_rating'] | ||
) return null; | ||
$rating = new Rating(); | ||
$rating->bestRating = $this->BEST_RATING; | ||
$rating->ratingCount = $this->result['rating_count']; | ||
$rating->ratingValue = $this->result['overall_star_rating']; | ||
return $rating; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace DieSchittigs\StarScraper; | ||
|
||
class GooglePlaceProvider implements iRatingProvider{ | ||
private $reviews; | ||
private $BEST_RATING = 5; | ||
|
||
public function __construct($GoogleApiKey, $GoogleMapsPlaceID){ | ||
$placeReviews = []; | ||
try{ | ||
$placeDetails = json_decode( | ||
file_get_contents("https://maps.googleapis.com/maps/api/place/details/json?placeid=$GoogleMapsPlaceID&key=$GoogleApiKey") | ||
); | ||
$placeReviews = $placeDetails->result->reviews; | ||
} catch (\Exception $e){ | ||
echo 'Google API returned an error: ' . $e->getMessage(); | ||
} | ||
if($placeReviews && is_array($placeReviews) && !empty($placeReviews)){ | ||
$this->reviews = $placeReviews; | ||
} | ||
} | ||
public function getRating(){ | ||
if(!$this->reviews) return null; | ||
$rating = new Rating(); | ||
$rating->bestRating = $this->BEST_RATING; | ||
$rating->ratingCount = count($this->reviews); | ||
$score = 0; | ||
foreach($this->reviews as $review){ | ||
$score += $review->rating; | ||
} | ||
$rating->ratingValue = $score / $rating->ratingCount; | ||
return $rating; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace DieSchittigs\StarScraper; | ||
|
||
class Rating{ | ||
public $bestRating = 0; | ||
public $ratingCount = 0; | ||
public $ratingValue = 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace DieSchittigs\StarScraper; | ||
|
||
class StarRating implements iRatingProvider{ | ||
private $providers = []; | ||
private $best_rating; | ||
public function __construct($best_rating = 5){ | ||
$this->best_rating = $best_rating; | ||
} | ||
public function addProvider(iRatingProvider $provider){ | ||
$this->providers[] = $provider; | ||
} | ||
public function getRating(){ | ||
if(!$this->providers) return null; | ||
$rating = new Rating; | ||
$rating->bestRating = $this->best_rating; | ||
$rating->ratingCount = 0; | ||
$score = 0; | ||
$validProviders = 0; | ||
foreach($this->providers as $provider){ | ||
$_rating = $provider->getRating(); | ||
if(!$_rating) continue; | ||
$rating->ratingCount += $_rating->ratingCount; | ||
$score += $_rating->ratingValue / $_rating->bestRating * $rating->bestRating; | ||
$validProviders ++; | ||
} | ||
$rating->ratingValue = $score / $validProviders; | ||
return $rating; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace DieSchittigs\StarScraper; | ||
|
||
interface iRatingProvider{ | ||
public function getRating(); | ||
} |