Ejemplo n.º 1
0
 def __getMusicLists(self, albumName, country):
     #get album musics info
     url = self.baseUrl + 'term=%s&country=%s&media=music&limit=%d'%(albumName, country, self.limit)
     #DEBUG.p(url)
     jsonResultMusics = self.__getResponeResult(url)
     DEBUG.p('result count: %d'%(len(jsonResultMusics)))
     return jsonResultMusics
Ejemplo n.º 2
0
 def __getResponeResult(self, url):
     try:
         login_data = urllib.urlencode({})
         login_headers = {
             'Referer': url,
             'User-Agent': 'Opera/9.60',
         }
         login_request = urllib2.Request(url, login_data, login_headers)
         result = urllib2.urlopen(login_request, data=None,
                                  timeout=30).read()
         return json.loads(result.decode("utf-8"))['results']
     except:
         DEBUG.p('get data failed, and try again...')
         try:
             login_data = urllib.urlencode({})
             login_headers = {
                 'Referer': url,
                 'User-Agent': 'Opera/9.60',
             }
             login_request = urllib2.Request(url, login_data, login_headers)
             result = urllib2.urlopen(login_request, data=None,
                                      timeout=30).read()
             return json.loads(result.decode("utf-8"))['results']
         except:
             DEBUG.p('err: get data failed from: %s' % (url))
             result = []
             return result
Ejemplo n.º 3
0
    def getInfosWithAlbum_deepdeep(self, albumName, country, limit=50):
        if not albumName:
            DEBUG.p('Please special the album name')
            return false
        if not country:
            DEBUG.p('country undefine, use "us" as defaulted')
            country = 'us'
        if limit != 50:
            self.limit = int(limit)

        infos = []
        #get all albums of name 'albumName'
        jsonResultAlbums = self.__getInfoByAlbumName(albumName, country)
        for jsonResultAlbum in jsonResultAlbums:
            ret = self.__saveAllInfos(jsonResultAlbum, country)
            if not ret:
                continue
            infos.append(ret)

            #get all albums of the artist
            infos.extend(
                self.getInfosWithAritstName_deep(jsonResultAlbum['artistName'],
                                                 country, limit))
            #get all albums of the album
            infos.extend(
                self.getInfosWithAlbumName_deep(
                    jsonResultAlbum['collectionName'], country, limit))
        return infos
Ejemplo n.º 4
0
 def __getMusicLists(self, albumName, country):
     #get album musics info
     url = self.baseUrl + 'term=%s&country=%s&media=music&limit=%d' % (
         albumName, country, self.limit)
     #DEBUG.p(url)
     jsonResultMusics = self.__getResponeResult(url)
     DEBUG.p('result count: %d' % (len(jsonResultMusics)))
     return jsonResultMusics
Ejemplo n.º 5
0
    def __init__(self):
        try:
            self.__server = Server()
        except:
            print 'can not connect to Couchdb:%s' % (settings.c['db_url'])

        self.__db = {}
        self.__db_name = settings.c['db_name']
        DEBUG.p(self.__db_name.items())
        for (k, v) in self.__db_name.items():
            try:
                self.__db[v] = self.__server.create(v)
            except:
                self.__db[v] = self.__server[v]
Ejemplo n.º 6
0
    def __init__(self):
        try:
            self.__server = Server()
        except:
            print 'can not connect to Couchdb:%s'%(settings.c['db_url'])

        self.__db = {} 
        self.__db_name = settings.c['db_name']
        DEBUG.p(self.__db_name.items())
        for (k, v) in self.__db_name.items():
            try:
                self.__db[v] = self.__server.create(v)
            except:
                self.__db[v] = self.__server[v]
Ejemplo n.º 7
0
def make_dir():
    name = "Waldemar Matuska, Karel \xc4\x8cernoch, Lenka Filipova, Karel Gott, Karel H\xc3\xa1la, Ji\xc5\x99\xc3\xad \xc5\xa0t\xc4\x9bdro\xc5\x88, Felix Slov\xc3\xa1\xc4\x8dek, Na\xc4\x8fa Urb\xc3\xa1nkov\xc3\xa1, Helena Vondr\xc3\xa1\xc4\x8dkov\xc3\xa1, Hana Zagorov\xc3\xa1, Karel V\xc3\xa1gner Se Sv\xc3\xbdm Orchestrem, Ladislav \xc5\xa0Taidl Se Sv\xc3\xbdm Orchestrem, Orchestr \xc4\x8cs. televize & Tane\xc4\x8dn\xc3\xad"
    print len(name)
    print name[0:254]
    artist_dir = os.getcwd()
    dir_len = len(name)
    if dir_len > 255:
        album_dir = artist_dir + '/' + name[0:254]
    else:
        album_dir = artist_dir + '/' + name

    if not os.path.exists(album_dir):
        os.makedirs(album_dir)
    else:
        DEBUG.p('this album info exist!!!!')
