-
-
Save ffr4nz/74876ab91474aa411064 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.InputStream; | |
import java.io.BufferedReader; | |
import java.io.DataOutputStream; | |
import java.io.InputStreamReader; | |
import java.io.UnsupportedEncodingException; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.net.CookieHandler; | |
import java.net.CookieManager; | |
import java.net.HttpURLConnection; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.UUID; | |
import java.util.concurrent.LinkedBlockingQueue; | |
import java.util.zip.GZIPInputStream; | |
import javax.net.ssl.HttpsURLConnection; | |
import javax.net.ssl.*; | |
import org.json.JSONArray; | |
import org.json.JSONObject; | |
import org.json.XML; | |
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; | |
import com.fasterxml.jackson.dataformat.xml.XmlMapper; | |
import java.security.SecureRandom; | |
import java.math.BigInteger; | |
public class GoogleAlert extends BaseSinfonierSpout { | |
private String entity = "feedItem"; | |
private URL url; | |
private LinkedBlockingQueue<String> queue = null; | |
private List<String> emitted = new ArrayList<>(); | |
private int frequency = 300; | |
private SecureRandom random = new SecureRandom(); | |
public GoogleAlert(String spoutName, String xmlPath) { | |
super(spoutName, xmlPath); | |
} | |
public void useropen(){ | |
try { | |
url = new URL((String)this.getParam("url")); | |
frequency = Integer.parseInt(getParam("frequency",true)); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
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(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.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 { | |
private JSONArray jsonLastArray = null; | |
@Override | |
public void execute(JobExecutionContext context) throws JobExecutionException { | |
SchedulerContext schedulerContext = null; | |
URL url; | |
HttpURLConnection conn; | |
List<String> cookies = null; | |
try { | |
// Create a trust manager that does not validate certificate chains | |
TrustManager[] trustAllCerts = new TrustManager[] { | |
new X509TrustManager() { | |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { | |
return new java.security.cert.X509Certificate[0]; | |
} | |
public void checkClientTrusted( | |
java.security.cert.X509Certificate[] certs, String authType) { | |
} | |
public void checkServerTrusted( | |
java.security.cert.X509Certificate[] certs, String authType) { | |
} | |
} | |
}; | |
// Install the all-trusting trust manager | |
try { | |
SSLContext sc = SSLContext.getInstance("SSL"); | |
sc.init(null, trustAllCerts, new java.security.SecureRandom()); | |
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); | |
} catch (Exception e) { | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
try { | |
schedulerContext = context.getScheduler().getContext(); | |
@SuppressWarnings("unchecked") | |
LinkedBlockingQueue<String> queue = (LinkedBlockingQueue<String>) schedulerContext | |
.get("queue"); | |
url = (URL) schedulerContext.get("url"); | |
List<String> emitted = (List<String>) schedulerContext.get("emitted"); | |
// Create cookie handler | |
CookieHandler.setDefault(new CookieManager()); | |
// Perform a request to get cookies | |
conn = (HttpURLConnection) url.openConnection(); | |
conn.setRequestMethod("GET"); | |
conn.setUseCaches(false); | |
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:36.0) Gecko/20100101 Firefox/36.0"); | |
conn.setRequestProperty("Accept", | |
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); | |
conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); | |
if (cookies != null) { | |
for (String cookie : cookies) { | |
conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]); | |
} | |
} | |
int responseCode = conn.getResponseCode(); | |
System.out.println("\nSending 'GET' request to URL : " + url); | |
System.out.println("Response Code : " + responseCode); | |
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); | |
String inputLine; | |
StringBuffer response = new StringBuffer(); | |
while ((inputLine = in.readLine()) != null) { | |
response.append(inputLine); | |
} | |
in.close(); | |
// Store cookies | |
cookies = conn.getHeaderFields().get("Set-Cookie"); | |
//////---////// | |
URLConnection openConnection = url.openConnection(); | |
openConnection.addRequestProperty("User-Agent", "Feedfetcher-Google; (+http://www.google.com/feedfetcher.html; feed-id=8639390370582375869)"); | |
openConnection.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); | |
openConnection.addRequestProperty("Accept-Encoding", "gzip, deflate"); | |
openConnection.addRequestProperty("Accept-Language", "en-US,en;q=0.5"); | |
openConnection.addRequestProperty("DNT", "1"); | |
if (cookies != null) { | |
for (String cookie : cookies) { | |
openConnection.addRequestProperty("Cookie", cookie.split(";", 1)[0]); | |
} | |
} | |
InputStream is = openConnection.getInputStream(); | |
if("gzip".equals(openConnection.getContentEncoding())){ | |
is = new GZIPInputStream(is); | |
} | |
System.out.println(is.toString()); | |
int ptr = 0; | |
StringBuilder builder = new StringBuilder(); | |
while ((ptr = is.read()) != -1) { | |
builder.append((char) ptr); | |
} | |
String xml = builder.toString(); | |
JSONObject jsonObject = XML.toJSONObject(xml).getJSONObject("feed"); | |
JSONArray jsonArray = jsonObject.getJSONArray("entry"); | |
for (int i = 0; i < jsonArray.length(); i++) { | |
if (emitted.isEmpty() | |
|| !emitted.contains(jsonArray.getJSONObject(i).toString())) { | |
queue.put(jsonArray.getJSONObject(i).toString()); | |
} | |
} | |
emitted.clear(); | |
for (int i = 0; i < jsonArray.length(); i++) { | |
emitted.add(jsonArray.getJSONObject(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