Skip to content

Commit

Permalink
Improvements to Instagram Plugin
Browse files Browse the repository at this point in the history
Only get newests posts and only get friends every two days.
  • Loading branch information
gbrady92 authored and ginatrapani committed Apr 9, 2014
1 parent c596df2 commit cf2bf57
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 13 deletions.
3 changes: 1 addition & 2 deletions webapp/_lib/dao/class.OwnerInstanceMySQLDAO.php
Expand Up @@ -31,9 +31,8 @@
*/
class OwnerInstanceMySQLDAO extends PDODAO implements OwnerInstanceDAO {
/**
*
* Cached query results for doesOwnerHaveAccessToPost() reduces query load while looping through post results
* @var array $post_access_query_cache
* @var arr $post_access_query_cache
*/
static $post_access_query_cache = array();

Expand Down
Expand Up @@ -136,8 +136,7 @@ protected function processPageActions($options, Instagram\Auth $instagramAuth) {
$instagram_user_profile = $instagram->getCurrentUser()->getData();
$instagram_username = $instagram_user_profile->username;
$instagram_user_id = $instagram_user_profile->id;
$this->addSuccessMessage($this->saveAccessToken($instagram_user_id, $access_token, $instagram_username),
'authorization');
$this->saveAccessToken($instagram_user_id, $access_token, $instagram_username);
} else {
$error_msg = "Problem authorizing your instagram account! Please correct your plugin settings.";
$this->addErrorMessage($error_msg, 'authorization', true);
Expand Down Expand Up @@ -169,7 +168,7 @@ protected function saveAccessToken($instagram_user_id, $instagram_access_token,
"a different account, log out of Instagram in a different browser tab and try again.", 'user_add');
}
//set auth error to empty string
$owner_instance_dao->setAuthError($this->owner->id, $instance->id);
$owner_instance_dao->setAuthErrorByTokens($instance->id, $instagram_access_token, '');
} else { //Instance does not exist
$instance_dao->insert($instagram_user_id, $instagram_username, 'instagram');
$instance = $instance_dao->getByUserIdOnNetwork($instagram_user_id, 'instagram');
Expand Down
33 changes: 27 additions & 6 deletions webapp/plugins/instagram/model/PHP5.3/class.InstagramCrawler.php
Expand Up @@ -107,7 +107,6 @@ private function parseUserDetails(Instagram\User $details) {

$user_vals["post_count"] = $details->getMediaCount();
$user_vals["follower_count"] = $details->getFollowersCount();

$user_vals["user_name"] = $details->getUserName();
$user_vals["full_name"] = $details->getFullName();
$user_vals["user_id"] = $details->getId();
Expand All @@ -132,16 +131,38 @@ private function parseUserDetails(Instagram\User $details) {
* user's or pages archive of posts.
*/
public function fetchPostsAndReplies() {
$plugin_dao = DAOFactory::getDAO('PluginDAO');
$plugin_id = $plugin_dao->getPluginId('instagram');
$namespace = OptionDAO::PLUGIN_OPTIONS.'-'.$plugin_id;
$id = $this->instance->network_user_id;
$option_dao = DAOFactory::getDAO('OptionDAO');
$network = $this->instance->network;
// fetch user's friends
$this->storeFriends();
//Checks if last friends update is over 2 days ago and runs storeFriends if it is.
$friends_last_updated = $option_dao->getOptionByName($namespace,'last_crawled_friends');
$friends_last_updated_check = microtime(true) - 172800;
if($friends_last_updated == NULL) {
$this->storeFriends();
$option_dao->insertOption($namespace,'last_crawled_friends', microtime(true));
} elseif($friends_last_updated->option_value < $friends_last_updated_check) {
$this->storeFriends();
$option_dao->updateOptionByName($namespace,'last_crawled_friends', microtime(true));
}

$fetch_next_page = true;
$current_page_number = 1;
$api_param = array();
if($this->instance->total_posts_in_system !=0) {
$last_crawl = $this->instance->crawler_last_run;
$crawl_less_week = date($last_crawl, strtotime("-1 week"));
$unix_less_week = strtotime($crawl_less_week);
$api_param = array('min_timestamp' => $unix_less_week ,'count' => 20);

} else {
$api_param = array('count' => 20);
}

$this->logger->logUserInfo("About to request media",__METHOD__.','.__LINE__);
$posts = InstagramAPIAccessor::apiRequest('media', $id, $this->access_token, array('count' => 20));
$posts = InstagramAPIAccessor::apiRequest('media', $id, $this->access_token, $api_param);
$this->logger->logUserInfo("Media requested",__METHOD__.','.__LINE__);

//Cap crawl time for very busy pages with thousands of likes/comments
Expand All @@ -166,8 +187,8 @@ public function fetchPostsAndReplies() {
$this->processPosts($posts, $network, $current_page_number);

if ($posts->getNext() != null) {
$posts = InstagramAPIaccessor::apiRequest('media', $id, $this->access_token, array('count' => 20,
'max_id' => $posts->getNext()));
$api_param['max_id'] = $posts->getNext();
$posts = InstagramAPIaccessor::apiRequest('media', $id, $this->access_token,$api_param);
$current_page_number++;
} else {
$fetch_next_page = false;
Expand Down
67 changes: 65 additions & 2 deletions webapp/plugins/instagram/tests/TestOfInstagramCrawler.php
Expand Up @@ -80,6 +80,18 @@ public function setUp() {
'earliest_post_in_system'=>'2009-01-01 13:48:05', 'favorites_profile' => '0'
);
$this->profile2_instance = new Instance($r);

$r = array('id'=>5, 'network_username'=>'ni_ato', 'network_user_id'=>'502993749',
'network_viewer_id'=>'502993749', 'last_post_id'=>'0', 'last_page_fetched_replies'=>0,
'last_page_fetched_tweets'=>'0', 'total_posts_in_system'=>'7', 'total_replies_in_system'=>'0',
'total_follows_in_system'=>'0', 'is_archive_loaded_replies'=>'0',
'is_archive_loaded_follows'=>'0', 'crawler_last_run'=>'2014-01-01 13:48:05', 'earliest_reply_in_system'=>'',
'avg_replies_per_day'=>'2', 'is_public'=>'0', 'is_active'=>'0', 'network'=>'instagram',
'last_favorite_id' => '0', 'owner_favs_in_system' => '0', 'total_posts_by_owner'=>0,
'posts_per_day'=>1, 'posts_per_week'=>1, 'percentage_replies'=>50, 'percentage_links'=>50,
'earliest_post_in_system'=>'2009-01-01 13:48:05', 'favorites_profile' => '0'
);
$this->profile3_instance = new Instance($r);
}

public function tearDown() {
Expand Down Expand Up @@ -178,7 +190,6 @@ public function testFetchPostsAndRepliesForProfile1() {
$this->assertEqual($user->avatar, 'http://images.ak.instagram.com/profiles/anonymousUser.jpg');
$this->assertFalse($user->is_protected);
$this->assertEqual($user->network, 'instagram');

// Check the second photo was added, it has no comments and two likes
$post = $photo_dao->getPhoto('519642461157682352_494785218', 'instagram');
$this->assertEqual($post->post_id, '519642461157682352_494785218' );
Expand Down Expand Up @@ -232,4 +243,56 @@ public function testFetchPostsAndRepliesForProfile2Error() {
$this->expectException('Instagram\Core\ApiAuthException', 'The "access_token" provided is invalid.');
$ic->fetchPostsAndReplies();
}
}

public function testFetchNewestPosts() {
$user_dao = new UserMySQLDAO();
$photo_dao = new PhotoMySQLDAO();
$count_history_dao = new CountHistoryMySQLDAO();
$favorite_dao = new FavoritePostMySQLDAO();
$follow_dao = new FollowMySQLDAO();
$ic = new InstagramCrawler($this->profile3_instance, 'fauxaccesstoken', 120);

$config = Config::getInstance();
$instagram_crawler_log = $config->getValue('log_location');
// prepare log for reading after fetchPostsAndReplies.
$log_reader_handle = fopen($instagram_crawler_log, 'r');
fseek($log_reader_handle, 0, SEEK_END);
//Check if newest posts are returned.
$ic->fetchPostsAndReplies();

$post = $photo_dao->getPhoto('519671854563291086', 'instagram');
$this->assertEqual($post->post_id, '519671854563291086' );
$this->assertEqual($post->author_user_id, '502993749' );
$this->assertEqual($post->author_username, 'ni_ato' );
$this->assertEqual($post->author_fullname, 'niki' );
$avatar = 'http://images.ak.instagram.com/profiles/anonymousUser.jpg';
$this->assertEqual($post->author_avatar, $avatar);
$this->assertEqual($post->post_text, 'Epikinduna paixnidia');
$this->assertFalse($post->is_protected);
$this->assertEqual($post->pub_date, '2013-08-10 20:28:00');
$this->assertEqual($post->network, 'instagram');
$this->assertEqual($post->in_reply_to_user_id, '494785218');
$this->assertEqual($post->in_reply_to_post_id, '519644594447805677_494785218');
}

public function testFetchFriendsAfterTwoDays() {
$plugin_dao = new PluginMySQLDAO();
$plugin_id = $plugin_dao->getPluginId('instagram');
$namespace = OptionDAO::PLUGIN_OPTIONS.'-'.$plugin_id;
$option_dao = new OptionMySQLDAO();
$ic = new InstagramCrawler($this->profile3_instance, 'fauxaccesstoken', 120);
$ic->fetchPostsAndReplies();
//Checks to see if date value has been inserted into table after first crawl.
$select_insert = $option_dao->getOptionByName($namespace,'last_crawled_friends');
$this->assertNotNull($select_insert->option_value);
//Checks to see if date value hasn't changed after a crawl within two days of the last.
$ic->fetchPostsAndReplies();
$select_under_two_days = $option_dao->getOptionByName($namespace,'last_crawled_friends');
$this->assertEqual($select_insert->option_value, $select_under_two_days->option_value);
//Checks to see if date value has changed after a crawl 3 days after last crawl.
$option_dao->updateOptionByName($namespace,'last_crawled_friends', '1396566000');
$ic->fetchPostsAndReplies();
$select_over_two_days = $option_dao->getOptionByName($namespace,'last_crawled_friends');
$this->assertNotEqual($select_insert->option_value, $select_over_two_days->option_value);
}
}
Expand Up @@ -424,6 +424,7 @@ public function testConnectAccountThatAlreadyExists() {

$controller = new InstagramPluginConfigurationController($owner, 'instagram');
$output = $controller->go();
$this->debug($output);

$v_mgr = $controller->getViewManager();
$msgs = $v_mgr->getTemplateDataItem('success_msgs');
Expand Down
@@ -0,0 +1,145 @@
{
"pagination": {},
"meta": {
"code": 200
},
"data": [
{
"attribution": null,
"tags": [],
"type": "image",
"location": null,
"comments": {
"count": 1,
"data": [
{
"created_time": "1376169734",
"text": "Epikinduna paixnidia",
"from": {
"username": "ni_ato",
"profile_picture": "http://images.ak.instagram.com/profiles/anonymousUser.jpg",
"id": "502993749",
"full_name": "niki"
},
"id": "519671854563291086"
}
]
},
"filter": "Valencia",
"created_time": "1376166484",
"link": "http://instagram.com/p/c2JgFlg2zt/",
"likes": {
"count": 0,
"data": []
},
"images": {
"low_resolution": {
"url": "http://distilleryimage5.s3.amazonaws.com/5c3132b801fb11e38a2722000a9f1925_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distilleryimage5.s3.amazonaws.com/5c3132b801fb11e38a2722000a9f1925_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distilleryimage5.s3.amazonaws.com/5c3132b801fb11e38a2722000a9f1925_7.jpg",
"width": 612,
"height": 612
}
},
"users_in_photo": [],
"caption": {
"created_time": "1376166514",
"text": "Pwpw katapiesh...",
"from": {
"username": "afokou",
"profile_picture": "http://images.ak.instagram.com/profiles/anonymousUser.jpg",
"id": "494785218",
"full_name": "Aggeliki Fokou"
},
"id": "519644847053958719"
},
"user_has_liked": false,
"id": "519644594447805677_494785218",
"user": {
"username": "afokou",
"website": "",
"profile_picture": "http://images.ak.instagram.com/profiles/anonymousUser.jpg",
"full_name": "Aggeliki Fokou",
"bio": "",
"id": "494785218"
}
},
{
"attribution": null,
"tags": [],
"type": "image",
"location": null,
"comments": {
"count": 0,
"data": []
},
"filter": "X-Pro II",
"created_time": "1376166230",
"link": "http://instagram.com/p/c2JBCzg2yw/",
"likes": {
"count": 2,
"data": [
{
"username": "louk_as",
"profile_picture": "http://images.ak.instagram.com/profiles/profile_20065178_75sq_1335521050.jpg",
"id": "20065178",
"full_name": "lucas asimo"
},
{
"username": "ni_ato",
"profile_picture": "http://images.ak.instagram.com/profiles/anonymousUser.jpg",
"id": "502993749",
"full_name": "niki"
}
]
},
"images": {
"low_resolution": {
"url": "http://distilleryimage8.s3.amazonaws.com/c49be7f401fa11e3bcc122000a1fa49d_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distilleryimage8.s3.amazonaws.com/c49be7f401fa11e3bcc122000a1fa49d_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distilleryimage8.s3.amazonaws.com/c49be7f401fa11e3bcc122000a1fa49d_7.jpg",
"width": 612,
"height": 612
}
},
"users_in_photo": [],
"caption": {
"created_time": "1376166355",
"text": "H diaskedastiki mou arxi ;(",
"from": {
"username": "afokou",
"profile_picture": "http://images.ak.instagram.com/profiles/anonymousUser.jpg",
"id": "494785218",
"full_name": "Aggeliki Fokou"
},
"id": "519643515018505730"
},
"user_has_liked": false,
"id": "519642461157682352_494785218",
"user": {
"username": "afokou",
"website": "",
"profile_picture": "http://images.ak.instagram.com/profiles/anonymousUser.jpg",
"full_name": "Aggeliki Fokou",
"bio": "",
"id": "494785218"
}
}
]
}

0 comments on commit cf2bf57

Please sign in to comment.