Ejemplo n.º 8
0
def make_dir():
        name = "Waldemar Matuska, Karel \xc4\x8cernoch, Lenka Filipova, Karel Gott, Karel H\xc3\xa1la, Ji\xc5\x99\xc3\xad \xc5\xa0t\xc4\x9bdro\xc5\x88, Felix Slov\xc3\xa1\xc4\x8dek, Na\xc4\x8fa Urb\xc3\xa1nkov\xc3\xa1, Helena Vondr\xc3\xa1\xc4\x8dkov\xc3\xa1, Hana Zagorov\xc3\xa1, Karel V\xc3\xa1gner Se Sv\xc3\xbdm Orchestrem, Ladislav \xc5\xa0Taidl Se Sv\xc3\xbdm Orchestrem, Orchestr \xc4\x8cs. televize & Tane\xc4\x8dn\xc3\xad"
        print len(name)
        print name[0:254]
        artist_dir = os.getcwd()
        dir_len = len(name)
        if dir_len > 255:
            album_dir = artist_dir + '/' + name[0:254]
        else:
            album_dir = artist_dir + '/' + name

        if not os.path.exists(album_dir):
           os.makedirs(album_dir)
        else:
           DEBUG.p('this album info exist!!!!')
Ejemplo n.º 9
0
    def getInfosWithAritstName_deep(self, artistName, country, limit=50):
        if not artistName:
            DEBUG.p('Please special the artist name')
            return false
        if not country:
            DEBUG.p('country undefine, use "us" as defaulted')
            country = 'us'
        if limit != 50:
            self.limit = int(limit)

        infos = []
        #get all albums of the artist
        jsonResultAlbums = self.__getInfoByArtistName(artistName, country)
        for jsonResultAlbum in jsonResultAlbums:
            ret = self.__saveAllInfos(jsonResultAlbum, country)
            if not ret:
                continue
            infos.append(ret)
            #enter db

            #get all albums of other artist
            if jsonResultAlbum['artistName'] != artistName:
                jsonAlbums = self.__getInfoByArtistName(
                    jsonResultAlbum['artistName'], country)
                for jsonAlbum in jsonAlbums:
                    ret = self.__saveAllInfos(jsonAlbum, country)
                    if not ret:
                        continue
                    infos.append(ret)

                    jsonResultAlbums2 = self.__getInfoByAlbumName(
                        jsonAlbum['collectionName'], country)
                    for jsonResultAlbum2 in jsonResultAlbums2:
                        ret = self.__saveAllInfos(jsonResultAlbum2, country)
                        if not ret:
                            continue
                        infos.append(ret)

                        jsonAlbums2 = self.__getInfoByArtistName(
                            jsonResultAlbum2['artistName'], country)
                        for jsonAlbum2 in jsonAlbums2:
                            ret = self.__saveAllInfos(jsonAlbum2, country)
                            if not ret:
                                continue
                            infos.append(ret)

        return infos
Ejemplo n.º 10
0
    def getInfosWithAritstName_deep(self, artistName, country, limit = 50):
        if not artistName:
           DEBUG.p('Please special the artist name')
           return false 
        if not country:
           DEBUG.p('country undefine, use "us" as defaulted')
           country = 'us'
        if limit != 50:
           self.limit = int(limit)

        infos = []
        #get all albums of the artist
        jsonResultAlbums = self.__getInfoByArtistName(artistName, country)
        for jsonResultAlbum in jsonResultAlbums:
            ret = self.__saveAllInfos(jsonResultAlbum, country)
            if not ret: 
               continue
            infos.append(ret) 
            #enter db

            #get all albums of other artist
            if jsonResultAlbum['artistName'] != artistName:
                    jsonAlbums = self.__getInfoByArtistName(jsonResultAlbum['artistName'], country)
                    for jsonAlbum in jsonAlbums:
                        ret = self.__saveAllInfos(jsonAlbum, country)
                        if not ret:
                           continue
                        infos.append(ret) 

                        jsonResultAlbums2 = self.__getInfoByAlbumName(jsonAlbum['collectionName'], country)
                        for jsonResultAlbum2 in jsonResultAlbums2:
                            ret = self.__saveAllInfos(jsonResultAlbum2, country)
                            if not ret:
                               continue
                            infos.append(ret) 

                            jsonAlbums2 = self.__getInfoByArtistName(jsonResultAlbum2['artistName'], country)
                            for jsonAlbum2 in jsonAlbums2:
                                ret = self.__saveAllInfos(jsonAlbum2, country)
                                if not ret:
                                   continue
                                infos.append(ret) 

        return infos
