Created
May 31, 2015 20:39
-
-
Save raphaelbussa/7f1a321c0cba4556df5a to your computer and use it in GitHub Desktop.
A simple parser for new video from a YouTube user, remember add Jsoup (http://jsoup.org/) in your project for parse XML
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
/* | |
* The MIT License (MIT) | |
* | |
* Copyright (c) 2015 Raphael Bussa <raphaelbussa@gmail.com> | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
package rebus.youtube.parser.sample; | |
import java.io.Serializable; | |
public class YouTubeItem implements Serializable { | |
private String videoId; | |
private String title; | |
private String pubDate; | |
private String thumbnail; | |
private String description; | |
private String statistics; | |
public String getVideoId() { | |
return videoId; | |
} | |
public void setVideoId(String videoId) { | |
this.videoId = videoId; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public String getPubDate() { | |
return pubDate; | |
} | |
public void setPubDate(String pubDate) { | |
this.pubDate = pubDate; | |
} | |
public String getThumbnail() { | |
return thumbnail; | |
} | |
public void setThumbnail(String thumbnail) { | |
this.thumbnail = thumbnail; | |
} | |
public String getDescription() { | |
return description; | |
} | |
public void setDescription(String description) { | |
this.description = description; | |
} | |
public String getStatistics() { | |
return statistics; | |
} | |
public void setStatistics(String statistics) { | |
this.statistics = statistics; | |
} | |
} |
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
/* | |
* The MIT License (MIT) | |
* | |
* Copyright (c) 2015 Raphael Bussa <raphaelbussa@gmail.com> | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
package rebus.youtube.parser.sample; | |
import android.os.AsyncTask; | |
import org.jsoup.Jsoup; | |
import org.jsoup.nodes.Document; | |
import org.jsoup.nodes.Element; | |
import org.jsoup.select.Elements; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
public class YouTubeParser extends AsyncTask { | |
private String USER; | |
private ArrayList<YouTubeItem> youTubeItems; | |
private OnComplete onComplete; | |
public YouTubeParser(String USER) { | |
this.USER = USER; | |
} | |
public interface OnComplete { | |
void onFinish(ArrayList<YouTubeItem> youTubeItems); | |
void onError(); | |
} | |
public void setOnComplete (OnComplete onComplete) { | |
this.onComplete = onComplete; | |
} | |
@Override | |
protected void onPreExecute() { | |
youTubeItems = new ArrayList<>(); | |
} | |
@Override | |
protected Object doInBackground(Object[] params) { | |
try { | |
Document feed = Jsoup.connect("https://www.youtube.com/feeds/videos.xml?user=" + USER) | |
.userAgent("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22") | |
.timeout(60000).ignoreContentType(true).get(); | |
Elements entrys = feed.getElementsByTag("entry"); | |
for (Element entry : entrys) { | |
YouTubeItem youTubeItem = new YouTubeItem(); | |
youTubeItem.setVideoId(entry.getElementsByTag("yt:videoId").text()); | |
youTubeItem.setTitle(entry.getElementsByTag("title").text()); | |
youTubeItem.setPubDate(entry.getElementsByTag("published").text()); | |
youTubeItem.setThumbnail("http://img.youtube.com/vi/" + entry.getElementsByTag("yt:videoId").text() + "/default.jpg"); | |
youTubeItem.setDescription(entry.getElementsByTag("media:description").text()); | |
youTubeItem.setStatistics(entry.select("media|statistics").attr("views")); | |
youTubeItems.add(youTubeItem); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(Object o) { | |
if (youTubeItems.size() == 0) { | |
onComplete.onError(); | |
} else { | |
onComplete.onFinish(youTubeItems); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment