示例#1
0
    def update(self):
        args = {'access_token': self.config['access_token']}
        query = "https://graph.facebook.com/%s/feed?%s" % (
            self.config['object'],
            urllib.urlencode(args),
        )
        file = urllib.urlopen(query)
        raw = file.read()
        response = json.loads(raw)

        from_id = self.config.get('from_id', None)

        for entry in response['data']:
            if from_id and entry['from']['id'] != from_id:
                continue

            if 'to' in entry:  # messages
                continue

            link = 'https://facebook.com/%s' % (
                entry['id'].replace('_', '/posts/'),
            )

            self.create_story(
                link,
                title=(
                    entry.get('name')
                    or entry.get('message')
                    or entry.get('story', u'')
                ),
                body=entry.get('message', u''),
                image_url=entry.get('picture', u''),
                timestamp=datetime.strptime(
                    entry['created_time'], '%Y-%m-%dT%H:%M:%S+0000'),
            )
示例#2
0
    def update(self):
        args = {'access_token': self.config['access_token']}
        query = "https://graph.facebook.com/%s/feed?%s" % (
            self.config['object'],
            urllib.urlencode(args),
        )
        file = urllib.urlopen(query)
        raw = file.read()
        response = json.loads(raw)

        from_id = self.config.get('from_id', None)

        for entry in response['data']:
            if from_id and entry['from']['id'] != from_id:
                continue

            if 'to' in entry:  # messages
                continue

            link = 'https://facebook.com/%s' % (entry['id'].replace(
                '_', '/posts/'), )

            self.create_story(
                link,
                title=(entry.get('name') or entry.get('message')
                       or entry.get('story', u'')),
                body=entry.get('message', u''),
                image_url=entry.get('picture', u''),
                timestamp=datetime.strptime(entry['created_time'],
                                            '%Y-%m-%dT%H:%M:%S+0000'),
            )
示例#3
0
 def get_room_status(self, channel):
     """
     Return information about the current status of a room.
     (most importanly, members currently online in that room).
     """
     path = '/status/room/%s' % (channel)
     response = self._do_request('GET', path)
     data = response.read()
     try:
         return json.loads(data)
     except ValueError:
         return None
示例#4
0
 def get_token(self, user_id):
     """
     For a given user ID, return a token. Keep in mind that this ID
     could be anything that uniquley describes a user in the system.
     A primary key from the users table is a good fit, but so is
     a session ID, as long as it is unique.
     """
     path = '/auth/token/%s' % (user_id)
     response = self._do_request('POST', path)
     if not response or response.status > 399:
         return None
     try:
         resp = json.loads(response.read())
     except ValueError:
         return None
     if resp:
         if 'token' in resp:
             return '%s|%s' % (user_id, resp['token'])
     return None
示例#5
0
    def handle(self, *args, **options):
        # clear everything out to start with
        Tag.objects.all().delete()
        Photo.objects.all().delete()
        Album.objects.all().delete()
        AlbumPhoto.objects.all().delete()

        for directory in args:
            idmap = dict()
            images = set([filename for filename in os.listdir(directory)
                          if filename.endswith("jpg")])
            image_json = [filename for filename in os.listdir(directory)
                          if filename.endswith("json") and
                          not filename.startswith("set_")]
            sets_json = [filename for filename in os.listdir(directory)
                         if filename.endswith("json") and
                         filename.startswith("set_")]
            for filename in image_json:
                print "Image", filename

                # find the matching image and upload it to reticulum
                flickrid = os.path.splitext(filename)[0]
                image_filename = flickrid + ".jpg"
                if image_filename not in images:
                    print "no image found"
                    continue

                fullpath = os.path.join(directory, image_filename)
                files = {
                    'image': (image_filename, open(fullpath, 'rb'))
                }
                r = requests.post(RETICULUM_BASE, files=files)
                rhash = loads(r.text)["hash"]

                json = loads(open(os.path.join(directory, filename)).read())
                taken = datetime.strptime(json['taken'], "%Y-%m-%d %H:%M:%S")
                p = Photo.objects.create(
                    title=json['title'],
                    description=json['description'],
                    taken=pytz.utc.localize(taken),
                    modified=datetime.utcfromtimestamp(
                        float(int(json['lastupdate']))),
                    uploaded=datetime.utcfromtimestamp(
                        float(int(json['dateuploaded']))),
                    views=int(json['views']),
                    reticulum_key=rhash,
                    extension='jpg',
                )
                for tag in json['tags']:
                    p.add_tag(tag)
                idmap[flickrid] = p

            for filename in sets_json:
                json = loads(open(os.path.join(directory, filename)).read())
                print "Set", filename
                a = Album.objects.create(
                    title=json['title'],
                    description=json['description'],
                    created=datetime.utcfromtimestamp(
                        float(int(json['date_create']))),
                    modified=datetime.utcfromtimestamp(
                        float(int(json['date_update']))),
                )
                for flickrid in json['images']:
                    p = idmap[flickrid]
                    AlbumPhoto.objects.get_or_create(album=a, photo=p)