Ejemplo n.º 11
0
 def __getResponeResult(self, url):
     try:
         login_data = urllib.urlencode({})
         login_headers = {'Referer':url, 'User-Agent':'Opera/9.60',}
         login_request = urllib2.Request(url, login_data, login_headers)
         result = urllib2.urlopen(login_request, data=None, timeout=30).read()
         return json.loads(result.decode("utf-8"))['results']
     except:
         DEBUG.p('get data failed, and try again...')
         try:
             login_data = urllib.urlencode({})
             login_headers = {'Referer':url, 'User-Agent':'Opera/9.60',}
             login_request = urllib2.Request(url, login_data, login_headers)
             result = urllib2.urlopen(login_request, data=None, timeout=30).read()
             return json.loads(result.decode("utf-8"))['results']
         except:
             DEBUG.p('err: get data failed from: %s' % (url))
             result = []
             return result
Ejemplo n.º 12
0
    def getInfosWithAlbumName(self, albumName, country, limit = 50):
        if not albumName:
           DEBUG.p('Please special the album name')
           return false 
        if not country:
           DEBUG.p('country undefine, use "us" as defaulted')
           country = 'us'
        if limit != 50:
           self.limit = int(limit)

        infos = []
        #get all albums of name 'albumName' 
        jsonResultAlbums = self.__getInfoByAlbumName(albumName, country)
        for jsonResultAlbum in jsonResultAlbums:
            ret = self.__saveAllInfos(jsonResultAlbum, country)
            if not ret: 
               continue
            infos.append(ret) 

        return infos
Ejemplo n.º 13
0
    def __getInfoByArtistName(self, artistName, country):
        if not artistName:
           DEBUG.p('Please special the artist name')
           return false 
        if not country:
           DEBUG.p('country undefine, use "us" as defaulted')
           country = 'us'

        url = self.baseUrl + 'term=%s&country=%s&media=music&entity=album&limit=%d'%(artistName, country, self.limit)
        DEBUG.p(url)
        jsonResultAlbums = self.__getResponeResult(url)
        DEBUG.p('result count: %d'%(len(jsonResultAlbums)))
        return jsonResultAlbums
Ejemplo n.º 14
0
    def getInfosWithArtistName(self, artistName, country, limit=50):
        if not artistName:
            DEBUG.p('Please special the artist name')
            return false
        if not country:
            DEBUG.p('country undefine, use "us" as defaulted')
            country = 'us'
        if limit != 50:
            self.limit = int(limit)

        infos = []
        #get all albums of the artist
        jsonResultAlbums = self.__getInfoByArtistName(artistName, country)
        for jsonResultAlbum in jsonResultAlbums:
            ret = self.__saveAllInfos(jsonResultAlbum, country)
            if not ret:
                continue
            infos.append(ret)

        return infos
Ejemplo n.º 15
0
    def getInfosWithAlbumNameAndArtistName(self,
                                           albumName,
                                           artistName,
                                           country,
                                           limit=50):
        if not albumName and not artistName:
            DEBUG.p('Please special the album name and artist name')
            return false
        if not country:
            DEBUG.p('country undefine, use "us" as defaulted')
            country = 'us'
        if limit != 50:
            self.limit = int(limit)

        infos = []
        #get all albums of name 'albumName'
        jsonResultAlbums = self.__getInfoByArtistName(artistName, country)
        for jsonResultAlbum in jsonResultAlbums:
            DEBUG.p('albumName:%s; collectionName:%s' %
                    (albumName, jsonResultAlbum['collectionName']))
            if jsonResultAlbum['collectionName'] == albumName:
                ret = self.__saveAllInfos(jsonResultAlbum, country)
                if not ret:
                    continue
                infos.append(ret)

        return infos
