Created
July 24, 2015 00:58
-
-
Save alexdzul/3f1e550c0c719f5d436e to your computer and use it in GitHub Desktop.
Pipeline para Twitter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""" | |
Pipeline para guardar información desde un login con Twitter. | |
Obtiene la imagen grande de su cuenta de Twitter. | |
"""" | |
__author__ = 'alex' | |
from urllib.request import urlopen | |
from django.core.files.base import ContentFile | |
from social.backends.twitter import TwitterOAuth | |
from .models import UserProfile | |
def update_user_social_data(strategy, *args, **kwargs): | |
"""Set the name and avatar for a user only if is new. | |
""" | |
if not kwargs['is_new']: | |
return | |
else: | |
backend = kwargs['backend'] | |
user = kwargs['user'] | |
print(kwargs) | |
print(kwargs['user']) | |
if isinstance(backend, TwitterOAuth): | |
if kwargs['response'].get('profile_image_url'): | |
image_name = 'tw_avatar_%s.jpg' % user.username | |
url = kwargs['response'].get('profile_image_url') | |
url_split = url.split("/") | |
url_image_name = url_split[len(url_split)-1] | |
del url_split[-1] | |
url_image_name_original = url_image_name.replace("_normal", "") # Agregamos la imagen original | |
new_url = "" | |
first_loop = True | |
for element in url_split: | |
if first_loop: | |
new_url += element | |
first_loop = False | |
else: | |
new_url += "/" + element | |
image_url = new_url + "/" + url_image_name_original | |
user.first_name = kwargs['details'].get('first_name') | |
user.last_name = kwargs['details'].get('last_name') | |
user.save() | |
image_stream = urlopen(image_url) | |
user_profile = UserProfile() | |
user_profile.user = user | |
user_profile.image.save(image_name, ContentFile(image_stream.read())) | |
user_profile.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment