-
Notifications
You must be signed in to change notification settings - Fork 0
/
FacebookExtractor.php
94 lines (86 loc) · 2.92 KB
/
FacebookExtractor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
require_once 'FbImage.php';
require_once "FacebookHelp.php";
require_once "EventPost.php";
require_once "BasePost.php";
require_once "exceptions/orientationexception.php";
require_once "exceptions/imageurlexception.php";
class FacebookExtractor {
private $fbHelp;
public function __construct($fbHelp) {
$this->fbHelp = $fbHelp;
$this->fbHelp->generateSession();
}
/*
* Parses a facebook feed as GraphEdge from the FacebookApi.
*
* @params GraphEdge $graphEdge is the feed page to be parsed.
*
* @return list of Post objects
*/
public function parseFeed($graphEdge) {
$postArray = array();
foreach($graphEdge as $key => $post) {
$postId = $post->getField('id');
$type;
$message = $post->getField('message');
$story = $post->getField('story');
$createdTime = $post->getField('created_time');
$attachmentsNode;
$imageArray = array();
if (($attachmentsNode = $this->fbHelp->requestGraphNode('/' . $postId . '?fields=attachments,type'))) {
$type = $attachmentsNode->getField('type');
$media = $attachmentsNode->getField('attachments', $default_string = "empty");
// collect attached media files
if ($media != "empty")
if (($submedia = $media[0]->getField('subattachments')) == true) { // post has multiple images
foreach ($submedia as $key1 => $mediaelem) {
$pictureLink = self::getPictureLink($mediaelem);
$orientation = self::calcOrientation($pictureLink);
try {
$imageArray[] = new FbImage($pictureLink, $orientation);
} catch (OrientationException | ImageUrlException $e) {
continue;
}
}
} else {
$pictureLink = self::getPictureLink($media[0]);
$orientation = self::calcOrientation($pictureLink);
try{
$imageArray[] = new FbImage($pictureLink, $orientation);
} catch (OrientationException | ImageUrlException $e) {
continue;
}
}
}
if ($type == 'event') {
$eventId = self::calcEventId($post->getField('id'));
$eventPost;
if (!($eventPost = $this->fbHelp->requestGraphNode('/' . $eventId)))
continue;
$postArray[] = new EventPost($postId, $eventId, $type, $message, $story, $imageArray, $createdTime,
$eventPost->getField('start_time'), $eventPost->getField('end_time'),
$eventPost->getField('place'), $eventPost->getField('name'));
} else {
$postArray[] = new BasePost($postId, $type, $message, $story, $imageArray, $createdTime);
}
}
return $postArray;
}
private function calcOrientation($imageurl) {
$imageDimension = getimagesize($imageurl);
if ($imageDimension[0] / $imageDimension[1] < 1)
return 'p';
elseif ($imageDimension[0] / $imageDimension[1] == 1)
return 's';
return 'l';
}
private function calcEventId($id) {
$eventId = preg_split("/_/", $id);
return $eventId[1];
}
private function getPictureLink($jsonNode) {
return $jsonNode->getField('media')->getField('image')->getField('src');
}
}
?>