Ejemplo n.º 16
0
    def __getInfoByArtistName(self, artistName, country):
        if not artistName:
            DEBUG.p('Please special the artist name')
            return false
        if not country:
            DEBUG.p('country undefine, use "us" as defaulted')
            country = 'us'

        url = self.baseUrl + 'term=%s&country=%s&media=music&entity=album&limit=%d' % (
            artistName, country, self.limit)
        DEBUG.p(url)
        jsonResultAlbums = self.__getResponeResult(url)
        DEBUG.p('result count: %d' % (len(jsonResultAlbums)))
        return jsonResultAlbums
Ejemplo n.º 17
0
def __saveCoverImage(albumDir, coverImageUrl):
        try:
            f = urllib2.urlopen(coverImageUrl)
            with open(albumDir + '/' + coverImageUrl.split('/')[-1], 'wb') as code:
                code.write(f.read())
            DEBUG.p('%s Pic Saved!' % (coverImageUrl.split('/')[-1])) 
        except:
            DEBUG.p('%s Pic Saved failed! and try again...' % (coverImageUrl.split('/')[-1])) 
            try:
                f = urllib2.urlopen(coverImageUrl)
                with open(albumDir + '/' + coverImageUrl.split('/')[-1], 'wb') as code:
                    code.write(f.read())
                DEBUG.p('%s Pic Saved!' % (coverImageUrl.split('/')[-1])) 
            except:
                DEBUG.p('err: %s Pic Saved failed!' % (coverImageUrl.split('/')[-1])) 
Ejemplo n.º 18
0
def __saveCoverImage(albumDir, coverImageUrl):
    try:
        f = urllib2.urlopen(coverImageUrl)
        with open(albumDir + '/' + coverImageUrl.split('/')[-1], 'wb') as code:
            code.write(f.read())
        DEBUG.p('%s Pic Saved!' % (coverImageUrl.split('/')[-1]))
    except:
        DEBUG.p('%s Pic Saved failed! and try again...' %
                (coverImageUrl.split('/')[-1]))
        try:
            f = urllib2.urlopen(coverImageUrl)
            with open(albumDir + '/' + coverImageUrl.split('/')[-1],
                      'wb') as code:
                code.write(f.read())
            DEBUG.p('%s Pic Saved!' % (coverImageUrl.split('/')[-1]))
        except:
            DEBUG.p('err: %s Pic Saved failed!' %
                    (coverImageUrl.split('/')[-1]))
Ejemplo n.º 19
0
 def __saveCoverImage(self, album_dir, coverImageUrl):
     try:
         #f = urllib2.urlopen(coverImageUrl)
         #with open(album_dir + '/' + coverImageUrl.split('/')[-1], 'wb') as code:
         #    code.write(f.read())
         data = urllib2.urlopen(coverImageUrl, data=None, timeout=20).read()
         DEBUG.p('got data:')
         f = open(album_dir + '/' + coverImageUrl.split('/')[-1], 'wb')
         f.write(data)
         DEBUG.p('%s Pic Saved!' % (coverImageUrl.split('/')[-1]))
         f.close()
         return 1
     except:
         DEBUG.p('%s Pic Saved failed! and try again...' %
                 (coverImageUrl.split('/')[-1]))
         try:
             data = urllib2.urlopen(coverImageUrl, data=None,
                                    timeout=40).read()
             DEBUG.p('got data:')
             f = open(album_dir + '/' + coverImageUrl.split('/')[-1], 'wb')
             f.write(data)
             DEBUG.p('%s Pic Saved!' % (coverImageUrl.split('/')[-1]))
             f.close()
             return 1
         except:
             DEBUG.p('err: %s Pic Saved failed!' %
                     (coverImageUrl.split('/')[-1]))
             #remove failed-file
             if os.path.exists(album_dir + '/' +
                               coverImageUrl.split('/')[-1]):
                 os.remove(album_dir + '/' + coverImageUrl.split('/')[-1])
             f = open(
                 album_dir + '/' + coverImageUrl.split('/')[-1] + ".failed",
                 "w")
             f.close()
             #save music url into json file
             file = open(os.getcwd() + '/static/failed_url', "ab")
             json.dump((coverImageUrl, album_dir), file)
             file.close()
             return 0
