Beispiel #1
0
    def loads(self, data):
        def object_hook(name, d):
            # Need to lowercase these, because the data is inconsistent in how
            # it names the classes
            name = name.lower()
            if name == 'album':
                fields = d['fields']
                return Album(name=fields['name'], title=fields.get('title'),
                             description=fields.get('description'),
                             parent=fields.get('parentAlbumName'))
            elif name == 'albumitem':
                # This contains info about the image and the thumbnail.  But
                # since we don't care about the thumbnail, just collapse this
                # together with the contained image, which we already parsed
                kwargs = {'caption': d['caption']}
                if d['isAlbumName']:
                    return AlbumItem.SubAlbum(d['isAlbumName'],
                                              caption=d['caption'])
                else:
                    image = d['image']
                    captured_at=datetime.date.fromtimestamp(
                        d['itemCaptureDate'][0])
                    return AlbumItem.Photo(image.name,
                                           caption=d['caption'],
                                           image_type=image.image_type,
                                           captured_at=captured_at)
            elif name == 'image':
                return Photo(name=d['name'], image_type=d['type'])

        loaded = phpserialize.loads(data, object_hook=object_hook)
        if isinstance(loaded, dict):
            loaded = phpserialize.dict_to_list(loaded)
        return loaded
Beispiel #2
0
def process_db_result(raw_data):
    data = {}
    # remove non-useful keys to other dbs
    for key in {
            "fa_id",
            "fa_deleted_user",
            "fa_deleted_reason_id",
            "fa_description_id",
            "fa_actor",
    }:
        raw_data.pop(key, None)

    # Metadata can be stored as JSON or as a serialized PHP array.
    metadata = raw_data.pop("fa_metadata", "")
    try:
        data["fa_metadata"] = json.loads(metadata)
    except json.JSONDecodeError:
        data["fa_metadata"] = phpserialize.loads(metadata, decode_strings=True)
        components = data["fa_metadata"].get("ComponentsConfiguration", {})
        if components:
            components.pop("_type", "")
            data["fa_metadata"][
                "ComponentsConfiguration"] = phpserialize.dict_to_list(
                    components)
    # stringify everything else
    for key, value in raw_data.items():
        if not value:
            continue
        elif isinstance(value, bytes):
            # decode unicode strings
            data[key] = str(value, encoding="utf-8")
        else:
            data[key] = str(value)

    return data
def phpserialized(serialized):
    """ convert to python string list
    """
    from phpserialize import loads
    from phpserialize import dict_to_list
    try:
        serialized = loads(serialized)
        serialized = dict_to_list(serialized)
    except ValueError:
        pass
    return serialized
Beispiel #4
0
def php_to_json(conditions_levels_serialized, id):
    """Convert the condition levels which were stored as serialized PHP
    objects to a JSON string.
    """
    if not conditions_levels_serialized:
        return None

    try:
        levels = parse_php_object(conditions_levels_serialized)
        return json.dumps(phpserialize.dict_to_list(levels))
    except Exception as e:
        print(id)
        raise e
Beispiel #5
0
def php_to_json(conditions_levels_serialized, id):
    """Convert the condition levels which were stored as serialized PHP
    objects to a JSON string.
    """
    if not conditions_levels_serialized:
        return None

    try:
        levels = parse_php_object(conditions_levels_serialized)
        return json.dumps(phpserialize.dict_to_list(levels))
    except Exception as e:
        print(id)
        raise e
Beispiel #6
0
def php_to_json(conditions_levels_serialized, id):
    """Convert the condition levels which were stored as serialized PHP
    objects to a JSON string.
    """
    if not conditions_levels_serialized:
        return None

    try:
        data = bytes(conditions_levels_serialized, encoding='utf-8')
        levels = phpserialize.loads(
            data, object_hook=phpserialize.phpobject, decode_strings=True)
        return json.dumps(phpserialize.dict_to_list(levels))
    except Exception as e:
        print(id)
        raise e
Beispiel #7
0
def php_to_json(conditions_levels_serialized, id):
    """Convert the condition levels which were stored as serialized PHP
    objects to a JSON string.
    """
    if not conditions_levels_serialized:
        return None

    try:
        data = bytes(conditions_levels_serialized, encoding='utf-8')
        levels = phpserialize.loads(data,
                                    object_hook=phpserialize.phpobject,
                                    decode_strings=True)
        return json.dumps(phpserialize.dict_to_list(levels))
    except Exception as e:
        print(id)
        raise e
Beispiel #8
0
 def get_hq(cls):
     """
     从数据库获取行情
     """
     data = DB().select("select * from cache where `key`='cb_data'")
     cb_lists = dict_to_list(loads(data[0][1].encode('utf-8')))
     ret = []
     for item in cb_lists:
         d = {}
         for (k, v) in item.items():
             if type(k) is bytes:
                 k = k.decode()
             if type(v) is bytes:
                 v = v.decode()
             d[k] = v
         ret.append(d)
     return pd.DataFrame(ret)
Beispiel #9
0
def decode(data):
    fp = StringIO(data)
    r = {}
    while True:
        npos = data.find('|', fp.tell())
        if npos == -1:
            break
        k = data[fp.tell():npos]
        fp.seek(npos + 1)
        d = phpserialize.load(fp)
        try:
            if sorted(map(int, d.keys())) == range(len(d)):
                d = phpserialize.dict_to_list(d)
        except:
            pass
        r[k] = d
    return r
Beispiel #10
0
 def test_list_roundtrips(self):
     x = phpserialize.loads(phpserialize.dumps(list(range(2))))
     self.assertEqual(x, {0: 0, 1: 1})
     y = phpserialize.dict_to_list(x)
     self.assertEqual(y, [0, 1])
Beispiel #11
0
 def test_list_roundtrips(self):
     x = phpserialize.loads(phpserialize.dumps(list(range(2))))
     self.assertEqual(x, {0: 0, 1: 1})
     y = phpserialize.dict_to_list(x)
     self.assertEqual(y, [0, 1])
Beispiel #12
0
        try:
            self.cursor.execute(sql)
            data = self.cursor.fetchall()
            return data
        except:
            Log.d("---查询失败---")
        finally:
            self.cursor.close()


# 关闭数据库连接

    def close(self):
        self.db.close()

if __name__ == "__main__":
    data = DB().select("select * from cache where `key`='cb_data'")
    print(data[0][1])
    cb_lists = dict_to_list(loads(data[0][1].encode('utf-8')))
    ret = []
    for item in cb_lists:
        d = {}
        for (k, v) in item.items():
            if type(k) is bytes:
                k = k.decode()
            if type(v) is bytes:
                v = v.decode()
            d[k] = v
        ret.append(d)
    print(ret)