package fr.ece.tweetstats.twitterapi;

import java.util.Date;
import java.util.List;

import fr.ece.tweetstats.core.domain.Fetch;
import fr.ece.tweetstats.core.domain.Tweet;

import twitter4j.*;
import twitter4j.auth.AccessToken;

public class TwitterAPI {

	public static final String Consumer_key = "g43gQM0aonUSCILzb84xQ";
	public static final String Consumer_secret = "dpeXTuKau8CpmcDzC1J1FL90VbGuV75GVuHWOBsuAY";
	public static final String Access_token = "2242385018-Edtcq4Ej5acALsCfehGkBHPO6naNR0dDSr0Kvpe";
	public static final String Access_token_secret = "9Ayq225X0yTnupFAznOrgZR8svVve6cdWjQ93EcHghxtX";

	public static Fetch getByBrandAndAdjective(String brand, String adj) {

		Fetch fetch = new Fetch();

		try {

			fetch.setAdjective(adj);
			fetch.setDate(new Date());
			fetch.setCreatedDate(new Date());
			fetch.setBrand(brand);
			Twitter twitter = getTwitter();

			Query query = new Query(fetch.getBrand() + " " + fetch.getAdjective());
			QueryResult result;
			do {

				result = twitter.search(query);
				List<Status> tweets = result.getTweets();

				for (Status tweet : tweets) {
					Tweet tw = new Tweet(tweet.getId(), tweet.getCreatedAt(),tweet.getText());
					fetch.addResult(tw);
				}
			} while ((query = result.nextQuery()) != null);

		} catch (TwitterException e) {
			e.printStackTrace();
		}
		return fetch;
	}

	private static Twitter getTwitter() {
		Twitter twitter = new TwitterFactory().getInstance();
		twitter.setOAuthConsumer(Consumer_key, Consumer_secret);
		twitter.setOAuthAccessToken(loadAccessToken());
		return twitter;
	}

	private static AccessToken loadAccessToken() {
		String token = Access_token;
		String tokenSecret = Access_token_secret;
		return new AccessToken(token, tokenSecret);
	}
}

@@ -32,16 +32,44 @@
 erapi;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -83,16 +83,16 @@
 l.Date;%0A
-
 import j
@@ -195,17 +195,16 @@
 .Tweet;%0A
-%0A
 import t
@@ -953,17 +953,22 @@
 () + %22 %22
- 
+%0A%09%09%09%09%09
 + fetch.
@@ -1195,16 +1195,24 @@
 tedAt(),
+%0A%09%09%09%09%09%09%09
 tweet.ge
@@ -1366,16 +1366,16 @@
 ();%0A%09%09%7D%0A
-
 %09%09return
@@ -1382,24 +1382,300 @@
  fetch;%0A%09%7D%0A%0A
+%09public static List%3CFetch%3E getByBrandAndAdjectives(String brand, List%3CString%3E adjs) %7B%0A%09%09List%3CFetch%3E fetchs = new ArrayList%3CFetch%3E();%0A%0A%09%09for (int i = 0; i %3C adjs.size(); i++) %7B%0A%09%09%09Fetch f = getByBrandAndAdjective(brand, adjs.get(i));%0A%09%09%09fetchs.add(f);%0A%09%09%7D%0A%09%09return fetchs;%0A%09%7D%0A%0A
 %09private sta

add getbyadjectives