Ejemplo n.º 20
0
    def POST_old(self):
        data = web.input(path={}) 

        coverPath = ''
        needMv = 0
        if data.coverurl:
            DEBUG.p(data.coverurl)
            coverPath = borrowData.getAlbumImageFromUrl(data.coverurl) 
            if len(coverPath) <= 0:
               return 'sorry, get image failed...'
        elif data.path.filename:
            filename = data.path.filename.replace('\\','/')
            coverPath = os.getcwd() +'/static/images/'+ filename
            fout = open(coverPath, 'wb')
            fout.write(data.path.file.read())
            needMv = 0
        else:
            return 'please input the file or coverurl!'

        newInstance = img.getImgInfo(coverPath)
            

        if data.path:
           newInstance['name'] = data.path
           newInstance['path'] = os.getcwd() + '/static/images/' + data.path

        newInstance['des'] = data.des

        if data.ximiurl:
           #musics list
           musics = borrowData.getMusicListFromXiMi(data.ximiurl) 
           print ','.join(musics)
           newInstance['music_contain'] = ';'.join(musics) 
           
           #artist, album_name, year_record
           (newInstance['artist'], newInstance['album_name'], newInstance['year_record']) = borrowData.getArtistAndAlbumFromXiMi(data.ximiurl)          
        else:
           newInstance['artist'] = data.artist
           newInstance['album_name'] = data.album_name
           newInstance['year_record'] = data.year_record
           newInstance['music_contain'] = data.music_contain

        if needMv and newInstance['artist'] and newInstance['album_name']:
           artist_dir = os.getcwd() + '/static/images/' + newInstance['artist'] 
           album_dir = artist_dir + '/' + newInstance['album_name'] 
           if not os.path.exists(artist_dir):
              os.makedirs(artist_dir)
           if not os.path.exists(album_dir):
              os.makedirs(album_dir)
           shutil.move(coverPath, album_dir)
           

        print 'ximiurl: ' + data.ximiurl 
        print 'name: ' + newInstance['name'] 
        print 'album_name: ' + newInstance['album_name']
        print 'artist: ' + newInstance['artist'] 
        print 'year_record' + newInstance['year_record'] 
        print 'des: ' + newInstance['des'] 
        print 'music_contain: ' + newInstance['music_contain'] 
        print 'path: ' + newInstance['path']
        try:
           dao.inster_instance(newInstance)
        except:
           print 'dao.inster_instance failed!'
           print traceback.print_exc() 

        return self.render.coverShow(newInstance)
Ejemplo n.º 21
0
    def POST(self):
        data = web.input(path={}) 
        if data.limit:
           limit = int(data.limit)

        #itunes api
        if data.album_name or data.artist_name:
            if data.country == 'deep' and data.album_name:
               DEBUG.p('get deep creep: %s' % (data.album_name))
               infos = itunesapi.getInfosWithAlbumName_deep(data.album_name, None, data.limit)
            elif data.country == 'deep' and data.artist_name:
               DEBUG.p('get deep creep: %s' % (data.artist_name))
               infos = itunesapi.getInfosWithAritstName_deep(data.artist_name, None, data.limit)
            elif data.album_name and data.artist_name:
               DEBUG.p('get %s by %s' % (data.album_name, data.artist_name))
               infos = itunesapi.getInfosWithAlbumNameAndArtistName(data.album_name, data.artist_name, data.country, data.limit)
            elif data.album_name: 
               DEBUG.p('get album: %s' % (data.album_name))
               infos = itunesapi.getInfosWithAlbumName(data.album_name, data.country, data.limit)
            elif data.artist_name: 
               DEBUG.p('get album by %s' % (data.artist_name))
               infos = itunesapi.getInfosWithArtistName(data.artist_name, data.country, data.limit)
        else:
            DEBUG.p("empty album name or drtist name!")
            infos = [{
                'album_name': '',
                'artist': '',
                'year_record': '12345',
                'music_contain': 'Nothing',
                'path': u'static/images/onePiece.png',
                'cover_name_1200': u'onePiece.png',
                'cover_name_170': u'onePiece.png',
                'cover_name_100': u'onePiece.png',
                'copy_right': u'lewis',
                'track_count': 1,
                'width': 0,
                'height': 0,
                'size': 0,
                'format': 'png',
                'des':'come on boy!!! day day up!!!',
                'url': ''},
                {'album_name': '',
                'artist': '',
                'year_record': '12345',
                'music_contain': 'Nothing',
                'path': u'static/images/onePiece.png',
                'cover_name_1200': u'onePiece.png',
                'cover_name_170': u'onePiece.png',
                'cover_name_100': u'onePiece.png',
                'copy_right': u'lewis',
                'track_count': 1,
                'width': 0,
                'height': 0,
                'size': 0,
                'format': 'png',
                'des':'come on boy!!! day day up!!!',
                'url': ''
            }]
        return self.render.coverShow(infos)
