Created
December 2, 2015 14:19
-
-
Save evasuarez22/daa20841ff9657c0d847 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
/* | |
The MIT License (MIT) | |
Copyright (c) 2014 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.spouts; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.io.InputStream; | |
import java.io.UnsupportedEncodingException; | |
import java.math.BigInteger; | |
import java.security.InvalidKeyException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.SecureRandom; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.concurrent.LinkedBlockingQueue; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpHost; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.auth.AuthScope; | |
import org.apache.http.auth.UsernamePasswordCredentials; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.util.EntityUtils; | |
import org.apache.http.message.BasicNameValuePair; | |
import org.json.simple.JSONArray; | |
import org.json.simple.JSONObject; | |
import org.json.simple.parser.JSONParser; | |
import org.quartz.Job; | |
import org.quartz.JobBuilder; | |
import org.quartz.JobDetail; | |
import org.quartz.JobExecutionContext; | |
import org.quartz.JobExecutionException; | |
import org.quartz.Scheduler; | |
import org.quartz.SchedulerContext; | |
import org.quartz.SchedulerException; | |
import org.quartz.SimpleScheduleBuilder; | |
import org.quartz.Trigger; | |
import org.quartz.TriggerBuilder; | |
import org.quartz.impl.StdSchedulerFactory; | |
import backtype.storm.utils.Utils; | |
public class InfoJobsSpout extends BaseSinfonierSpout { | |
private String clientID; | |
private String apisecret; | |
private List<String> emitted = new ArrayList<>(); | |
private LinkedBlockingQueue<String> queue = null; | |
private int frequency; | |
private SecureRandom random = new SecureRandom(); | |
public InfoJobsSpout(String spoutName, String xmlPath) { | |
super(spoutName, xmlPath); | |
} | |
public void useropen(){ | |
this.clientID = (String)this.getParam("clientID"); | |
this.apisecret = (String)this.getParam("apisecret"); | |
this.frequency = Integer.parseInt((String)this.getParam("frequency")); | |
queue = new LinkedBlockingQueue<String>(1000); | |
JobDetail job = JobBuilder.newJob(ParseFeed.class).withIdentity("dummyJobName", "group1").build(); | |
Trigger trigger = TriggerBuilder | |
.newTrigger() | |
.withIdentity("Retrieve Items") | |
.withSchedule( | |
SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(this.frequency) | |
.repeatForever()).build(); | |
String schedulerName = new BigInteger(130, random).toString(32); | |
System.setProperty("org.quartz.scheduler.instanceName", schedulerName); | |
Scheduler scheduler; | |
try { | |
StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory(); | |
stdSchedulerFactory.initialize(); | |
scheduler = stdSchedulerFactory.getScheduler(); | |
scheduler.getContext().put("queue", queue); | |
scheduler.getContext().put("emitted", emitted); | |
//scheduler.getContext().put("URL", URL); | |
scheduler.getContext().put("clientID", clientID); | |
scheduler.getContext().put("apisecret", apisecret); | |
scheduler.getContext().put("frequency", frequency); | |
scheduler.start(); | |
scheduler.scheduleJob(job, trigger); | |
} catch (SchedulerException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void usernextTuple(){ | |
if (!queue.isEmpty()) { | |
String json = queue.poll(); | |
this.setJson(json); | |
this.emit(); | |
} else { | |
Utils.sleep(50); | |
} | |
} | |
public void userclose() { | |
} | |
public static class ParseFeed implements Job { | |
@Override | |
public void execute(JobExecutionContext context) throws JobExecutionException { | |
SchedulerContext schedulerContext = null; | |
//String URL; | |
String clientID; | |
String apisecret; | |
int frequency; | |
try { | |
schedulerContext = context.getScheduler().getContext(); | |
@SuppressWarnings("unchecked") | |
LinkedBlockingQueue<String> queue = (LinkedBlockingQueue<String>) schedulerContext.get("queue"); | |
clientID = (String) schedulerContext.get("clientID"); | |
apisecret = (String) schedulerContext.get("apisecret"); | |
frequency = (Integer) schedulerContext.get("frequency"); | |
List<String> emitted = (List<String>) schedulerContext.get("emitted"); | |
DefaultHttpClient client = new DefaultHttpClient(); | |
HttpHost targetHost = new HttpHost("api.infojobs.net", 443, "https"); | |
System.out.println("Me estoy autenticando"); | |
client.getCredentialsProvider().setCredentials( | |
new AuthScope(targetHost.getHostName(), targetHost.getPort()), | |
new UsernamePasswordCredentials(clientID, apisecret) | |
); | |
// create a GET method that queries some API operation | |
HttpGet request = new HttpGet("/api/1/offer"); | |
HttpEntity entity = null; | |
try { | |
HttpResponse response = client.execute(targetHost, request); | |
entity = response.getEntity(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
System.out.println("ERROR en get entity"); | |
e.printStackTrace(); | |
} | |
JSONArray jsonArray = null; | |
if (entity != null) { | |
InputStream instream = null; | |
try { | |
instream = entity.getContent(); | |
} catch (IllegalStateException | IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
try { | |
BufferedReader br = new BufferedReader(new InputStreamReader(instream)); | |
String output = br.readLine(); | |
System.out.println("imprimiendo reesultados"); | |
//System.out.println(output); | |
JSONObject jsonObject = (JSONObject) new JSONParser().parse(output); | |
jsonArray = (JSONArray)jsonObject.get("offers"); | |
//System.out.println(jsonArray) | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
System.out.println("Error obteniendo jsonArray"); | |
e.printStackTrace(); | |
} finally { | |
try { | |
instream.close(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} | |
for (int i = 0; i < jsonArray.size(); i++) { | |
if (emitted.isEmpty() || !emitted.contains(jsonArray.get(i).toString())) { | |
queue.put(jsonArray.get(i).toString()); | |
//System.out.println(jsonArray.get(i).toString()); | |
} | |
} | |
emitted.clear(); | |
for (int i = 0; i < jsonArray.size(); i++) { | |
emitted.add(jsonArray.get(i).toString()); | |
} | |
} catch (SchedulerException e1) { | |
e1.printStackTrace(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment