Esempio n. 1
0
    def on_message(self, message):
        logging.info(message)
        msg = json.loads(message['data'])
        musics = msg.get('musics')

        # download
        for music in musics:
            song_name = music.get('song_name')
            song_id = music.get('song_id')
            singer_name = music.get('singer_name')
            singer_id = music.get('singer_id')
            audition_list = music.get('audition_list')
            album_id = music.get('album_id')
            album_name = music.get('album_name')

            audition = audition_list[-1]
            url = audition.get('url')
            duration = audition.get('duration')
            suffix = audition.get('suffix')
            bit_rate = audition.get('bitRate')
            type_description = audition.get('typeDescription')
            size = audition.get('size')
            pic_url = music.get('mv_list')[0].get('pic_url') if music.get(
                'mv_list') else ''

            if not Query(Music).equal_to('song_id', song_id).find():
                filename = u'{}-{}.mp3'.format(song_name, singer_name)
                f = File(filename,
                         StringIO.StringIO(urllib2.urlopen(url).read()))
                f.save()

                picture = None
                if pic_url:
                    picture_name = u'{}-{}.jpg'.format(song_name, singer_name)
                    picture = File(
                        picture_name,
                        StringIO.StringIO(urllib2.urlopen(pic_url).read()))
                    picture.save()

                m = Music()
                m.file = f
                m.duration = duration
                m.suffix = suffix
                m.bit_rate = bit_rate
                m.type_description = type_description
                m.size = size
                m.song_name = song_name
                m.song_id = song_id
                m.singer_name = singer_name
                m.singer_id = singer_id
                m.album_id = album_id
                m.album_name = album_name
                if picture:
                    m.picture = picture
                m.save()
Esempio n. 2
0
def test_basic():
    s = BytesIO(b'blah blah blah')
    if PY2:
        import cStringIO
        s1 = cStringIO.StringIO()
        s1.write('blah blah blah')
    else:
        s1 = s
    f1 = File('Blah', s, 'text/plain')
    f2 = File('Blah', s1)
    f3 = File('Blah', open('tests/test_file.txt', 'rb'))
    for f in (f1, f2, f3):
        assert f.name == 'Blah'
        assert f._metadata['size'] == 14
        assert f.size == 14
        assert f._type == 'text/plain'
Esempio n. 3
0
def test_destroy():
    r = requests.get('http://i1.wp.com/leancloud.cn/images/static/default-avatar.png')
    b = buffer_type(r.content)
    f = File('Lenna2.jpg', b)
    f.save()
    assert f.id
    f.destroy()
Esempio n. 4
0
def test_destroy():  # type: () -> None
    r = requests.get("http://i1.wp.com/leancloud.cn/images/static/default-avatar.png")
    b = io.BytesIO(r.content)
    f = File("Lenna2.jpg", b)
    f.save()
    assert f.id
    f.destroy()
def upload(file_path):
    if os.path.exists(file_path):
        img_name = os.path.split(file_path)[1]
        img_file = open(file_path)
        up_file = File(img_name, img_file)
        img_url = up_file.save().url
        print(" url: %s" % img_url)
        return img_url
def test_save_with_specified_key_but_without_master_key():  # type: () -> None
    f = File("Blah.txt", open("tests/sample_text.txt", "rb"))
    f.key = "abc"
    try:
        f.save()
    except LeanCloudError as e:
        if e.code == 1 and e.error.startswith("Unsupported file key"):
            pass
Esempio n. 7
0
def test_thumbnail_size_erorr():
    r = requests.get('http://i1.wp.com/leancloud.cn/images/static/default-avatar.png')
    b = buffer_type(r.content)
    f = File('Lenna2.jpg', b)
    f.save()
    assert f.id

    f.get_thumbnail_url(-1, -1)
    f.get_thumbnail_url(1, 1, quality=110)
Esempio n. 8
0
def test_thumbnail():
    r = requests.get('http://i1.wp.com/leancloud.cn/images/static/default-avatar.png')
    b = buffer_type(r.content)
    f = File('Lenna2.jpg', b)
    f.save()
    assert f.id

    url = f.get_thumbnail_url(100, 100)
    assert url.endswith('?imageView/2/w/100/h/100/q/100/format/png')
Esempio n. 9
0
def test_thumbnail():  # type: () -> None
    r = requests.get("http://i1.wp.com/leancloud.cn/images/static/default-avatar.png")
    b = io.BytesIO(r.content)
    f = File("Lenna2.jpg", b)
    f.save()
    assert f.id

    url = f.get_thumbnail_url(100, 100)
    assert url.endswith("?imageView/2/w/100/h/100/q/100/format/png")
Esempio n. 10
0
def test_thumbnail():
    r = requests.get('http://www.lenna.org/full/len_std.jpg')
    b = buffer(r.content)
    f = File('lenna.jpg', b)
    f.save()
    assert f.id

    url = f.get_thumbnail_url(100, 100)
    assert url.endswith('?imageView/2/w/100/h/100/q/100/format/png')
Esempio n. 11
0
def test_thumbnail_size_erorr():  # type: () -> None
    r = requests.get("http://i1.wp.com/leancloud.cn/images/static/default-avatar.png")
    b = io.BytesIO(r.content)
    f = File("Lenna2.jpg", b)
    f.save()
    assert f.id

    f.get_thumbnail_url(-1, -1)
    f.get_thumbnail_url(1, 1, quality=110)
Esempio n. 12
0
File: lzmd.py Progetto: chinaq/blog
def upload(file_path):
    print file_path
    img_name = path.split(file_path)[1]
    with open(file_path, 'rb') as f:
        img_file = read_full_file(f)  # Qiang
        up_file = File(img_name, buffer(img_file))
        up_file.save()
        img_url = up_file.url
        return img_url
Esempio n. 13
0
def test_file_field():
    user = User()
    user.login('user1_name', 'password')
    user.set('xxxxx', File('xxx.txt', buffer('qqqqq')))
    user.save()

    q = Query(User)
    saved_user = q.get(user.id)
    assert isinstance(saved_user.get('xxxxx'), File)
    assert saved_user.get('xxxxx').name == 'xxx.txt'
Esempio n. 14
0
def test_file_field():  # type: () -> None
    user = User()
    user.login('user1_name', 'password')
    user.set('xxxxx', File('xxx.txt', io.BytesIO(b'qqqqq')))
    user.save()

    q = Query(User)
    saved_user = q.get(user.id)
    assert isinstance(saved_user.get('xxxxx'), File)
    assert saved_user.get('xxxxx').name == 'xxx.txt'
Esempio n. 15
0
def test_file_field():  # type: () -> None
    user = User()
    user.login("user1_name", "password")
    user.set("xxxxx", File("xxx.txt", io.BytesIO(b"qqqqq")))
    user.save()

    q = Query(User)
    saved_user = q.get(user.id)
    assert isinstance(saved_user.get("xxxxx"), File)
    assert saved_user.get("xxxxx").name == "xxx.txt"
Esempio n. 16
0
def get_img(url):
    try:
        fn = url.split('/')[-1]
        c = urllib2.urlopen(url).read()
        f = StringIO(c)
        lc_file = File(fn, f)
        lc_file.save()
        r = lc_file.url
    except Exception, e:
        print '---------', e
        r = None
Esempio n. 17
0
def test_file_callback():  # type: () -> None
    d = {}

    def noop(token, *args, **kwargs):
        d["token"] = token

    f = File("xxx", io.BytesIO(b"xxx"))
    f._save_to_s3 = noop
    f._save_to_qiniu = noop
    f._save_to_qcloud = noop
    f.save()
    f._save_callback(d["token"], False)
Esempio n. 18
0
def test_file_callback():  # type: () -> None
    d = {}

    def noop(token, *args, **kwargs):
        d['token'] = token

    f = File('xxx', io.BytesIO(b'xxx'))
    f._save_to_s3 = noop
    f._save_to_qiniu = noop
    f._save_to_qcloud = noop
    f.save()
    f._save_callback(d['token'], False)
Esempio n. 19
0
def test_basic():  # type: () -> None
    s = BytesIO(b'blah blah blah')
    if PY2:
        import cStringIO  # type: ignore
        s1 = cStringIO.StringIO()
        s1.write('blah blah blah')
    else:
        s1 = s
    f = File('Blah', s, mime_type='text/plain')
    assert f.name == 'Blah'
    assert f._metadata['size'] == 14
    assert f.size == 14
Esempio n. 20
0
def test_save():  # type: () -> None
    user = leancloud.User()
    user.login('user1_name', 'password')

    f = File('Blah.txt', open('tests/sample_text.txt', 'rb'))
    f.save()

    assert f.owner_id == user.id
    assert f.id
    assert f.name == 'Blah.txt'
    assert f.mime_type == 'text/plain'
    assert not f.url.endswith('.')
 def upload_file(self, file_abspath):
     filename = os.path.basename(file_abspath)  # filename have suffix
     with open(file_abspath, 'r') as f:
         upload_file = File(filename, f)
         upload_file.save()
         print 'uploaded', file_abspath
         img_file = self._class()
         img_file.set('File', upload_file)
         img_file.set('filename', filename)
         tag_list = LeanCloudApi.get_tag_list(filename)
         img_file.set('tag_list', tag_list)
         img_file.save()
         self.add_img_info(img_file.id)  # save img_info after save
