def getContextAvatar(context, request): """ Get context avatar To the date, this is only implemented to work integrated with Twitter. """ chash = context['hash'] twitter_username = context['twitterUsername'] base_folder = request.registry.settings.get('avatar_folder') avatar_folder = get_avatar_folder(base_folder, 'contexts', chash) context_image_filename = '%s/%s' % (avatar_folder, chash) api = get_twitter_api(request.registry) if not os.path.exists(context_image_filename): download_twitter_user_image(api, twitter_username, context_image_filename) if os.path.exists(context_image_filename): # Calculate time since last download and set if we have to re-download or not modification_time = os.path.getmtime(context_image_filename) hours_since_last_modification = (time.time() - modification_time) / 60 / 60 if hours_since_last_modification > 3: download_twitter_user_image(api, twitter_username, context_image_filename) else: context_image_filename = '{}/missing-context.png'.format(base_folder) data = open(context_image_filename).read() image = Response(data, status_int=200) image.content_type = 'image/png' return image
def test_get_twitter_api_no_image_url(self): """ Given a tweepy api without user image property When i try to get the user image I get no download """ from max.utils.twitter import download_twitter_user_image api = MockTweepyAPI(None, provide_image=False) imagefile = os.path.join(self.tempfolder, 'twitter.png') download_twitter_user_image(api, 'maxupcnet', imagefile) self.assertFileNotExists(imagefile)
def test_get_twitter_avatar(self): """ Given a valid cloudapis settings When i try to get a twitter user Then get a user object And the expected properties are there """ from max.utils.twitter import get_twitter_api from max.utils.twitter import download_twitter_user_image api = get_twitter_api(self.app.registry) imagefile = os.path.join(self.tempfolder, 'twitter.png') download_twitter_user_image(api, 'maxupcnet', imagefile) self.assertFileExists(imagefile)
def test_get_twitter_api_failing_download(self): """ Given a tweepy api with user image property And a url that fails for some unknown reason When i try to get the user image I get no download """ from max.utils.twitter import download_twitter_user_image avatar_image = os.path.join(self.conf_dir, "avatar.png") http_mock_twitter_user_image(avatar_image, status=500) api = MockTweepyAPI(None, provide_image=True) imagefile = os.path.join(self.tempfolder, 'twitter.png') download_twitter_user_image(api, 'maxupcnet', imagefile) self.assertFileNotExists(imagefile)