Twitter Import improvements. Still buggy?
Apparently mrvdb has problems with duplicate inserts and missing files when unlinking. It could be due to coding, or it could be due to parallelizing.
This commit is contained in:
parent
cd6fa512ac
commit
fb4e9b234d
|
@ -89,16 +89,13 @@ class Avatar extends Managed_DataObject
|
||||||
if (!$avatar->find(true)) {
|
if (!$avatar->find(true)) {
|
||||||
throw new NoResultException($avatar);
|
throw new NoResultException($avatar);
|
||||||
}
|
}
|
||||||
return $avatar;
|
if (!file_exists(Avatar::path($avatar->filename))) {
|
||||||
}
|
// The delete call may be odd for, say, unmounted filesystems
|
||||||
|
// that cause a file to currently not exist, but actually it does...
|
||||||
public static function hasUploaded(Profile $profile) {
|
$avatar->delete();
|
||||||
try {
|
throw new FileNotFoundException(Avatar::path($avatar->filename));
|
||||||
$avatar = Avatar::getUploaded($profile);
|
|
||||||
} catch (NoResultException $e) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return file_exists(Avatar::path($avatar->filename));
|
return $avatar;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getProfileAvatars(Profile $target) {
|
public static function getProfileAvatars(Profile $target) {
|
||||||
|
|
41
lib/filenotfoundexception.php
Normal file
41
lib/filenotfoundexception.php
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* StatusNet, the distributed open-source microblogging tool
|
||||||
|
*
|
||||||
|
* Class for an exception when a database lookup returns no results
|
||||||
|
*
|
||||||
|
* PHP version 5
|
||||||
|
*
|
||||||
|
* LICENCE: This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* @category Exception
|
||||||
|
* @package GNUSocial
|
||||||
|
* @author Mikael Nordfeldth <mmn@hethane.se>
|
||||||
|
* @copyright 2013 Free Software Foundation, Inc.
|
||||||
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
|
||||||
|
* @link http://www.gnu.org/software/social/
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!defined('GNUSOCIAL')) { exit(1); }
|
||||||
|
|
||||||
|
class FileNotFoundException extends ServerException
|
||||||
|
{
|
||||||
|
public $path = null;
|
||||||
|
|
||||||
|
public function __construct($path)
|
||||||
|
{
|
||||||
|
$this->path = $path;
|
||||||
|
parent::__construct(_('File not found in filesystem.'), 404);
|
||||||
|
}
|
||||||
|
}
|
|
@ -364,7 +364,7 @@ class TwitterBridgePlugin extends Plugin
|
||||||
|
|
||||||
// For saving the last-synched status of various timelines
|
// For saving the last-synched status of various timelines
|
||||||
// home_timeline, messages (in), messages (out), ...
|
// home_timeline, messages (in), messages (out), ...
|
||||||
$schema->ensureTable('twitter_synch_status', Twitter_sync_status::schemaDef());
|
$schema->ensureTable('twitter_synch_status', Twitter_synch_status::schemaDef());
|
||||||
|
|
||||||
// For storing user-submitted flags on profiles
|
// For storing user-submitted flags on profiles
|
||||||
$schema->ensureTable('notice_to_status', Notice_to_status::schemaDef());
|
$schema->ensureTable('notice_to_status', Notice_to_status::schemaDef());
|
||||||
|
|
|
@ -31,9 +31,7 @@ Batch script for synching local friends with Twitter friends.
|
||||||
END_OF_TRIM_HELP;
|
END_OF_TRIM_HELP;
|
||||||
|
|
||||||
require_once INSTALLDIR . '/scripts/commandline.inc';
|
require_once INSTALLDIR . '/scripts/commandline.inc';
|
||||||
require_once INSTALLDIR . '/lib/parallelizingdaemon.php';
|
|
||||||
require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
|
require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
|
||||||
require_once INSTALLDIR . '/plugins/TwitterBridge/twitteroauthclient.php';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Daemon to sync local friends with Twitter friends
|
* Daemon to sync local friends with Twitter friends
|
||||||
|
|
|
@ -40,7 +40,6 @@ require_once INSTALLDIR . '/scripts/commandline.inc';
|
||||||
require_once INSTALLDIR . '/lib/common.php';
|
require_once INSTALLDIR . '/lib/common.php';
|
||||||
require_once INSTALLDIR . '/lib/daemon.php';
|
require_once INSTALLDIR . '/lib/daemon.php';
|
||||||
require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
|
require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
|
||||||
require_once INSTALLDIR . '/plugins/TwitterBridge/lib/twitteroauthclient.php';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch statuses from Twitter
|
* Fetch statuses from Twitter
|
||||||
|
|
|
@ -230,44 +230,19 @@ class TwitterImport
|
||||||
$profile->profileurl = $profileurl;
|
$profile->profileurl = $profileurl;
|
||||||
$profile->limit(1);
|
$profile->limit(1);
|
||||||
|
|
||||||
if (!$profile->find()) {
|
if (!$profile->find(true)) {
|
||||||
throw new NoResultException($profile);
|
throw new NoResultException($profile);
|
||||||
}
|
}
|
||||||
|
|
||||||
$profile->fetch();
|
|
||||||
return $profile;
|
return $profile;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Check to see if this Twitter status has already been imported
|
|
||||||
*
|
|
||||||
* @param Profile $profile Twitter user's local profile
|
|
||||||
* @param string $statusUri URI of the status on Twitter
|
|
||||||
*
|
|
||||||
* @return mixed value a matching Notice or null
|
|
||||||
*/
|
|
||||||
function checkDupe($profile, $statusUri)
|
|
||||||
{
|
|
||||||
$notice = new Notice();
|
|
||||||
$notice->uri = $statusUri;
|
|
||||||
$notice->profile_id = $profile->id;
|
|
||||||
$notice->limit(1);
|
|
||||||
|
|
||||||
if ($notice->find()) {
|
|
||||||
$notice->fetch();
|
|
||||||
return $notice;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function ensureProfile($twuser)
|
protected function ensureProfile($twuser)
|
||||||
{
|
{
|
||||||
// check to see if there's already a profile for this user
|
// check to see if there's already a profile for this user
|
||||||
$profileurl = 'http://twitter.com/' . $twuser->screen_name;
|
$profileurl = 'http://twitter.com/' . $twuser->screen_name;
|
||||||
try {
|
try {
|
||||||
$profile = $this->getProfileByUrl($twuser->screen_name, $profileurl);
|
$profile = $this->getProfileByUrl($twuser->screen_name, $profileurl);
|
||||||
$this->checkAvatar($twuser, $profile);
|
$this->updateAvatar($twuser, $profile);
|
||||||
return $profile;
|
return $profile;
|
||||||
} catch (NoResultException $e) {
|
} catch (NoResultException $e) {
|
||||||
common_debug(__METHOD__ . ' - Adding profile and remote profile ' .
|
common_debug(__METHOD__ . ' - Adding profile and remote profile ' .
|
||||||
|
@ -323,29 +298,11 @@ class TwitterImport
|
||||||
return $profile;
|
return $profile;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function checkAvatar($twuser, Profile $profile)
|
/*
|
||||||
{
|
* Checks whether we have to update the profile's avatar
|
||||||
$path_parts = pathinfo($twuser->profile_image_url);
|
*
|
||||||
$ext = isset($path_parts['extension'])
|
* @return true when updated, false on failure, null when no action taken
|
||||||
? '.'.$path_parts['extension']
|
*/
|
||||||
: ''; // some lack extension
|
|
||||||
$img_root = basename($path_parts['basename'], '_normal'.$ext); // cut off extension
|
|
||||||
$newname = "Twitter_{$twuser->id}_{$img_root}_{$this->avatarsizename}{$ext}";
|
|
||||||
|
|
||||||
try {
|
|
||||||
$avatar = Avatar::getUploaded($profile);
|
|
||||||
$oldname = $avatar->filename;
|
|
||||||
$avatar->free();
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$oldname = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($newname != $oldname || !Avatar::hasUploaded($profile)) {
|
|
||||||
common_debug(__METHOD__ . " - Avatar for {$profile->nickname} has changed.");
|
|
||||||
$this->updateAvatar($twuser, $profile);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function updateAvatar($twuser, Profile $profile)
|
protected function updateAvatar($twuser, Profile $profile)
|
||||||
{
|
{
|
||||||
$path_parts = pathinfo($twuser->profile_image_url);
|
$path_parts = pathinfo($twuser->profile_image_url);
|
||||||
|
@ -353,17 +310,33 @@ class TwitterImport
|
||||||
? '.'.$path_parts['extension']
|
? '.'.$path_parts['extension']
|
||||||
: ''; // some lack extension
|
: ''; // some lack extension
|
||||||
$img_root = basename($path_parts['basename'], '_normal'.$ext); // cut off extension
|
$img_root = basename($path_parts['basename'], '_normal'.$ext); // cut off extension
|
||||||
$url = "{$path_parts['dirname']}/{$img_root}_{$this->avatarsizename}{$ext}";
|
|
||||||
$filename = "Twitter_{$twuser->id}_{$img_root}_{$this->avatarsizename}{$ext}";
|
$filename = "Twitter_{$twuser->id}_{$img_root}_{$this->avatarsizename}{$ext}";
|
||||||
$mediatype = $this->getMediatype(substr($ext, 1));
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
$avatar = Avatar::getUploaded($profile);
|
||||||
|
if ($avatar->filename === $filename) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// else we continue with creating a new avatar
|
||||||
|
} catch (Exception $e) {
|
||||||
|
// Avatar was not found. We can catch NoResultException or FileNotFoundException
|
||||||
|
// but generally we just want to continue creating a new avatar.
|
||||||
|
}
|
||||||
|
|
||||||
|
$url = "{$path_parts['dirname']}/{$img_root}_{$this->avatarsizename}{$ext}";
|
||||||
|
$mediatype = $this->getMediatype(mb_substr($ext, 1));
|
||||||
|
|
||||||
|
try {
|
||||||
|
common_debug(__METHOD__ . " - Updating profile avatar (profile_id={$profile->id} to {$filename}");
|
||||||
$this->newAvatar($profile, $url, $filename, $mediatype);
|
$this->newAvatar($profile, $url, $filename, $mediatype);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
if (file_exists(Avatar::path($filename))) {
|
if (file_exists(Avatar::path($filename))) {
|
||||||
unlink(Avatar::path($filename));
|
unlink(Avatar::path($filename));
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getMediatype($ext)
|
protected function getMediatype($ext)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user