Esempio n. 22
0
def test_save_with_specified_key():  # type: () -> None
    f = File("Blah.txt", open("tests/sample_text.txt", "rb"))
    user_specified_key = "abc"
    f.key = user_specified_key
    f.save()

    assert f.id
    assert f.name == "Blah.txt"
    assert f.mime_type == "text/plain"
    path = urlparse(f.url).path
    if path.startswith("/avos-cloud-"):  # old school aws s3 file url
        assert path.split("/")[2] == user_specified_key
    else:
        assert path == "/" + user_specified_key
Esempio n. 23
0
def test_fetch():
    r = requests.get('http://i1.wp.com/leancloud.cn/images/static/default-avatar.png')
    b = buffer_type(r.content)
    f = File('Lenna2.jpg', b)
    f.metadata['foo'] = 'bar'
    f.save()
    fetched = File.create_without_data(f.id)
    fetched.fetch()
    assert fetched.id == f.id
    assert fetched.metadata == f.metadata
    assert fetched.name == f.name
    assert fetched.url == f.url
    assert fetched.size == f.size
    assert fetched.url == f.url
    f.destroy()
Esempio n. 24
0
def test_fetch():  # type: () -> None
    r = requests.get("http://i1.wp.com/leancloud.cn/images/static/default-avatar.png")
    b = io.BytesIO(r.content)
    f = File("Lenna2.jpg", b)
    f.metadata["foo"] = "bar"
    f.save()
    fetched = File.create_without_data(f.id)
    fetched.fetch()

    normalized_f_url = f.url.split("/")[-1]
    normalized_fetched_url = f.url.split("/")[-1]

    assert fetched.id == f.id
    assert fetched.metadata == f.metadata
    assert fetched.name == f.name
    assert fetched.size == f.size
    assert fetched.url == f.url or normalized_fetched_url == normalized_f_url
    f.destroy()
Esempio n. 25
0
 def upload_file_by_url(self, filename, url, tag_list=None):
     """tag_list is tag of string list"""
     data = LeanCloudApi.fetch_data(url)
     if not data:
         return
     data = data.content
     f = File(filename, StringIO(data))
     img_file = self._class()
     img_file.set('File', f)
     img_file.set('filename', filename)
     if tag_list:
         img_file.set('tag_list', tag_list)
     try:
         img_file.save()
         print filename, '----uploaded----'
         self.add_img_info(img_file.id)  # save img_info after save
     except:
         'save failed'
         return
Esempio n. 26
0
def test_save():  # type: () -> None
    user = leancloud.User()
    name = 'user1_name'
    passwd = 'password'
    try:
        user.login(name, passwd)
    except LeanCloudError as e:
        if e.code == 211:
            user.set_username(name)
            user.set_password(passwd)
            user.sign_up()
            user.login(name, passwd)

    f = File('Blah.txt', open('tests/sample_text.txt', 'rb'))
    f.save()

    assert f.owner_id == user.id
    assert f.id
    assert f.name == 'Blah.txt'
    assert f.mime_type == 'text/plain'
    assert not f.url.endswith('.')
Esempio n. 27
0
def test_save():  # type: () -> None
    user = leancloud.User()
    name = "user1_name"
    passwd = "password"
    try:
        user.login(name, passwd)
    except LeanCloudError as e:
        if e.code == 211:
            user.set_username(name)
            user.set_password(passwd)
            user.sign_up()
            user.login(name, passwd)

    f = File("Blah.txt", open("tests/sample_text.txt", "rb"))
    f.save()

    assert f.owner_id == user.id
    assert f.id
    assert f.name == "Blah.txt"
    assert f.mime_type == "text/plain"
    assert not f.url.endswith(".")
Esempio n. 28
0
def test_save():
    f = File('blah', buffer('xxx'))
    f.save()
    assert f.id
Esempio n. 29
0
def test_acl():
    acl = ACL()
    f = File('blah', buffer('xxx'))
    f.set_acl(acl)
    assert f.get_acl() == acl
Esempio n. 30
0
def test_basic():
    s = StringIO('blah blah blah')
    f = File('blah', s)
    assert f.name == 'blah'
    assert f._metadata['size'] == 14
    assert f._type == 'text/plain'