script/utils/search_twitter_json.py
author Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
Wed, 16 Mar 2016 23:09:57 +0100
changeset 1334 e1d3c1469691
child 1496 184372ec27e2
permissions -rw-r--r--
new command to search twitter using twitter front json api
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1334
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     1
import argparse
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     2
import logging
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     3
import math
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     4
import re
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     5
import time
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     6
import datetime
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     7
import urllib
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     8
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     9
from blessings import Terminal
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    10
import requests
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    11
import twitter
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    12
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    13
from iri_tweet import models, utils
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    14
from iri_tweet.processor import TwitterProcessorStatus
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    15
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    16
from lxml import html
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    17
import json
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    18
from pyquery import PyQuery
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    19
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    20
logger = logging.getLogger(__name__)
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    21
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    22
APPLICATION_NAME = "Tweet seach json"
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    23
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    24
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    25
# TODO: implement some more parameters
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    26
# script to "scrap twitter results"
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    27
# Shamelessly taken from https://github.com/Jefferson-Henrique/GetOldTweets-python
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    28
# pyquery cssselect
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    29
class TweetManager:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    30
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    31
    def __init__(self, query):
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    32
        self.query = query
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    33
        self.refresh_cursor = ''
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    34
        pass
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    35
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    36
    def __iter__(self):
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    37
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    38
        results = []
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    39
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    40
        while True:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    41
            json = self.get_json_response()
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    42
            if len(json['items_html'].strip()) == 0:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    43
                break
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    44
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    45
            self.refresh_cursor = json['min_position']
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    46
            tweets = PyQuery(json['items_html'])('div.js-stream-tweet')
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    47
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    48
            if len(tweets) == 0:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    49
                break
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    50
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    51
            for tweetHTML in tweets:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    52
                tweet_pq = PyQuery(tweetHTML)
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    53
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    54
                username = tweet_pq("span.username.js-action-profile-name b").text();
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    55
                txt = re.sub(r"\s+", " ", re.sub(r"[^\x00-\x7F]", "", tweet_pq("p.js-tweet-text").text()).replace('# ', '#').replace('@ ', '@'));
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    56
                retweets = int(tweet_pq("span.ProfileTweet-action--retweet span.ProfileTweet-actionCount").attr("data-tweet-stat-count").replace(",", ""));
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    57
                favorites = int(tweet_pq("span.ProfileTweet-action--favorite span.ProfileTweet-actionCount").attr("data-tweet-stat-count").replace(",", ""));
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    58
                date_sec = int(tweet_pq("small.time span.js-short-timestamp").attr("data-time"));
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    59
                id = tweet_pq.attr("data-tweet-id");
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    60
                permalink = tweet_pq.attr("data-permalink-path");
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    61
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    62
                geo = ''
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    63
                geo_span = tweet_pq('span.Tweet-geo')
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    64
                if len(geo_span) > 0:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    65
                    geo = geo_span.attr('title')
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    66
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    67
                yield {
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    68
                    "id" : id,
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    69
                    "permalink": 'https://twitter.com' + permalink,
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    70
                    "username" : username,
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    71
                    "text": txt,
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    72
                    "date" : datetime.datetime.fromtimestamp(date_sec),
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    73
                    "retweets" : retweets,
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    74
                    "favorites" : favorites,
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    75
                    "mentions": " ".join(re.compile('(@\\w*)').findall(txt)),
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    76
                    "hashtags": " ".join(re.compile('(#\\w*)').findall(txt)),
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    77
                    "geo": geo,
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    78
                }
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    79
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    80
    def get_json_response(self):
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    81
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    82
        url = "https://twitter.com/i/search/timeline"
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    83
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    84
        # if hasattr(tweetCriteria, 'username'):
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    85
        #     urlGetData += ' from:' + tweetCriteria.username
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    86
        #
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    87
        # if hasattr(tweetCriteria, 'since'):
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    88
        #     urlGetData += ' since:' + tweetCriteria.since
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    89
        #
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    90
        # if hasattr(tweetCriteria, 'until'):
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    91
        #     urlGetData += ' until:' + tweetCriteria.until
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    92
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    93
        params = {
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    94
            'f': 'realtime',
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    95
            'q': self.query,
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    96
            'src': 'typd',
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    97
            'max_position': self.refresh_cursor
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    98
        }
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    99
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   100
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'}
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   101
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   102
        return requests.get(url, params=params, headers=headers).json()
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   103
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   104
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   105
def get_options():
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   106
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   107
    usage = "usage: %(prog)s [options] <connection_str_or_filepath>"
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   108
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   109
    parser = argparse.ArgumentParser(usage=usage)
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   110
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   111
    parser.add_argument(dest="conn_str",
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   112
                        help="write tweet to DATABASE. This is a connection string", metavar="CONNECTION_STR")
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   113
    parser.add_argument("-Q", dest="query",
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   114
                      help="query", metavar="QUERY")
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   115
    parser.add_argument("-k", "--key", dest="consumer_key",
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   116
                        help="Twitter consumer key", metavar="CONSUMER_KEY")
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   117
    parser.add_argument("-s", "--secret", dest="consumer_secret",
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   118
                        help="Twitter consumer secret", metavar="CONSUMER_SECRET")
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   119
    parser.add_argument("-t", dest="token_filename", metavar="TOKEN_FILENAME", default=".oauth_token",
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   120
                      help="Token file name")
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   121
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   122
    utils.set_logging_options(parser)
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   123
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   124
    return parser.parse_args()
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   125
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   126
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   127
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   128
if __name__ == "__main__":
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   129
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   130
    options = get_options()
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   131
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   132
    utils.set_logging(options);
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   133
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   134
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   135
    acess_token_key, access_token_secret = utils.get_oauth_token(consumer_key=options.consumer_key, consumer_secret=options.consumer_secret, token_file_path=options.token_filename, application_name=APPLICATION_NAME)
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   136
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   137
    t = twitter.Twitter(domain="api.twitter.com", auth=twitter.OAuth(acess_token_key, access_token_secret, options.consumer_key, options.consumer_secret), secure=True)
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   138
    t.secure = True
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   139
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   140
    conn_str = options.conn_str.strip()
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   141
    if not re.match("^\w+://.+", conn_str):
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   142
        conn_str = 'sqlite:///' + conn_str
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   143
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   144
    engine, metadata, Session = models.setup_database(conn_str, echo=((options.verbose-options.quiet)>0), create_all=True)
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   145
    session = None
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   146
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   147
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   148
    term = Terminal()
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   149
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   150
    try:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   151
        session = Session()
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   152
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   153
        results = None
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   154
        print options.query
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   155
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   156
        tm = TweetManager(options.query)
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   157
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   158
        move_up = 0
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   159
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   160
        for i,item in enumerate(tm):
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   161
            # get id
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   162
            tweet_id = item.get("id")
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   163
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   164
            if not tweet_id:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   165
                continue
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   166
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   167
            if move_up > 0:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   168
                print((move_up+1)*term.move_up())
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   169
                move_up = 0
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   170
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   171
            print ("%d: %s - %r" % (i+1, tweet_id, item.get("text", "") ) + term.clear_eol())
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   172
            move_up += 1
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   173
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   174
            count_tweet = session.query(models.Tweet).filter_by(id_str=tweet_id).count()
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   175
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   176
            if count_tweet:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   177
                continue
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   178
            try:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   179
                tweet = t.statuses.show(id=tweet_id, include_entities=True)
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   180
            except twitter.api.TwitterHTTPError as e:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   181
                if e.e.code == 404 or e.e.code == 403:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   182
                    continue
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   183
                else:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   184
                    raise
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   185
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   186
            processor = TwitterProcessorStatus(tweet, None, None, session, None, options.token_filename, logger)
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   187
            processor.process()
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   188
            session.flush()
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   189
            session.commit()
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   190
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   191
            print("rate limit remaining %s of %s" % (str(tweet.rate_limit_remaining), str(tweet.headers.getheader('X-Rate-Limit-Limit'))) + term.clear_eol())
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   192
            move_up += 1
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   193
            rate_limit_limit = int(tweet.headers.getheader('X-Rate-Limit-Limit'))
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   194
            rate_limit_remaining = int(tweet.rate_limit_remaining)
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   195
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   196
            if rate_limit_remaining > rate_limit_limit:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   197
                time_to_sleep = 0
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   198
            else:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   199
                time_to_sleep = int(math.ceil((tweet.rate_limit_reset - time.mktime(time.gmtime())) / tweet.rate_limit_remaining))
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   200
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   201
            for i in xrange(time_to_sleep):
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   202
                if i:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   203
                    print(2*term.move_up())
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   204
                else:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   205
                    move_up += 1
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   206
                print(("Sleeping for %d seconds, %d remaining" % (time_to_sleep, time_to_sleep-i)) + term.clear_eol())
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   207
                time.sleep(1)
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   208
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   209
    except twitter.api.TwitterHTTPError as e:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   210
        fmt = ("." + e.format) if e.format else ""
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   211
        print "Twitter sent status %s for URL: %s%s using parameters: (%s)\ndetails: %s" % (repr(e.e.code), repr(e.uri), repr(fmt), repr(e.uriparts), repr(e.response_data))
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   212
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   213
    finally:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   214
        if session:
e1d3c1469691 new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   215
            session.close()