def account_for_tumblr_userinfo(userinfo, person=None): username = userinfo['name'] try: account = Account.objects.get(service='tumblr.com', ident=username) except Account.DoesNotExist: pass else: person = account.person if not person.avatar_source or person.avatar_source == 'tumblr.com': (primary_blog, ) = [ blog for blog in userinfo['blogs'] if blog.get('primary', False) ] tumblr_avatar_url = 'http://api.tumblr.com/v2/blog/%s.tumblr.com/avatar/64' % primary_blog[ 'name'] if not person.avatar or person.avatar.image_url != tumblr_avatar_url: avatar = Media( image_url=tumblr_avatar_url, width=64, height=64, ) avatar.save() person.avatar = avatar person.avatar_source = 'tumblr.com' person.save() elif not person.avatar_source: person.avatar_source = 'tumblr.com' person.save() return account (primary_blog, ) = [ blog for blog in userinfo['blogs'] if blog.get('primary', False) ] display_name = primary_blog.get('title', username) if person is None: tumblr_avatar_url = 'http://api.tumblr.com/v2/blog/%s.tumblr.com/avatar/64' % primary_blog[ 'name'] avatar = Media( image_url=tumblr_avatar_url, width=64, height=64, ) avatar.save() person = Person( display_name=display_name, permalink_url=primary_blog['url'], avatar=avatar, ) person.save() account = Account( service='tumblr.com', ident=account_name, display_name=display_name, person=person, ) account.save() return account
def account_for_facebook_user(fb_user, person=None): try: account = Account.objects.get(service='facebook.com', ident=fb_user["id"]) except Account.DoesNotExist: if person is None: avatar = Media( width=50, height=50, image_url='http://graph.facebook.com/%s/picture' % fb_user["id"] ) avatar.save() person = Person( display_name=fb_user["name"], avatar=avatar, permalink_url=fb_user["link"], ) person.save() account = Account( service='facebook.com', ident=fb_user["id"], display_name=fb_user["name"], person=person, ) account.save() return account
def account_for_mlkshk_userinfo(userinfo, person=None): account_id = str(userinfo['id']) try: return Account.objects.get(service='mlkshk.com', ident=account_id) except Account.DoesNotExist: pass username = userinfo['name'] if person is None: # TODO: use mlkshk profile images when we get stabler urls for them person = Person( display_name=username, permalink_url='http://mlkshk.com/user/%s' % username, ) person.save() account = Account( service='mlkshk.com', ident=account_id, display_name=username, person=person, ) account.save() return account
def account_for_embed_resource(resource): url = resource.get('author_url') if url is None: return None try: return Account.objects.get(service='', ident=url) except Account.DoesNotExist: pass if resource.get('author_name') is None: author_url_parts = urlparse(url) author_name = '/'.join((author_url_parts.netloc, author_url_parts.path)) else: author_name = resource['author_name'] # TODO: go find an avatar by way of ~~=<( MAGIC )>=~~ person = Person( display_name=author_name, ) person.save() acc = Account( service='', ident=url, display_name=author_name, person=person, ) acc.save() return acc
def account_for_embed_resource(resource): url = resource.get('author_url') if url is None: return None try: return Account.objects.get(service='', ident=url) except Account.DoesNotExist: pass if resource.get('author_name') is None: author_url_parts = urlparse(url) author_name = '/'.join( (author_url_parts.netloc, author_url_parts.path)) else: author_name = resource['author_name'] # TODO: go find an avatar by way of ~~=<( MAGIC )>=~~ person = Person(display_name=author_name, ) person.save() acc = Account( service='', ident=url, display_name=author_name, person=person, ) acc.save() return acc
def account_for_tumblr_shortname(shortname): try: # We don't have any data to update the account with, so whatever we have is fine. return Account.objects.get(service='tumblr.com', ident=shortname) except Account.DoesNotExist: pass csr = oauth.Consumer(*settings.TUMBLR_CONSUMER) url = 'http://api.tumblr.com/v2/blog/%s.tumblr.com/info?api_key=%s' % (shortname, csr.key) http = httplib2.Http() resp, cont = http.request(url) if resp.status == 500: log.info("Server error fetching tumblr info %s (is Tumblr down?)", account.ident) return if resp.status == 408: log.info("Timeout fetching tumblr info %s (is Tumblr down/slow?)", account.ident) return if resp.status == 401: log.info("401 Unauthorized fetching tumblr info %s (maybe suspended?)", account.ident) return if resp.status == 403: raise ValueError("403 Forbidden fetching tumblr info %s\n\n%s" % (account.ident, cont)) if resp.status != 200: raise ValueError("Unexpected HTTP response %d %s fetching tumblr info %s" % (resp.status, resp.reason, account.ident)) data = json.loads(cont) blogdata = data['response']['blog'] display_name = blogdata['title'] tumblr_avatar_url = 'http://api.tumblr.com/v2/blog/%s.tumblr.com/avatar/64' % shortname avatar = Media( image_url=tumblr_avatar_url, width=64, height=64, ) avatar.save() person = Person( display_name=display_name, permalink_url=blogdata['url'], avatar=avatar, ) person.save() account = Account( service='tumblr.com', ident=shortname, display_name=blogdata['title'], person=person, ) account.save() return account
def account_for_tumblr_userinfo(userinfo, person=None): username = userinfo['name'] try: account = Account.objects.get(service='tumblr.com', ident=username) except Account.DoesNotExist: pass else: person = account.person if not person.avatar_source or person.avatar_source == 'tumblr.com': (primary_blog,) = [blog for blog in userinfo['blogs'] if blog.get('primary', False)] tumblr_avatar_url = 'http://api.tumblr.com/v2/blog/%s.tumblr.com/avatar/64' % primary_blog['name'] if not person.avatar or person.avatar.image_url != tumblr_avatar_url: avatar = Media( image_url=tumblr_avatar_url, width=64, height=64, ) avatar.save() person.avatar = avatar person.avatar_source = 'tumblr.com' person.save() elif not person.avatar_source: person.avatar_source = 'tumblr.com' person.save() return account (primary_blog,) = [blog for blog in userinfo['blogs'] if blog.get('primary', False)] display_name = primary_blog.get('title', username) if person is None: tumblr_avatar_url = 'http://api.tumblr.com/v2/blog/%s.tumblr.com/avatar/64' % primary_blog['name'] avatar = Media( image_url=tumblr_avatar_url, width=64, height=64, ) avatar.save() person = Person( display_name=display_name, permalink_url=primary_blog['url'], avatar=avatar, ) person.save() account = Account( service='tumblr.com', ident=account_name, display_name=display_name, person=person, ) account.save() return account
def account_for_flickr_id(nsid, person=None): try: # TODO: update flickr avatar pictures (but that requires fetching their people info speculatively) return Account.objects.get(service='flickr.com', ident=nsid) except Account.DoesNotExist: pass try: result = call_flickr('flickr.people.getInfo', user_id=nsid) except leapfrog.poll.embedlam.RequestError: return persondata = result['person'] if person is None: if int(persondata['iconfarm']) == 0: avatar = None else: avatar = Media( image_url='http://farm%s.static.flickr.com/%s/buddyicons/%s.jpg' % (persondata['iconfarm'], persondata['iconserver'], nsid), width=48, height=48, ) avatar.save() namenode = persondata.get('realname', persondata.get('username')) person = Person( display_name=namenode['_content'], permalink_url=persondata['profileurl']['_content'], avatar=avatar, ) person.save() try: display_name = persondata['realname']['_content'] except KeyError: display_name = None acc = Account( service='flickr.com', ident=nsid, display_name=display_name or nsid, person=person, ) acc.save() return acc
def account_for_twitter_user(userdata, person=None): try: account = Account.objects.get(service='twitter.com', ident=str(userdata['id'])) except Account.DoesNotExist: if person is None: avatar = Media( width=48, height=48, image_url=userdata['profile_image_url'], ) avatar.save() person = Person( display_name=userdata['name'], avatar=avatar, permalink_url='http://twitter.com/%s' % userdata['screen_name'], ) person.save() account = Account( service='twitter.com', ident=str(userdata['id']), display_name=userdata['name'], person=person, status_background_color=userdata.get('profile_background_color') or '', status_background_image_url=userdata.get('profile_background_image_url') or '', status_background_tile=userdata.get('profile_background_tile') or False, ) account.save() else: person = account.person if not person.avatar_source or person.avatar_source == 'twitter.com': if not person.avatar or person.avatar.image_url != userdata['profile_image_url']: avatar = Media( width=48, height=48, image_url=userdata['profile_image_url'], ) avatar.save() person.avatar = avatar person.avatar_source = 'twitter.com' person.save() elif not person.avatar_source: person.avatar_source = 'twitter.com' person.save() # We can always update these (but only pay for saving if they changed). if not (account.status_background_color == (userdata.get('profile_background_color') or '') and account.status_background_image_url == userdata.get('profile_background_image_url') or '' and account.status_background_tile == (userdata.get('profile_background_tile') or False)): account.status_background_color = userdata.get('profile_background_color') or '' account.status_background_image_url = userdata.get('profile_background_image_url') or '' account.status_background_tile = userdata.get('profile_background_tile') or False account.save() return account
def account_for_vimeo_id(user_id, person=None): try: # TODO: update vimeo avatar pictures (but that requires fetching their people info speculatively) return Account.objects.get(service='vimeo.com', ident=user_id) except Account.DoesNotExist: pass # get vimeo data log.debug('Getting info on user %r', user_id) userdata = call_vimeo('vimeo.people.getInfo', user_id=user_id) persondata = userdata['person'] if person is None: portraits = persondata.get('portraits', {}).get('portrait') avatar = None if portraits is not None: portraits = sorted([portrait for portrait in portraits if int(portrait['height']) >= 75], key=lambda x: int(x['height'])) if portraits: portrait = portraits[0] avatar = Media( image_url=portrait['_content'], width=int(portrait['width']), height=int(portrait['height']), ) avatar.save() person = Person( display_name=persondata['display_name'], permalink_url=persondata['profileurl'], avatar=avatar, ) person.save() acc = Account( service='vimeo.com', ident=user_id, display_name=persondata.get('display_name', persondata.get('username', user_id)), person=person, ) acc.save() return acc
def account_for_tumblelog_element(tumblelog_elem, person=None): account_name = tumblelog_elem.attrib['name'] try: account = Account.objects.get(service='tumblr.com', ident=account_name) except Account.DoesNotExist: pass else: person = account.person if not person.avatar_source or person.avatar_source == 'tumblr.com': try: tumblr_avatar_url = tumblelog_elem.attrib['avatar-url-64'] except KeyError: pass else: if not person.avatar or person.avatar.image_url != tumblr_avatar_url: avatar = Media( image_url=tumblr_avatar_url, width=64, height=64, ) avatar.save() person.avatar = avatar person.avatar_source = 'tumblr.com' person.save() elif not person.avatar_source: person.avatar_source = 'tumblr.com' person.save() return account display_name = tumblelog_elem.attrib.get('title', account_name) if person is None: try: host = tumblelog_elem.attrib['cname'] except KeyError: host = '%s.tumblr.com' % tumblelog_elem.attrib['name'] avatar = None if 'avatar-url-64' in tumblelog_elem.attrib: avatar = Media( image_url=tumblelog_elem.attrib['avatar-url-64'], width=64, height=64, ) avatar.save() person = Person( display_name=display_name, permalink_url='http://%s/' % host, avatar=avatar, ) person.save() account = Account( service='tumblr.com', ident=account_name, display_name=display_name, person=person, ) account.save() return account
def account_for_tumblr_shortname(shortname): try: # We don't have any data to update the account with, so whatever we have is fine. return Account.objects.get(service='tumblr.com', ident=shortname) except Account.DoesNotExist: pass csr = oauth.Consumer(*settings.TUMBLR_CONSUMER) url = 'http://api.tumblr.com/v2/blog/%s.tumblr.com/info?api_key=%s' % ( shortname, csr.key) http = httplib2.Http() resp, cont = http.request(url) if resp.status == 500: log.info("Server error fetching tumblr info %s (is Tumblr down?)", account.ident) return if resp.status == 408: log.info("Timeout fetching tumblr info %s (is Tumblr down/slow?)", account.ident) return if resp.status == 401: log.info("401 Unauthorized fetching tumblr info %s (maybe suspended?)", account.ident) return if resp.status == 403: raise ValueError("403 Forbidden fetching tumblr info %s\n\n%s" % (account.ident, cont)) if resp.status != 200: raise ValueError( "Unexpected HTTP response %d %s fetching tumblr info %s" % (resp.status, resp.reason, account.ident)) data = json.loads(cont) blogdata = data['response']['blog'] display_name = blogdata['title'] tumblr_avatar_url = 'http://api.tumblr.com/v2/blog/%s.tumblr.com/avatar/64' % shortname avatar = Media( image_url=tumblr_avatar_url, width=64, height=64, ) avatar.save() person = Person( display_name=display_name, permalink_url=blogdata['url'], avatar=avatar, ) person.save() account = Account( service='tumblr.com', ident=shortname, display_name=blogdata['title'], person=person, ) account.save() return account
def account_for_typepad_user(tp_user, person=None): try: account = Account.objects.get(service='typepad.com', ident=tp_user.url_id) except Account.DoesNotExist: if person is None: if tp_user.avatar_link.url_template: avatar = Media( width=50, height=50, image_url=tp_user.avatar_link.url_template.replace('{spec}', '50si'), ) else: avatar = Media( width=tp_user.avatar_link.width, height=tp_user.avatar_link.height, image_url=tp_user.avatar_link.url, ) avatar.save() person = Person( display_name=tp_user.display_name or tp_user.preferred_username, avatar=avatar, permalink_url=tp_user.profile_page_url, ) person.save() account = Account( service='typepad.com', ident=tp_user.url_id, display_name=tp_user.display_name or tp_user.preferred_username, person=person, ) account.save() else: person = account.person if not person.avatar_source or person.avatar_source == 'typepad.com': if tp_user.avatar_link.url_template: tp_avatar_url = tp_user.avatar_link.url_template.replace('{spec}', '50si') else: tp_avatar_url = tp_user.avatar_link.url if not person.avatar or person.avatar.image_url != tp_avatar_url: if tp_user.avatar_link.url_template: avatar = Media( width=50, height=50, image_url=tp_avatar_url, ) else: avatar = Media( width=tp_user.avatar_link.width, height=tp_user.avatar_link.height, image_url=tp_avatar_url, ) avatar.save() person.avatar = avatar person.avatar_source = 'typepad.com' person.save() elif not person.avatar_source: person.avatar_source = 'typepad.com' person.save() return account
def account_for_typepad_user(tp_user, person=None): try: account = Account.objects.get(service='typepad.com', ident=tp_user.url_id) except Account.DoesNotExist: if person is None: if tp_user.avatar_link.url_template: avatar = Media( width=50, height=50, image_url=tp_user.avatar_link.url_template.replace( '{spec}', '50si'), ) else: avatar = Media( width=tp_user.avatar_link.width, height=tp_user.avatar_link.height, image_url=tp_user.avatar_link.url, ) avatar.save() person = Person( display_name=tp_user.display_name or tp_user.preferred_username, avatar=avatar, permalink_url=tp_user.profile_page_url, ) person.save() account = Account( service='typepad.com', ident=tp_user.url_id, display_name=tp_user.display_name or tp_user.preferred_username, person=person, ) account.save() else: person = account.person if not person.avatar_source or person.avatar_source == 'typepad.com': if tp_user.avatar_link.url_template: tp_avatar_url = tp_user.avatar_link.url_template.replace( '{spec}', '50si') else: tp_avatar_url = tp_user.avatar_link.url if not person.avatar or person.avatar.image_url != tp_avatar_url: if tp_user.avatar_link.url_template: avatar = Media( width=50, height=50, image_url=tp_avatar_url, ) else: avatar = Media( width=tp_user.avatar_link.width, height=tp_user.avatar_link.height, image_url=tp_avatar_url, ) avatar.save() person.avatar = avatar person.avatar_source = 'typepad.com' person.save() elif not person.avatar_source: person.avatar_source = 'typepad.com' person.save() return account
def account_for_twitter_user(userdata, person=None): try: account = Account.objects.get(service='twitter.com', ident=str(userdata['id'])) except Account.DoesNotExist: if person is None: avatar = Media( width=48, height=48, image_url=userdata['profile_image_url'], ) avatar.save() person = Person( display_name=userdata['name'], avatar=avatar, permalink_url='http://twitter.com/%s' % userdata['screen_name'], ) person.save() account = Account( service='twitter.com', ident=str(userdata['id']), display_name=userdata['name'], person=person, status_background_color=userdata.get('profile_background_color') or '', status_background_image_url=userdata.get( 'profile_background_image_url') or '', status_background_tile=userdata.get('profile_background_tile') or False, ) account.save() else: person = account.person if not person.avatar_source or person.avatar_source == 'twitter.com': if not person.avatar or person.avatar.image_url != userdata[ 'profile_image_url']: avatar = Media( width=48, height=48, image_url=userdata['profile_image_url'], ) avatar.save() person.avatar = avatar person.avatar_source = 'twitter.com' person.save() elif not person.avatar_source: person.avatar_source = 'twitter.com' person.save() # We can always update these (but only pay for saving if they changed). if not (account.status_background_color == (userdata.get('profile_background_color') or '') and account.status_background_image_url == userdata.get('profile_background_image_url') or '' and account.status_background_tile == (userdata.get('profile_background_tile') or False)): account.status_background_color = userdata.get( 'profile_background_color') or '' account.status_background_image_url = userdata.get( 'profile_background_image_url') or '' account.status_background_tile = userdata.get( 'profile_background_tile') or False account.save() return account