Ejemplo n.º 1
0
def readPlaylistFromDB():
  playlistDict = {}
  effectivePlaylist = DBProcess.getEffectivePlaylist()
  for pid in effectivePlaylist.keys():
    pList = Playlist(pid,effectivePlaylist[pid])
    playlistDict[pid] = pList
  print 'Thare are %d playlist have been read.' % len(playlistDict)
  return playlistDict
Ejemplo n.º 2
0
def writePlaylistsToFile():
  filename = "txt/playlists.txt"
  if os.path.exists(filename):
    print '%s is existing......' % filename
    return
  else:
    print 'Begin to write playlists......'
    pFile = open(filename,"w")
    effectivePlaylist = DBProcess.getEffectivePlaylist()
    for pid in effectivePlaylist.keys():
      pList = effectivePlaylist[pid]
      content = "%d:" % pid
      count = len(pList)
      for i in range(0,count-1):
        content = "%s%d," % (content,pList[i])
      content = "%s%d" % (content,pList[count-1])
      pFile.write(content+'\n')
    pFile.close()
    print 'End of writing playlists......'
Ejemplo n.º 3
0
def generateDocs():
    #import global variable
    global DBHOST
    global DBUSER
    global DBPWD
    global DBPORT
    global DBNAME
    global DBCHARSET

    #rm folder songs
    rmDir("data/songs")
    #mkdir songs
    os.mkdir("data/songs")

    try:
        #connect db and select db name
        conn = MySQLdb.Connect(host=DBHOST,
                               user=DBUSER,
                               passwd=DBPWD,
                               port=DBPORT,
                               charset=DBCHARSET)
        cur = conn.cursor()
        conn.select_db(DBNAME)
        #get song dict and playlist dict from DBProcess
        songDict, playlistDict = DBProcess.genEffectivePlaylist()
        #tags'count:number
        countDict = {}
        #listeners'count:number
        lisDict = {}
        #playcount:number
        playDict = {}
        songNum = len(songDict)
        index = 0
        for sid in songDict.keys():
            index = index + 1
            print 'begin to generate file of song %d(%d/%d)' % (sid, index,
                                                                songNum)
            logging.debug('begin to generate file of song %d(%d/%d)' %
                          (sid, index, songNum))
            #select info of a song with sid
            cur.execute(
                'select sname,aname,count,tags,listeners,playcount,useful from effective_song where id = %d'
                % sid)
            result = cur.fetchone()
            sname = result[0]
            aname = result[1]
            count = int(result[2])
            #update count dict
            if count not in countDict:
                countDict[count] = 1
            else:
                countDict[count] = countDict[count] + 1
            tags = result[3]
            #update listener dict
            listeners = int(result[4])
            if listeners not in lisDict:
                lisDict[listeners] = 1
            else:
                lisDict[listeners] = lisDict[listeners] + 1
            #update playcount dict
            playcount = int(result[5])
            if playcount not in playDict:
                playDict[playcount] = 1
            else:
                playDict[playcount] = playDict[playcount] + 1

            useful = int(result[6])
            if useful == 0:
                print '%d useful is 0...' % sid
                logging.warning('%d useful is 0...' % sid)
                return

            tagDict = generateTagDictofSong(sname, aname, tags)
            generateDocofSong(sid, tagDict)

            print 'end of generating file of song %d' % sid
            logging.debug('end of generating file of song %d' % sid)
        conn.commit()
        cur.close()
        conn.close()
        print 'There are %d different tag count' % len(countDict)
        print 'There are %d different listener numbers' % len(lisDict)
        print 'There are %d different playcount numbers' % len(playDict)
        return countDict, lisDict, playDict
    except MySQLdb.Error, e:
        print 'Mysql Error %d:%s' % (e.args[0], e.args[1])
        logging.error('Mysql Error %d:%s' % (e.args[0], e.args[1]))
Ejemplo n.º 4
0
def generateDocs():
  #import global variable
  global DBHOST
  global DBUSER
  global DBPWD
  global DBPORT
  global DBNAME
  global DBCHARSET

  #rm folder songs
  rmDir("data/songs")
  #mkdir songs
  os.mkdir("data/songs")  

  try:
    #connect db and select db name
    conn = MySQLdb.Connect(host=DBHOST,user=DBUSER,passwd=DBPWD,port=DBPORT,charset=DBCHARSET)
    cur = conn.cursor()
    conn.select_db(DBNAME)
    #get song dict and playlist dict from DBProcess
    songDict,playlistDict = DBProcess.genEffectivePlaylist()
    #tags'count:number
    countDict = {}
    #listeners'count:number
    lisDict = {}
    #playcount:number
    playDict = {}
    songNum = len(songDict)
    index = 0
    for sid in songDict.keys():
      index = index + 1
      print 'begin to generate file of song %d(%d/%d)' % (sid,index,songNum)
      logging.debug('begin to generate file of song %d(%d/%d)' % (sid,index,songNum))
      #select info of a song with sid
      cur.execute('select sname,aname,count,tags,listeners,playcount,useful from effective_song where id = %d' % sid)
      result = cur.fetchone()
      sname = result[0]
      aname = result[1]
      count = int(result[2])
      #update count dict
      if count not in countDict:
        countDict[count] = 1
      else:
        countDict[count] = countDict[count] + 1
      tags = result[3]
      #update listener dict
      listeners = int(result[4])
      if listeners not in lisDict:
        lisDict[listeners] = 1
      else:
        lisDict[listeners] = lisDict[listeners] + 1
      #update playcount dict
      playcount = int(result[5])
      if playcount not in playDict:
        playDict[playcount] = 1
      else:
        playDict[playcount] = playDict[playcount] + 1

      useful = int(result[6])
      if useful == 0:
        print '%d useful is 0...' % sid
        logging.warning('%d useful is 0...' % sid)
        return

      tagDict = generateTagDictofSong(sname,aname,tags)
      generateDocofSong(sid,tagDict)
    
      print 'end of generating file of song %d' % sid
      logging.debug('end of generating file of song %d' % sid)
    conn.commit()
    cur.close()
    conn.close()
    print 'There are %d different tag count' % len(countDict)
    print 'There are %d different listener numbers' % len(lisDict)
    print 'There are %d different playcount numbers' % len(playDict)
    return countDict,lisDict,playDict
  except MySQLdb.Error,e:
    print 'Mysql Error %d:%s' % (e.args[0],e.args[1])
    logging.error('Mysql Error %d:%s' % (e.args[0],e.args[1]))