Example #1
0
    def receive(cls, xml, c_from, u_to):
        """
        Contact <c_from has updated their profile.
        """
        data = cls.as_dict(xml)
        assert(data['diaspora_handle'] == c_from.diasp.username)
        c_from.realname = " ".join(
            data.get(k, '') or '' for k in ('first_name', 'last_name')
        )
        c_from.bio = MimePart(
            text_preview=data.get('bio', '(bio)'),
            body=dumps(data).encode('utf-8'),
            type='application/x-pyaspora-diaspora-profile'
        )
        if 'image_url' in data:
            mp = import_url_as_mimepart(urljoin(
                c_from.diasp.server,
                data['image_url']
            ))
            mp.text_preview = '(picture for {0})'.format(c_from.realname)
            c_from.avatar = mp
        else:
            c_from.avatar = None

        c_from.interests = cls.find_tags(data['tag_string'] or '')

        db.session.add(c_from)
        db.session.commit()
Example #2
0
    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
Example #3
0
    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:
            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]
        hcard = html.parse(urlopen(hcard_url))
        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:
            mp = import_url_as_mimepart(urljoin(pod_loc, photo_url))
            mp.text_preview = '(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)

        return d
Example #4
0
    def receive(cls, xml, c_from, u_to):
        """
        Contact <c_from has updated their profile.
        """
        data = cls.as_dict(xml)
        assert (data['diaspora_handle'] == c_from.diasp.username)
        c_from.realname = " ".join(
            data.get(k, '') or '' for k in ('first_name', 'last_name'))
        c_from.bio = MimePart(text_preview=data.get('bio', '(bio)'),
                              body=dumps(data).encode('utf-8'),
                              type='application/x-pyaspora-diaspora-profile')
        if 'image_url' in data:
            mp = import_url_as_mimepart(
                urljoin(c_from.diasp.server, data['image_url']))
            mp.text_preview = '(picture for {0})'.format(c_from.realname)
            c_from.avatar = mp
        else:
            c_from.avatar = None

        c_from.interests = cls.find_tags(data['tag_string'] or '')

        db.session.add(c_from)
        db.session.commit()