Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
saibotd committed Oct 21, 2017
0 parents commit 8631073
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
11 changes: 11 additions & 0 deletions composer.json
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/"
}
}

}
77 changes: 77 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions index.php
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 .'"
}
';
41 changes: 41 additions & 0 deletions src/FacebookPageProvider.php
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;
}
}

36 changes: 36 additions & 0 deletions src/GooglePlaceProvider.php
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;
}
}

9 changes: 9 additions & 0 deletions src/Rating.php
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;
}
31 changes: 31 additions & 0 deletions src/StarRating.php
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;
}
}
7 changes: 7 additions & 0 deletions src/iRatingProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace DieSchittigs\StarScraper;

interface iRatingProvider{
public function getRating();
}

0 comments on commit 8631073

Please sign in to comment.