Skip to content

Instantly share code, notes, and snippets.

@ramon-rd
Created February 18, 2016 16:19
Show Gist options
  • Save ramon-rd/ab0f659e72f267cf9e7e to your computer and use it in GitHub Desktop.
Save ramon-rd/ab0f659e72f267cf9e7e to your computer and use it in GitHub Desktop.
/*
The MIT License (MIT)
Copyright (c) 2016 sinfonier-project
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 com.sinfonier.bolts;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class ArchiveBolt extends BaseSinfonierBolt {
// Class variables
public String BUSQUEDA;
// Must implement constructor. Do not touch
public ArchiveBolt(String path) {
super(path);
}
@Override
public void userprepare() {
// Initialize your DB Connections or non-serializable classes.
// This method will be executed once.
this.BUSQUEDA = getParam("busqueda");
}
@Override
public void userexecute() {
// TO-DO: Write code here. This method will be called every ingoing tuple
String url = "https://archive.org/search.php?query="+this.getField(this.BUSQUEDA).toString();
Document doc;
try {
// need http protocol
doc = Jsoup.connect(url).get();
// get page title
String title = doc.title();
// get all links
Elements loginform = doc.getElementsByClass("item-ia");
int i = 1;
for (Element link : loginform) {
System.out.println();
if(!link.getElementsByClass("ttl").text().equals("null"))
this.addField("busqueda"+i,link.getElementsByClass("ttl").text());
else
this.addField("busqueda"+i, "0");
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
this.emit();
}
@Override
public void usercleanup() {
// Close connections, clean resources
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment