¿Cómo descargar el comic de la serie Heroes sin publicidad?

septiembre 03, 2009

Si os gusta la serie Heroes tanto como a mi, quizás conozcais también el comic de la misma. Este se actualiza regularmente y cuenta con más de 140 números. Se puede descargar gratis en formato PDF de la página de NBC y hay hasta una aplicación para Iphone. Nissan patrocina el comic poniendo un anuncio en la pequeña página. El siguiente programa de Java, os descargará todos los PDF y eliminará dicha primera página.

 
import java.io.*; 
import java.net.*; 
import com.lowagie.text.Document; 
import com.lowagie.text.DocumentException; 
import com.lowagie.text.pdf.PdfCopy; 
import com.lowagie.text.pdf.PdfImportedPage; 
import com.lowagie.text.pdf.PdfReader; 
public class UrlDownload { 
 public static int numberOfPages = 0; 
 public static boolean OVERWRITE = false; 
 public static String URL = "http://www.nbc.com/Heroes/novels/downloads/"; 
 public static String FOLDER = "HeroesNovel"; 
 public static String OUTPUT = "output"; 
 public static void main(String[] args) { 
  (new File(FOLDER)).mkdir(); 
  (new File(OUTPUT)).mkdir(); 
  for(int i = 1; i < 2000; i++) { 
   String filename = "Heroes_novel_"; 
   if(i < 10) { 
    filename += "00" + i + ".pdf"; 
   } else if((9 < i) && (i < 100)) { 
    filename += "0" + i + ".pdf"; 
   } else { 
    filename += i + ".pdf"; 
   } 
   try { 
    if(!(new File(FOLDER + "/" + filename).exists()) || OVERWRITE) {     
     saveUrl(FOLDER + "/" + filename, URL + "/" + filename); 
     System.out.println(filename + " downloaded!"); 
     removeFirstPage(filename); 
     System.out.println("Removing the first page publicity of " + filename); 
    }    
   } catch (MalformedURLException e) { 
    System.out.println("MalformedURLException"); 
    break; 
   } catch (DocumentException e) { 
    System.out.println("DocumentException"); 
    break; 
   } catch (IOException e) { 
    System.out.println("IOException"); 
    break; 
   }   
  } 
  System.out.println("Number of pages: " + numberOfPages);  
 } 
 public static void saveUrl(String filename, String urlString) throws MalformedURLException, IOException { 
  BufferedInputStream in = null; 
  FileOutputStream fout = null; 
  try { 
   URL u = new URL(urlString); 
   HttpURLConnection huc =  (HttpURLConnection)  u.openConnection(); 
   huc.setRequestMethod("GET");  
   huc.setRequestProperty("User-Agent", " Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)"); 
   huc.connect(); 
   if(huc.getResponseCode()==404) { 
    throw new IOException(); 
   } 
   in = new BufferedInputStream(huc.getInputStream()); 
   fout = new FileOutputStream(filename); 
   byte data[] = new byte[1024]; 
   int count; 
   while ((count = in.read(data, 0, 1024)) != -1) { 
    fout.write(data, 0, count); 
   } 
  } finally { 
   if (in != null) 
    in.close(); 
   if (fout != null) 
    fout.close(); 
  } 
 } 
 public static void removeFirstPage(String pdf_filename) throws DocumentException, IOException { 
  File outFile = new File(OUTPUT + "/" + pdf_filename); 
  PdfReader reader = new PdfReader(FOLDER + "/" + pdf_filename); 
  Document document = new Document(reader.getPageSizeWithRotation(1)); 
  PdfCopy copy = new PdfCopy(document, new FileOutputStream(outFile)); 
  int pages = reader.getNumberOfPages(); 
  numberOfPages += pages; 
  document.open(); 
  if(pages > 0) { 
   for(int i=2; i <= pages;i++) { 
    PdfImportedPage page = copy.getImportedPage(reader, i); 
    copy.addPage(page); 
   } 
  } 
  document.close();   
 } 
 public int getNumberOfPages() { 
  return numberOfPages; 
 } 
 public void setNumberOfPages(int number_of_pages) { 
  numberOfPages = number_of_pages; 
 } 
} 

Para poder compilar el código anterior, necesitareís la librería gratuita iText con la cual podeís maninpular PDF en Java. Heroes vuelve este Otoño, disfrutad de más de 1.000 páginas de comic mientras tanto.

Etiquetas: , ,

0 comentarios:

Publicar un comentario