Skip to content

Instantly share code, notes, and snippets.

@ramon-rd
Last active February 18, 2016 15:29
Show Gist options
  • Save ramon-rd/4af1bdce6aabd8723f77 to your computer and use it in GitHub Desktop.
Save ramon-rd/4af1bdce6aabd8723f77 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.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class LinkedinBolt extends BaseSinfonierBolt {
// Class variables
String ESTUDIOS;
String LOCALIDAD;
// Must implement constructor. Do not touch
public LinkedinBolt(String path) {
super(path);
}
@Override
public void userprepare() {
// Initialize your DB Connections or non-serializable classes.
// This method will be executed once.
this.ESTUDIOS = this.getParam("educacion");
this.LOCALIDAD = this.getParam("localidad");
}
@Override
public void userexecute() {
String url="";
if ( this.getField(this.ESTUDIOS) != "0" && this.getField(this.LOCALIDAD) != "0")
url = "https://www.linkedin.com/jobs/search?keywords="+ this.getField(this.ESTUDIOS)+"&location="+this.getField(this.LOCALIDAD)+"&trk=jobs_jserp_search_button_execute&locationId=";
else if ( this.getField(this.ESTUDIOS) == "0" && this.getField(this.LOCALIDAD) != "0")
url = "https://www.linkedin.com/jobs/search?location="+this.getField(this.LOCALIDAD)+"&trk=jobs_jserp_search_button_execute&locationId=";
else if ( this.getField(this.ESTUDIOS) != "0" && this.getField(this.LOCALIDAD) == "0")
url = "https://www.linkedin.com/jobs/search?keywords="+ this.getField(this.ESTUDIOS)+"&trk=jobs_jserp_search_button_execute&locationId=";
else if ( this.getField(this.ESTUDIOS) == "0" && this.getField(this.LOCALIDAD) == "0")
System.out.println("No existe el usuario ni la localización");
Document doc;
try {
// need http protocol
doc = Jsoup.connect(url).get();
// get page title
String title = doc.title();
// System.out.println("title : " + title);
// get all links
Elements loginform = doc.getElementsByClass("job-title-link");
int i = 1;
for (Element link : loginform) {
// get the value from href attribute
if(!link.attr("href").equals("null"))
this.addField("oferta"+i,link.attr("href"));
else
this.addField("oferta"+i,0);
if(!link.text().equals("null"))
this.addField("enlace"+i,link.text());
else
this.addField("enlace"+i,0);
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
this.emit(); // Call emit() always at the end of this method.
}
@Override
public void usercleanup() {
// Close connections, clean resources
}
}
@pejema
Copy link

pejema commented Feb 18, 2016

Es mejor realizar la inicialización de parámetros en la función userprepare(), ya que mantienen el valor durante toda la ejecución de la topología. Las urls también se podrían inicializar en esta función para un mayor rendimiento.

public void userprepare() {
estudios = this.getParam("educacion");
localidad = this.getParam("localidad");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment