From c843d7f3376de24d225157fe450730de639977bb Mon Sep 17 00:00:00 2001 From: agk512 Date: Sat, 23 Nov 2013 17:54:17 -0500 Subject: [PATCH] Twitter Alerts now feeding into AlertDB >AGK Twitter Alerts are now feeding into AlertDB properly, and are being displayed in both drop down notification and main page alert list. AGK --- .classpath | 4 +- src/com/uncc/gameday/alerts/AlertFetcher.java | 81 +++++++++++++++++-- src/com/uncc/gameday/alerts/AlertService.java | 2 +- 3 files changed, 77 insertions(+), 10 deletions(-) diff --git a/.classpath b/.classpath index 26bdfa6..b76ec6c 100644 --- a/.classpath +++ b/.classpath @@ -1,9 +1,9 @@ - - + + diff --git a/src/com/uncc/gameday/alerts/AlertFetcher.java b/src/com/uncc/gameday/alerts/AlertFetcher.java index 0ab3139..c863a72 100644 --- a/src/com/uncc/gameday/alerts/AlertFetcher.java +++ b/src/com/uncc/gameday/alerts/AlertFetcher.java @@ -1,5 +1,6 @@ package com.uncc.gameday.alerts; +import java.util.Date; import java.util.Iterator; import java.util.List; @@ -8,6 +9,9 @@ import twitter4j.Status; import twitter4j.Twitter; import twitter4j.TwitterException; import twitter4j.TwitterFactory; +import twitter4j.conf.ConfigurationBuilder; +import android.app.Activity; +import android.app.Application; import android.content.Context; import android.content.SharedPreferences; import android.util.Log; @@ -18,6 +22,8 @@ import com.uncc.gameday.R; /** * The Class AlertFetcher. */ + + public class AlertFetcher { // Class responsible for fetching all alerts as necessary. @@ -30,8 +36,7 @@ public class AlertFetcher { * @param ctx the ctx */ public void fetchAlerts(Context ctx) { - // Fetch all alerts. Responsible for discovering what sources need to be fetched. - + // Fetch all alerts. Responsible for discovering what sources need to be fetched. try { // Note we have to use the SharedPreferences so that we have preferences no matter what activity // sent us this context @@ -50,18 +55,30 @@ public class AlertFetcher { Log.w("AlertFetcher", "Unable to fetch alerts from Twitter...", e); } } - + /** * Fetch organization alerts. - * + * * @param ctx the ctx * @throws TwitterException the twitter exception */ private void fetchOrganizationAlerts(Context ctx) throws TwitterException { + + //creates configuration for twitter access + ConfigurationBuilder cb = new ConfigurationBuilder(); + + cb.setDebugEnabled(true) + .setOAuthConsumerKey("vfRa3Tr5QYaU8Jr2pKHtiA") + .setOAuthConsumerSecret("gGRdIrhPdX2Vrg296xOvTqE4sgOISMphRmPdrGirbU") + .setOAuthAccessToken("1912299896-uqrhDiif3oX9Ybkf8rM5pQDWN6mW4Y7vRLK47C7") + .setOAuthAccessTokenSecret("kZ11I74dcA00pWgQDZelFQz1ADJJMK0ejr6snnU34jUVT"); + + TwitterFactory tf = new TwitterFactory(cb.build()); + // Process fetching organization alerts (alerts retweeted by us) // Will not necessarily fetch `maxTweets` tweets. String handle = ctx.getString(R.string.gameday_handle); - Twitter twitter = TwitterFactory.getSingleton(); + Twitter twitter = tf.getInstance(); List statuses = twitter.getUserTimeline(handle, new Paging(1, maxTweets)); // Filter for anything created by us (retweet) @@ -71,6 +88,10 @@ public class AlertFetcher { it.remove(); } + String type = AlertType.getValue(AlertType.ORGANIZATION); + pushToDatabase(statuses, type, ctx); + + // List contains all valid alerts now } @@ -83,10 +104,26 @@ public class AlertFetcher { private void fetchUniversityAlerts(Context ctx) throws TwitterException { // Process fetching university alerts // Guaranteed to get `maxTweets` tweets + + //creates configuration for twitter access + ConfigurationBuilder cb = new ConfigurationBuilder(); + + cb.setDebugEnabled(true) + .setOAuthConsumerKey("vfRa3Tr5QYaU8Jr2pKHtiA") + .setOAuthConsumerSecret("gGRdIrhPdX2Vrg296xOvTqE4sgOISMphRmPdrGirbU") + .setOAuthAccessToken("1912299896-uqrhDiif3oX9Ybkf8rM5pQDWN6mW4Y7vRLK47C7") + .setOAuthAccessTokenSecret("kZ11I74dcA00pWgQDZelFQz1ADJJMK0ejr6snnU34jUVT"); + + TwitterFactory tf = new TwitterFactory(cb.build()); + String handle = ctx.getString(R.string.university_handle); - Twitter twitter = TwitterFactory.getSingleton(); + Twitter twitter = tf.getInstance(); List statuses = twitter.getUserTimeline(handle, new Paging(1, maxTweets)); + String type = AlertType.getValue(AlertType.UNIVERSITY); + pushToDatabase(statuses, type, ctx); + + System.out.println(statuses.get(0).getText()); // List contains all valid alerts now } @@ -99,8 +136,20 @@ public class AlertFetcher { private void fetchGamedayAlerts(Context ctx) throws TwitterException { // Process fetching alerts generated by staff of UNCCGameDay // Not guaranteed to get `maxTweets` tweets + + //creates configuration for twitter access + ConfigurationBuilder cb = new ConfigurationBuilder(); + + cb.setDebugEnabled(true) + .setOAuthConsumerKey("vfRa3Tr5QYaU8Jr2pKHtiA") + .setOAuthConsumerSecret("gGRdIrhPdX2Vrg296xOvTqE4sgOISMphRmPdrGirbU") + .setOAuthAccessToken("1912299896-uqrhDiif3oX9Ybkf8rM5pQDWN6mW4Y7vRLK47C7") + .setOAuthAccessTokenSecret("kZ11I74dcA00pWgQDZelFQz1ADJJMK0ejr6snnU34jUVT"); + + TwitterFactory tf = new TwitterFactory(cb.build()); + String handle = ctx.getString(R.string.gameday_handle); - Twitter twitter = TwitterFactory.getSingleton(); + Twitter twitter = tf.getInstance(); List statuses = twitter.getUserTimeline(handle, new Paging(1, maxTweets)); // Filter out anything not from us @@ -110,7 +159,25 @@ public class AlertFetcher { it.remove(); } + String type = AlertType.getValue(AlertType.GAMEDAY); + pushToDatabase(statuses, type, ctx); + // List contains all valid alerts now. } + + //takes list of statuses + //converts it to Alert object type + //and persists to database + public void pushToDatabase(List statuses, String type, Context ctx) + { + AlertDB db = new AlertDB(ctx); + Date todayDate = new Date(); + todayDate.getTime(); + for(int i = 0; i < statuses.size(); i++) + { + Alert temp = new Alert(todayDate, statuses.get(i).getText(), 0, type); + db.persist(temp); + } + } } \ No newline at end of file diff --git a/src/com/uncc/gameday/alerts/AlertService.java b/src/com/uncc/gameday/alerts/AlertService.java index 6a66272..7fa3024 100644 --- a/src/com/uncc/gameday/alerts/AlertService.java +++ b/src/com/uncc/gameday/alerts/AlertService.java @@ -18,7 +18,7 @@ public class AlertService extends IntentService { @Override protected void onHandleIntent(Intent intent) { // Go fetch all the alerts! - //new AlertFetcher().fetchAlerts(this); + new AlertFetcher().fetchAlerts(this); //if first application run, create and store //timed alerts into database