Skip to content

Commit

Permalink
Remove http redirect manual translation
Browse files Browse the repository at this point in the history
  • Loading branch information
gkiko committed Feb 26, 2017
1 parent 1904244 commit dc6c19e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 32 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<groupId>ge.kuku.movie.parser</groupId>
<artifactId>parser</artifactId>
<packaging>war</packaging>
<version>1.1.2</version>
<version>1.1.3</version>
<name>movieparser</name>
<url>http://maven.apache.org</url>
<build>
Expand Down
39 changes: 8 additions & 31 deletions src/main/java/ge/kuku/movie/parse/data/ImoviesParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public class ImoviesParser implements Parser {
@Override
public List<ImoviesEntity> parse(String movieName, String imdbId) throws IOException {
List<String> urls = getSearchResults(movieName);
List<String> redirectedUrls = handleUrlRedirects(urls);
String pageUrl = whichPageContains(imdbId, redirectedUrls);
String pageUrl = whichPageContains(imdbId, urls);
String imoviesId = getImoviesId(pageUrl);
return parseRss(imoviesId);
}
Expand Down Expand Up @@ -58,27 +57,7 @@ private List<String> getResultItemURLs(String url) throws IOException {
.collect(Collectors.toList());
}

private List<String> handleUrlRedirects(List<String> urls) {
return urls.parallelStream().map(url -> {
try {
Document doc = Jsoup.connect(url).get();
Element script = doc.select("script").last();
String scriptContent = script.html();

String movieName = scriptContent.substring(
scriptContent.lastIndexOf('/'),
scriptContent.lastIndexOf('\''));
return url + movieName;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}).collect(Collectors.toList());
}

private String whichPageContains(String imdbId, List<String> urls) throws IOException {
// find(imdbId, urls.get(0));

for (String url : urls) {
if (find(imdbId, url)) {
return url;
Expand All @@ -88,20 +67,18 @@ private String whichPageContains(String imdbId, List<String> urls) throws IOExce
}

private boolean find(String imdbId, String url) throws IOException {
Document doc = Jsoup.connect(url).get();
Document doc = Jsoup.connect(url).followRedirects(true).get();
Elements divs = doc.select(".imdbtop");
if (divs.isEmpty()) return false;

Element div = divs.first();
Elements links = div.select("a[href]");

for (Element link : links) {
String strLink = link.attr("href");
if (strLink.contains("imdb.com/title")) {
String foundId = strLink.substring(strLink.lastIndexOf("/") + 1);
if (foundId.equals(imdbId)) {
return true;
}
String strLink = links.first().attr("href");
if (strLink.contains("imdb.com/title")) {
String foundId = strLink.substring(strLink.lastIndexOf("/") + 1);
if (foundId.equals(imdbId)) {
return true;
}
}
return false;
Expand All @@ -110,7 +87,7 @@ private boolean find(String imdbId, String url) throws IOException {
private String getImoviesId(String pageUrl) {
if (pageUrl == null) return null;
String[] tokens = pageUrl.split("/");
return tokens[tokens.length - 2];
return tokens[tokens.length - 1];
}

private List<ImoviesEntity> parseRss(String imoviesId) {
Expand Down

0 comments on commit dc6c19e

Please sign in to comment.