Ejemplo n.º 22
0
    def __saveAllInfos(self, jsonResultAlbum, country):
        #prepare dir
        dir_len = len(jsonResultAlbum['artistName'])
        if dir_len > 255:
            artist_dir = os.getcwd() + '/static/images/' + jsonResultAlbum['artistName'][0:200]
        else:
            artist_dir = os.getcwd() + '/static/images/' + jsonResultAlbum['artistName']
        dir_len = len(jsonResultAlbum['collectionName'])
        if dir_len > 255:
            album_dir = artist_dir + '/' + jsonResultAlbum['collectionName'][0:200]
        else:
            album_dir = artist_dir + '/' + jsonResultAlbum['collectionName']

        if not os.path.exists(artist_dir):
           os.makedirs(artist_dir)
        if not os.path.exists(album_dir):
           os.makedirs(album_dir)
        else:
           DEBUG.p('this album dir exist:%s'%(album_dir))
           result = dao.getById(jsonResultAlbum['artistName'], jsonResultAlbum['collectionName']) 
           DEBUG.pd(result)
           if result != None:
              DEBUG.p('this album info exist in db!!!!')
              return result 
           else:
              DEBUG.pw('this album info not exist in db!!!!')

        #result = dao.getById(jsonResultAlbum['artistName'], jsonResultAlbum['collectionName']) 
        #coverImage_1200 = jsonResultAlbum['artworkUrl100'].replace('100x100', '1200x1200').split('/')[-1]
        #if result != None and os.path.exists(album_dir + '/' + coverImage_1200):
        #   DEBUG.p('this album info exist in db!!!!')
        #   DEBUG.p('album:%s; artist:%s'%( jsonResultAlbum['collectionName'], jsonResultAlbum['artistName']))
        #   return result

        #save album info json file
        file = open(album_dir + "/album.json","w")
        json.dump(jsonResultAlbum, file)
        file.close()

        
        ##save album cover images
        ret = 0 
        coverImageUrl = jsonResultAlbum['artworkUrl100']
        ret += self.__saveCoverImage(album_dir, coverImageUrl)
        coverImageUrl_170 = coverImageUrl.replace('100x100', '170x170')
        ret += self.__saveCoverImage(album_dir, coverImageUrl_170)
        coverImageUrl_600 = coverImageUrl.replace('100x100', '600x600')
        ret += self.__saveCoverImage(album_dir, coverImageUrl_600)
        coverImageUrl_1200 = coverImageUrl.replace('100x100', '1200x1200')
        ret += self.__saveCoverImage(album_dir, coverImageUrl_1200)
        if ret == 0:
           return ret 

        info = {
                'album_name': '',
                'artist': '',
                'year_record': '12345',
                'music_contain': 'Nothing',
                'path': u'static/images/',
                'cover_name_1200': u'onePiece.png',
                'cover_name_170': u'onePiece.png',
                'cover_name_100': u'onePiece.png',
                'copy_right': u'lewis',
                'track_count': 1,
                'width': 0,
                'height': 0,
                'size': 0,
                'format': 'jpeg',
                'des':'come on boy!!! day day up!!!',
                'itunes_album_url': '',
                'itunes_artist_url': ''
        }
        #DEBUG.pd(jsonResultAlbum)
        info['path'] = album_dir + '/'
        info['cover_name_1200'] = coverImageUrl_1200.split('/')[-1] 
        info['cover_name_170'] = coverImageUrl_170.split('/')[-1] 
        info['cover_name_100'] = coverImageUrl.split('/')[-1] 
        info['album_name'] = jsonResultAlbum['collectionName'] 
        info['artist'] = jsonResultAlbum['artistName'] 
        info['cover_name'] = coverImageUrl_1200.split('/')[-1] 
        info['year_record'] = jsonResultAlbum['releaseDate']
        info['track_count'] = jsonResultAlbum['trackCount']
        info['itunes_album_url'] = jsonResultAlbum['collectionViewUrl']
        if jsonResultAlbum.has_key('artistViewUrl'): 
           info['itunes_artist_url'] = jsonResultAlbum['artistViewUrl']
        if jsonResultAlbum.has_key('copyright'): 
           info['copy_right'] = jsonResultAlbum['copyright']

        musicContains = []
        jsonMusics = []
        jsonResultMusics = self.__getMusicLists(jsonResultAlbum['collectionName'], country)
        if len(jsonResultMusics) == jsonResultAlbum['trackCount']: 
           for jsonResultMusic in jsonResultMusics:
               musicContains.append(jsonResultMusic['trackName'])
           jsonMusics = jsonResultMusics
        else:
           for jsonResultMusic in jsonResultMusics:
               if jsonResultMusic['artistName'] == jsonResultAlbum['artistName']:
                  musicContains.append(jsonResultMusic['trackName'])
                  jsonMusics.append(jsonResultMusic)
        try:
            if len(jsonMusics) == 0:
               for jsonResultMusic in jsonResultMusics:
                   if jsonResultMusic['collectionId'] == jsonResultAlbum['collectionId']:
                      musicContains.append(jsonResultMusic['trackName'])
                      jsonMusics.append(jsonResultMusic)
        except:
            DEBUG.p("empty music list!!!")

        #save music list json file
        file = open(album_dir + "/album_musics.json","w")
        json.dump(jsonMusics, file)
        file.close()

        info['music_contain'] = list(set(musicContains)) 
        DEBUG.pd(info)

        try:
            #record info into db
            dao.addOneDoc(info)
        except:
            print 'add doc failed: %s:%s'%(info['artist'], info['album_name'])
            print traceback.print_exc()
            

        return info
Ejemplo n.º 23
0
 def __saveCoverImage(self, album_dir, coverImageUrl):
     try:
         #f = urllib2.urlopen(coverImageUrl)
         #with open(album_dir + '/' + coverImageUrl.split('/')[-1], 'wb') as code:
         #    code.write(f.read())
         data = urllib2.urlopen(coverImageUrl, data=None, timeout=20).read()
         DEBUG.p('got data:') 
         f = open(album_dir + '/' + coverImageUrl.split('/')[-1], 'wb')
         f.write(data)
         DEBUG.p('%s Pic Saved!' % (coverImageUrl.split('/')[-1])) 
         f.close()
         return 1 
     except:
         DEBUG.p('%s Pic Saved failed! and try again...' % (coverImageUrl.split('/')[-1])) 
         try:
             data = urllib2.urlopen(coverImageUrl, data=None, timeout=40).read()
             DEBUG.p('got data:') 
             f = open(album_dir + '/' + coverImageUrl.split('/')[-1], 'wb')
             f.write(data)
             DEBUG.p('%s Pic Saved!' % (coverImageUrl.split('/')[-1])) 
             f.close()
             return 1 
         except:
             DEBUG.p('err: %s Pic Saved failed!' % (coverImageUrl.split('/')[-1])) 
             #remove failed-file
             if os.path.exists(album_dir + '/' + coverImageUrl.split('/')[-1]):
                 os.remove(album_dir + '/' + coverImageUrl.split('/')[-1])
             f = open(album_dir + '/' + coverImageUrl.split('/')[-1] + ".failed", "w")
             f.close()
             #save music url into json file
             file = open(os.getcwd() + '/static/failed_url',"ab")
             json.dump((coverImageUrl,album_dir), file)
             file.close()
             return 0 
