# Path: calfire_tracker/tweet_manager.py
# class TwitterHashtagSearch(object):
# 
#     def __init__(self):
#         date_object = datetime.datetime.now()
#         queryset = CalWildfire.objects.filter(
#             year=date_object.year).order_by("-date_time_started", "fire_name")
#         for item in queryset:
#             max_id = None
#             search_is_done = False
#             while (search_is_done == False):
#                 tweet_results = self.get_tweets(item.twitter_hashtag, max_id)
#                 self.build_tweet_from(item.twitter_hashtag, tweet_results)
#                 if max_id == None:
#                     search_is_done = True
#                 else:
#                     logger.debug("Retrieving more tweets since %s" % (max_id))
# 
#     def get_tweets(self, hashtag, max_id):
#         """
#         function to auth with twitter and return tweets
#         """
#         twitter_object = Twitter(
#             auth=OAuth(
#                 TWITTER_ACCESS_TOKEN,
#                 TWITTER_ACCESS_TOKEN_SECRET,
#                 TWITTER_CONSUMER_KEY,
#                 TWITTER_CONSUMER_SECRET
#             )
#         )
#         try:
#             tweet_results = twitter_object.search.tweets(
#                 q=hashtag,
#                 count=100,
#                 result_type="recent",
#                 include_entities=False,
#                 max_id=max_id,
#                 lang="en"
#             )
#             return tweet_results
#         except TwitterHTTPError, exception:
#             logger.error(exception.response_data["errors"])
#             if exception.response_data["errors"][0]["code"] == 88:
#                 return
# 
#     def get_max_id(self, results):
#         """
#         get the max_id of the next twitter search if present
#         """
#         # see if the metadata has a next_results key
#         # value is the idea to pull tweets from
#         more_tweets = results["search_metadata"].has_key("next_results")
#         # if there are more
#         if more_tweets == True:
#             # find the max id
#             parsed_string = results["search_metadata"][
#                 "next_results"].split("&")
#             parsed_string = parsed_string[0].split("?max_id=")
#             max_id = parsed_string[1]
#         # otherwise
#         else:
#             # max id is nothing
#             max_id = None
#         # return the max id
#         return max_id
# 
#     def build_tweet_from(self, hashtag, results):
#         """
#         function to auth with twitter and return tweets
#         """
#         for tweet in results["statuses"]:
#             if tweet["retweet_count"] == 0 or tweet["retweet_count"] == None:
#                 logger.debug("%s: %s" % (hashtag, len(results)))
#                 tweet_date = parser.parse(tweet["created_at"])
#                 tweet_date = tweet_date.replace(tzinfo=TWITTER_TIMEZONE)
#                 obj = TwitterHashtagResult(
#                     hashtag,
#                     tweet["text"].encode("ascii", "ignore"),
#                     tweet_date,
#                     tweet["id"],
#                     tweet["user"]["screen_name"].encode("ascii", "ignore"),
#                     tweet["user"]["profile_image_url"].encode(
#                         "ascii", "ignore"),
#                 )
#                 obj.save_tweet(obj)


from django.core.management.base import BaseCommand
from calfire_tracker.tweet_manager import TwitterHashtagSearch
import datetime


class Command(BaseCommand):
    help = "Pulls tweets via and associates with a given fire"

    def handle(self, *args, **options):

# Path: calfire_tracker/tweet_manager.py
# class TwitterHashtagSearch(object):
# 
#     def __init__(self):
#         date_object = datetime.datetime.now()
#         queryset = CalWildfire.objects.filter(
#             year=date_object.year).order_by("-date_time_started", "fire_name")
#         for item in queryset:
#             max_id = None
#             search_is_done = False
#             while (search_is_done == False):
#                 tweet_results = self.get_tweets(item.twitter_hashtag, max_id)
#                 self.build_tweet_from(item.twitter_hashtag, tweet_results)
#                 if max_id == None:
#                     search_is_done = True
#                 else:
#                     logger.debug("Retrieving more tweets since %s" % (max_id))
# 
#     def get_tweets(self, hashtag, max_id):
#         """
#         function to auth with twitter and return tweets
#         """
#         twitter_object = Twitter(
#             auth=OAuth(
#                 TWITTER_ACCESS_TOKEN,
#                 TWITTER_ACCESS_TOKEN_SECRET,
#                 TWITTER_CONSUMER_KEY,
#                 TWITTER_CONSUMER_SECRET
#             )
#         )
#         try:
#             tweet_results = twitter_object.search.tweets(
#                 q=hashtag,
#                 count=100,
#                 result_type="recent",
#                 include_entities=False,
#                 max_id=max_id,
#                 lang="en"
#             )
#             return tweet_results
#         except TwitterHTTPError, exception:
#             logger.error(exception.response_data["errors"])
#             if exception.response_data["errors"][0]["code"] == 88:
#                 return
# 
#     def get_max_id(self, results):
#         """
#         get the max_id of the next twitter search if present
#         """
#         # see if the metadata has a next_results key
#         # value is the idea to pull tweets from
#         more_tweets = results["search_metadata"].has_key("next_results")
#         # if there are more
#         if more_tweets == True:
#             # find the max id
#             parsed_string = results["search_metadata"][
#                 "next_results"].split("&")
#             parsed_string = parsed_string[0].split("?max_id=")
#             max_id = parsed_string[1]
#         # otherwise
#         else:
#             # max id is nothing
#             max_id = None
#         # return the max id
#         return max_id
# 
#     def build_tweet_from(self, hashtag, results):
#         """
#         function to auth with twitter and return tweets
#         """
#         for tweet in results["statuses"]:
#             if tweet["retweet_count"] == 0 or tweet["retweet_count"] == None:
#                 logger.debug("%s: %s" % (hashtag, len(results)))
#                 tweet_date = parser.parse(tweet["created_at"])
#                 tweet_date = tweet_date.replace(tzinfo=TWITTER_TIMEZONE)
#                 obj = TwitterHashtagResult(
#                     hashtag,
#                     tweet["text"].encode("ascii", "ignore"),
#                     tweet_date,
#                     tweet["id"],
#                     tweet["user"]["screen_name"].encode("ascii", "ignore"),
#                     tweet["user"]["profile_image_url"].encode(
#                         "ascii", "ignore"),
#                 )
#                 obj.save_tweet(obj)


# Path: calfire_tracker/management/commands/tweepy_to_db.py
from django.core.management.base import BaseCommand
from calfire_tracker.tweet_manager import TwitterHashtagSearch
import datetime



class Command(BaseCommand):
    help = "Pulls tweets via and associates with a given fire"

    def handle(self, *args, **options):

        task_run = TwitterHashtagSearch()