Last active
December 19, 2015 18:17
-
-
Save faogustavo/c7aa943be4f765f849b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.xml.stream.XMLEventReader; | |
import javax.xml.stream.XMLInputFactory; | |
import javax.xml.stream.XMLStreamException; | |
import javax.xml.stream.events.Attribute; | |
import javax.xml.stream.events.Characters; | |
import javax.xml.stream.events.XMLEvent; | |
import java.io.*; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
public class ItunesFeedDownloader { | |
private static final String NOT_ALLOWED_CHARACTERS = "\\/*?\"<>|"; | |
private static final String NERD_CAST = "http://jovemnerd.com.br/categoria/nerdcast/feed/"; | |
private static final String RAPADURA_CAST = "http://feeds.feedburner.com/rapaduracast"; | |
private static final String CAFE_BRASIL_CAST = "http://www.portalcafebrasil.com.br/tools/lidercast.xml"; | |
private static final String HACK_N_CAST = "http://feeds.feedburner.com/hack-n-cast"; | |
private static final Integer MAX_DOWNLOADS = 8; | |
private static final String DOWNLOAD_PATH = "F:\\Documentos\\NerdCast\\"; | |
private static final String URL = NERD_CAST; | |
private int numDownloads; | |
private int pos; | |
public void go() { | |
File f = new File(DOWNLOAD_PATH); | |
if (!f.exists()) { | |
if (!f.mkdirs()) { | |
System.out.println("Não foi possível criar a pasta"); | |
return; | |
} | |
} | |
RSSFeedParser parser = new RSSFeedParser(URL); | |
Feed feed = parser.readFeed(); | |
System.out.println(feed); | |
numDownloads = 0; | |
pos = feed.getMessages().size() - 1; | |
Download(feed); | |
} | |
public void Download(Feed feed) { | |
if (pos < 0) { | |
return; | |
} | |
FeedMessage message = feed.getMessages().get(pos); | |
numDownloads++; | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
System.out.println("Downloading file " + message.getTitle()); | |
Download(message); | |
System.out.println("Finalizou file " + message.getTitle()); | |
numDownloads--; | |
pos--; | |
Download(feed); | |
} | |
}).start(); | |
if (numDownloads < MAX_DOWNLOADS) { | |
pos--; | |
Download(feed); | |
} | |
} | |
public void Download(FeedMessage message) { | |
try { | |
URL url = new URL(message.getEnclosure()); | |
HttpURLConnection httpConnection = (HttpURLConnection) (url.openConnection()); | |
long completeFileSize = httpConnection.getContentLength(); | |
String name = ""; | |
for (char c : message.getTitle().toCharArray()) { | |
if (!NOT_ALLOWED_CHARACTERS.contains(c + "")) { | |
name += c; | |
} | |
} | |
String fileName = DOWNLOAD_PATH + name + ".mp3"; | |
java.io.BufferedInputStream in = new java.io.BufferedInputStream(httpConnection.getInputStream()); | |
java.io.FileOutputStream fos = new java.io.FileOutputStream(fileName); | |
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos, 1024); | |
byte[] data = new byte[1024]; | |
long downloadedFileSize = 0; | |
int x = 0; | |
while ((x = in.read(data, 0, 1024)) >= 0) { | |
downloadedFileSize += x; | |
bout.write(data, 0, x); | |
} | |
bout.close(); | |
in.close(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) { | |
new ItunesFeedDownloader().go(); | |
} | |
public class RSSFeedParser { | |
static final String TITLE = "title"; | |
static final String LINK = "link"; | |
static final String ITEM = "item"; | |
static final String ENCLOSURE = "enclosure"; | |
static final String URL = "url"; | |
final URL url; | |
public RSSFeedParser(String feedUrl) { | |
try { | |
this.url = new URL(feedUrl); | |
} catch (MalformedURLException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public Feed readFeed() { | |
Feed feed = null; | |
try { | |
boolean isFeedHeader = true; | |
// Set header values intial to the empty string | |
String description = ""; | |
String title = ""; | |
String link = ""; | |
String language = ""; | |
String copyright = ""; | |
String author = ""; | |
String pubdate = ""; | |
String guid = ""; | |
String enclosure = ""; | |
// First create a new XMLInputFactory | |
XMLInputFactory inputFactory = XMLInputFactory.newInstance(); | |
// Setup a new eventReader | |
InputStream in = read(); | |
XMLEventReader eventReader = inputFactory.createXMLEventReader(in); | |
// read the XML document | |
while (eventReader.hasNext()) { | |
XMLEvent event = eventReader.nextEvent(); | |
if (event.isStartElement()) { | |
String localPart = event.asStartElement().getName() | |
.getLocalPart(); | |
switch (localPart) { | |
case ITEM: | |
if (isFeedHeader) { | |
isFeedHeader = false; | |
feed = new Feed(); | |
} | |
event = eventReader.nextEvent(); | |
break; | |
case TITLE: | |
title = getCharacterData(event, eventReader); | |
break; | |
case LINK: | |
link = getCharacterData(event, eventReader); | |
break; | |
case ENCLOSURE: | |
Iterator<Attribute> attrs = event.asStartElement().getAttributes(); | |
enclosure = ""; | |
while (attrs.hasNext()) { | |
Attribute attr = attrs.next(); | |
if (attr.getName().toString().equals(URL)) { | |
enclosure = attr.getValue(); | |
} | |
} | |
break; | |
} | |
} else if (event.isEndElement()) { | |
if (event.asEndElement().getName().getLocalPart() == (ITEM)) { | |
FeedMessage message = new FeedMessage(); | |
message.setLink(link); | |
message.setTitle(title); | |
message.setEnclosure(enclosure); | |
feed.getMessages().add(message); | |
event = eventReader.nextEvent(); | |
continue; | |
} | |
} | |
} | |
} catch (XMLStreamException e) { | |
throw new RuntimeException(e); | |
} | |
return feed; | |
} | |
private String getCharacterData(XMLEvent event, XMLEventReader eventReader) | |
throws XMLStreamException { | |
String result = ""; | |
event = eventReader.nextEvent(); | |
if (event instanceof Characters) { | |
result = event.asCharacters().getData(); | |
} | |
return result; | |
} | |
private InputStream read() { | |
try { | |
return url.openStream(); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} | |
public class Feed { | |
final List<FeedMessage> entries = new ArrayList<FeedMessage>(); | |
public Feed(){} | |
public List<FeedMessage> getMessages() { | |
return entries; | |
} | |
} | |
public class FeedMessage { | |
String title; | |
String link; | |
String enclosure; | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public String getLink() { | |
return link; | |
} | |
public void setLink(String link) { | |
this.link = link; | |
} | |
public String getEnclosure() { | |
return enclosure; | |
} | |
public void setEnclosure(String enclosure) { | |
this.enclosure = enclosure; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Para fazer o download, altere o valor das variáveis MAX_DOWNLOADS, DOWNLOAD_PATH e URL nas linhas 24, 25 e 26.
- MAX_DOWNLOADS: Número de downloads simultâneos
- DOWNLOAD_PATH: Local onde serão salvos os Downloads
- URL: URL do Feed do Itunes para fazer o Download