Ejemplo n.º 24
0
    def __saveAllInfos(self, jsonResultAlbum, country):
        #prepare dir
        dir_len = len(jsonResultAlbum['artistName'])
        if dir_len > 255:
            artist_dir = os.getcwd(
            ) + '/static/images/' + jsonResultAlbum['artistName'][0:200]
        else:
            artist_dir = os.getcwd(
            ) + '/static/images/' + jsonResultAlbum['artistName']
        dir_len = len(jsonResultAlbum['collectionName'])
        if dir_len > 255:
            album_dir = artist_dir + '/' + jsonResultAlbum['collectionName'][
                0:200]
        else:
            album_dir = artist_dir + '/' + jsonResultAlbum['collectionName']

        if not os.path.exists(artist_dir):
            os.makedirs(artist_dir)
        if not os.path.exists(album_dir):
            os.makedirs(album_dir)
        else:
            DEBUG.p('this album dir exist:%s' % (album_dir))
            result = dao.getById(jsonResultAlbum['artistName'],
                                 jsonResultAlbum['collectionName'])
            DEBUG.pd(result)
            if result != None:
                DEBUG.p('this album info exist in db!!!!')
                return result
            else:
                DEBUG.pw('this album info not exist in db!!!!')

        #result = dao.getById(jsonResultAlbum['artistName'], jsonResultAlbum['collectionName'])
        #coverImage_1200 = jsonResultAlbum['artworkUrl100'].replace('100x100', '1200x1200').split('/')[-1]
        #if result != None and os.path.exists(album_dir + '/' + coverImage_1200):
        #   DEBUG.p('this album info exist in db!!!!')
        #   DEBUG.p('album:%s; artist:%s'%( jsonResultAlbum['collectionName'], jsonResultAlbum['artistName']))
        #   return result

        #save album info json file
        file = open(album_dir + "/album.json", "w")
        json.dump(jsonResultAlbum, file)
        file.close()

        ##save album cover images
        ret = 0
        coverImageUrl = jsonResultAlbum['artworkUrl100']
        ret += self.__saveCoverImage(album_dir, coverImageUrl)
        coverImageUrl_170 = coverImageUrl.replace('100x100', '170x170')
        ret += self.__saveCoverImage(album_dir, coverImageUrl_170)
        coverImageUrl_600 = coverImageUrl.replace('100x100', '600x600')
        ret += self.__saveCoverImage(album_dir, coverImageUrl_600)
        coverImageUrl_1200 = coverImageUrl.replace('100x100', '1200x1200')
        ret += self.__saveCoverImage(album_dir, coverImageUrl_1200)
        if ret == 0:
            return ret

        info = {
            'album_name': '',
            'artist': '',
            'year_record': '12345',
            'music_contain': 'Nothing',
            'path': u'static/images/',
            'cover_name_1200': u'onePiece.png',
            'cover_name_170': u'onePiece.png',
            'cover_name_100': u'onePiece.png',
            'copy_right': u'lewis',
            'track_count': 1,
            'width': 0,
            'height': 0,
            'size': 0,
            'format': 'jpeg',
            'des': 'come on boy!!! day day up!!!',
            'itunes_album_url': '',
            'itunes_artist_url': ''
        }
        #DEBUG.pd(jsonResultAlbum)
        info['path'] = album_dir + '/'
        info['cover_name_1200'] = coverImageUrl_1200.split('/')[-1]
        info['cover_name_170'] = coverImageUrl_170.split('/')[-1]
        info['cover_name_100'] = coverImageUrl.split('/')[-1]
        info['album_name'] = jsonResultAlbum['collectionName']
        info['artist'] = jsonResultAlbum['artistName']
        info['cover_name'] = coverImageUrl_1200.split('/')[-1]
        info['year_record'] = jsonResultAlbum['releaseDate']
        info['track_count'] = jsonResultAlbum['trackCount']
        info['itunes_album_url'] = jsonResultAlbum['collectionViewUrl']
        if jsonResultAlbum.has_key('artistViewUrl'):
            info['itunes_artist_url'] = jsonResultAlbum['artistViewUrl']
        if jsonResultAlbum.has_key('copyright'):
            info['copy_right'] = jsonResultAlbum['copyright']

        musicContains = []
        jsonMusics = []
        jsonResultMusics = self.__getMusicLists(
            jsonResultAlbum['collectionName'], country)
        if len(jsonResultMusics) == jsonResultAlbum['trackCount']:
            for jsonResultMusic in jsonResultMusics:
                musicContains.append(jsonResultMusic['trackName'])
            jsonMusics = jsonResultMusics
        else:
            for jsonResultMusic in jsonResultMusics:
                if jsonResultMusic['artistName'] == jsonResultAlbum[
                        'artistName']:
                    musicContains.append(jsonResultMusic['trackName'])
                    jsonMusics.append(jsonResultMusic)
        try:
            if len(jsonMusics) == 0:
                for jsonResultMusic in jsonResultMusics:
                    if jsonResultMusic['collectionId'] == jsonResultAlbum[
                            'collectionId']:
                        musicContains.append(jsonResultMusic['trackName'])
                        jsonMusics.append(jsonResultMusic)
        except:
            DEBUG.p("empty music list!!!")

        #save music list json file
        file = open(album_dir + "/album_musics.json", "w")
        json.dump(jsonMusics, file)
        file.close()

        info['music_contain'] = list(set(musicContains))
        DEBUG.pd(info)

        try:
            #record info into db
            dao.addOneDoc(info)
        except:
            print 'add doc failed: %s:%s' % (info['artist'],
                                             info['album_name'])
            print traceback.print_exc()

        return info