예제 #1
0
파일: models.py 프로젝트: meZee/pyaspora
    def import_contact(cls, addr):
        """
        Fetch information about a Diaspora user and import it into the Contact
        provided.
        """
        try:
            wf = WebfingerRequest(addr).fetch()
        except URLError as e:
            current_app.logger.warning(e)
            current_app.logger.warning(e.readlines())
            return None
        if not wf:
            return None

        NS = {'XRD': 'http://docs.oasis-open.org/ns/xri/xrd-1.0'}

        c = Contact()

        pk = wf.xpath('//XRD:Link[@rel="diaspora-public-key"]/@href',
                      namespaces=NS)[0]
        c.public_key = b64decode(pk).decode("ascii")

        hcard_url = wf.xpath(
            '//XRD:Link[@rel="http://microformats.org/profile/hcard"]/@href',
            namespaces=NS)[0]
        req = Request(hcard_url)
        req.add_header('User-Agent', USER_AGENT)
        hcard = html.parse(urlopen(req, timeout=10))
        c.realname = hcard.xpath('//*[@class="fn"]')[0].text

        pod_loc = hcard.xpath('//*[@id="pod_location"]')[0].text
        photo_url = hcard.xpath('//*[@class="entity_photo"]//img/@src')[0]
        if photo_url:
            try:
                mp = import_url_as_mimepart(urljoin(pod_loc, photo_url))
            except:
                current_app.logger.debug(format_exc())
            else:
                mp.text_preview = u'(picture for {0})'.format(c.realname
                                                              or '(anonymous)')
                c.avatar = mp

        username = wf.xpath('//XRD:Subject/text()',
                            namespaces=NS)[0].split(':')[1]
        guid = wf.xpath(".//XRD:Link[@rel='http://joindiaspora.com/guid']",
                        namespaces=NS)[0].get("href")
        server = wf.xpath(
            ".//XRD:Link[@rel='http://joindiaspora.com/seed_location']",
            namespaces=NS)[0].get("href")
        d = cls(contact=c, guid=guid, username=username, server=server)
        db.session.add(d)
        db.session.add(c)

        try:
            d.import_public_posts()
        except:
            current_app.logger.debug(format_exc())

        return d
예제 #2
0
파일: models.py 프로젝트: meZee/pyaspora
 def __init__(self, contact=None):
     """
     Creates a new user, creating a new Contact for the user if none is
     supplied. The contact is then associated with the newly created User.
     """
     db.Model.__init__(self)
     if not contact:
         contact = Contact()
     self.contact = contact
     db.session.add(self)