Jump to content

lamentos

Active Members
  • Posts

    10
  • Joined

  • Last visited

Posts posted by lamentos

  1. ok, here goes the whole class

    (you know you have to recompile the project and create a new jar, don't you?)

    --------------------------------------------------

    package util;

    import org.blinkenlights.jid3.ID3Exception;

    import org.blinkenlights.jid3.MediaFile;

    import org.blinkenlights.jid3.MP3File;

    import org.blinkenlights.jid3.v2.ID3V2_3_0Tag;

    import org.blinkenlights.jid3.v2.APICID3V2Frame;

    import org.apache.commons.logging.Log;

    import org.apache.commons.logging.LogFactory;

    import java.io.*;

    import java.util.Arrays;

    import java.util.Comparator;

    import enums.FileType;

    public class Mp3Processor {

    private static String ERR_TMP_NOT_FOUND = "unable to find file make sure you have Pandora running in a FIRFOX browser";

    private static Log LOG = LogFactory.getLog(Mp3Processor.class);

    public static File findCurrentMP3() throws IOException {

    LOG.info("locating mp3");

    File pandoraTempDir = findUserTmpDir();

    File tmpMp3File = findCurrentTmpMp3(pandoraTempDir);

    return copyFile(tmpMp3File, new File(tmpMp3File.getParent(), "temp"));

    }

    public static File findCurrentTmpMp3(File pandoraTempDir) {

    File tmpMp3File2 = null;

    try {

    tmpMp3File2 = getAccessFile(pandoraTempDir);

    if (tmpMp3File2 == null) {

    throw new RuntimeException("access file could not be located");

    }

    } catch (Exception e) {

    if (e.getMessage().equals(ERR_TMP_NOT_FOUND)) {

    LOG.info("access file not found trying again after 1 sec");

    try {

    Thread.sleep(2000);

    } catch (InterruptedException e1) {

    throw new RuntimeException(e1);

    }

    return findCurrentTmpMp3(pandoraTempDir);

    }

    }

    try {

    LOG.debug("sleeping for a couple of seconds");

    Thread.sleep(2000);

    } catch (InterruptedException e) {

    return tmpMp3File2;

    }

    File currentTmpMp3 = getAccessFile(pandoraTempDir);

    LOG.info(currentTmpMp3.getName() + "|" + tmpMp3File2.getName());

    if (currentTmpMp3.getName().equals(tmpMp3File2.getName())) {

    return tmpMp3File2;

    }

    LOG.info("Difference found in temp file names looping again");

    return findCurrentTmpMp3(pandoraTempDir);

    }

    private static File getAccessFile(File pandoraTempDir) {

    File [] files = pandoraTempDir.listFiles();

    sortFilesByMostRecent(files);

    boolean foundBuffer = false;

    File tmpMp3File1 = null;

    File tmpMp3File2 = null;

    for (File file : files) {

    try{

    Long.parseLong(file.getName());

    // if (file.getName().indexOf("access") != -1) {

    if (!foundBuffer) {

    tmpMp3File1 = file;

    foundBuffer = true;

    } else {

    tmpMp3File2 = file;

    break;

    }

    }catch(NumberFormatException e){

    LOG.debug(file.getName()+" is not a long");

    }

    }

    if (tmpMp3File2 == null) {

    throw new RuntimeException(ERR_TMP_NOT_FOUND);

    }

    long tmp1Size = tmpMp3File1.length();

    long tmp2Size = tmpMp3File2.length();

    int loopCount = 0;

    while (true) {

    try {

    LOG.debug("comparing file after 1 sec: tmp2Size: " + tmp2Size);

    Thread.sleep(1000);

    if ((tmp1Size == tmpMp3File1.length() && tmp2Size == tmpMp3File2.length() && (tmp2Size > (1024 * 1000)))) {

    break;

    } else {

    String absolutePath = tmpMp3File2.getAbsolutePath();

    LOG.debug("absolutePath> " + absolutePath);

    tmpMp3File2 = new File(absolutePath);

    LOG.debug("buffer still building, looping");

    tmp1Size = tmpMp3File1.length();

    tmp2Size = tmpMp3File2.length();

    }

    } catch (InterruptedException e) {

    break;

    }

    if (loopCount > 40) {

    break;

    }

    loopCount ++;

    }

    return tmpMp3File2;

    }

    public static File findUserTmpDir() {

    File tmpDir = new File(System.getenv("TEMP"));

    File[] tempFiles = tmpDir.listFiles();

    sortFilesByMostRecent(tempFiles);

    File pandoraTempDirv = null;

    for (File file : tempFiles) {

    if (file.getName().indexOf("plugtmp") != -1) {

    pandoraTempDirv = file;

    break;

    }

    }

    if (pandoraTempDirv == null) {

    throw new RuntimeException(ERR_TMP_NOT_FOUND);

    }

    return pandoraTempDirv;

    }

    public static void sortFilesByMostRecent(File[] files) {

    Arrays.sort(files, new Comparator() {

    public int compare(Object o1, Object o2) {

    if (((File) o1).lastModified() > ((File) o2).lastModified()) {

    return -1;

    } else if (((File) o1).lastModified() < ((File) o2).lastModified()) {

    return +1;

    } else {

    return 0;

    }

    }

    });

    }

    public static File copyFile(File src, File dst) throws IOException {

    LOG.info("copying file[" + src.getAbsolutePath() + "] to temp[" + dst.getAbsolutePath() + "]");

    InputStream in = new FileInputStream(src);

    OutputStream out = new FileOutputStream(dst);

    byte[] buf = new byte[1024];

    int len;

    while ((len = in.read(buf)) > 0) {

    out.write(buf, 0, len);

    }

    in.close();

    out.close();

    return dst;

    }

    public static void addID3Tags(File currentMP3, File coverArt, String comment, SongInfo songInfo) throws ID3Exception, IOException {

    LOG.info("adding ID3 tags: " + songInfo.toString());

    MediaFile oMediaFile = new MP3File(currentMP3);

    ID3V2_3_0Tag oID3V2_3_0Tag = new ID3V2_3_0Tag();

    if (coverArt != null) {

    LOG.info("setting ID3 cover art");

    byte[] buffer = new byte[32 * 1024];

    BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(coverArt));

    ByteArrayOutputStream imageBinary = new ByteArrayOutputStream();

    while (bufferedInputStream.read(buffer) != -1) {

    imageBinary.write(buffer);

    }

    APICID3V2Frame apicid3V2Frame = new APICID3V2Frame("image/jpg", APICID3V2Frame.PictureType.Artist, songInfo.getAlbum() != null ? songInfo.getAlbum() : "unknown", imageBinary.toByteArray());

    oID3V2_3_0Tag.addAPICFrame(apicid3V2Frame);

    }

    if (songInfo.getAlbum() != null) {

    oID3V2_3_0Tag.setAlbum(songInfo.getAlbum());

    }

    if (songInfo.getArtist() != null) {

    oID3V2_3_0Tag.setArtist(songInfo.getArtist());

    }

    if (comment != null) {

    if (songInfo.getStationName() != null) {

    oID3V2_3_0Tag.setComment(songInfo.getStationName());

    } else {

    oID3V2_3_0Tag.setComment(comment);

    }

    }

    if (songInfo.getTrackNumber() != null) {

    Integer i = new Integer(songInfo.getTrackNumber());

    oID3V2_3_0Tag.setTrackNumber(i);

    }

    if (songInfo.getGenre() != null) {

    oID3V2_3_0Tag.setGenre(songInfo.getGenre());

    }

    if (songInfo.getTitle() != null) {

    oID3V2_3_0Tag.setTitle(songInfo.getTitle());

    }

    if (songInfo.getYear() != null) {

    try {

    Integer y = new Integer(songInfo.getYear());

    oID3V2_3_0Tag.setYear(y);

    } catch (NumberFormatException e) {

    //ignore

    }

    }

    oMediaFile.setID3Tag(oID3V2_3_0Tag);

    oMediaFile.sync();

    }

    public static void saveMP3(SongInfo songInfo, File fileToSave, FileType fileType, DataOutputStream output) throws IOException {

    File file = new File(Util.createSongHierarchy(songInfo), SafeString.getSafeFileName(songInfo.getArtist().trim() + "-" + songInfo.getTitle().trim()) + "." + fileType.toString().toLowerCase());

    Mp3Processor.copyFile(fileToSave, file);

    String mp3Path = file.getAbsolutePath();

    LOG.info("nripped file to: " + mp3Path + "nyea baby!!!n");

    if (fileType == FileType.MP3) {

    output.writeBytes("<span style="font: 12px arial">MP3 tagged and saved:</span><br/><span style="font: 12px arial">" + mp3Path + "</span>");

    }

    }

    }

    ------------------------------------------------------------------

  2. the old code was

    for (File file : files) {

    if (file.getName().indexOf("access") != -1) {

    if (!foundBuffer) {

    tmpMp3File1 = file;

    foundBuffer = true;

    } else {

    tmpMp3File2 = file;

    break;

    }

    }

    }

    the new code would be

    for (File file : files) {

    try{

    Long.parseLong(file.getName());

    if (!foundBuffer) {

    tmpMp3File1 = file;

    foundBuffer = true;

    } else {

    tmpMp3File2 = file;

    break;

    }

    }catch(NumberFormatException e){

    LOG.debug(file.getName()+" is not a long");

    }

    }

  3. Hi I've been checking the source code but it seems that classes on enum package are note complete, theyre not really a class they try to be an enum but there are many things left.

    Would you mind updating the source code on the zip file?

    tanks,

    PS: I'm just trying to check why I get so many "access file not found", I did it work once, but never again

×
×
  • Create New...