| author | ymh <ymh.work@gmail.com> |
| Fri, 15 Nov 2024 01:29:53 +0100 | |
| changeset 1575 | ce1d5b0d1479 |
| parent 1497 | 14a9bed2e3cd |
| permissions | -rw-r--r-- |
|
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 |
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
|
17 |
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
|
18 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
19 |
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
|
20 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
21 |
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
|
22 |
|
|
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 |
# 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
|
25 |
# 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
|
26 |
# 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
|
27 |
# 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
|
28 |
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
|
29 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
30 |
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
|
31 |
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
|
32 |
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
|
33 |
pass |
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
34 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
35 |
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
|
36 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
37 |
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
|
38 |
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
|
39 |
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
|
40 |
break |
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
41 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
42 |
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
|
43 |
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
|
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 |
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
|
46 |
break |
|
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 |
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
|
49 |
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
|
50 |
|
|
1496
184372ec27e2
upgrade to python 3 and twitter api
ymh <ymh.work@gmail.com>
parents:
1334
diff
changeset
|
51 |
username = tweet_pq("span.username.js-action-profile-name b").text() |
|
184372ec27e2
upgrade to python 3 and twitter api
ymh <ymh.work@gmail.com>
parents:
1334
diff
changeset
|
52 |
txt = re.sub(r"\s+", " ", re.sub(r"[^\x00-\x7F]", "", tweet_pq("p.js-tweet-text").text()).replace('# ', '#').replace('@ ', '@')) |
|
184372ec27e2
upgrade to python 3 and twitter api
ymh <ymh.work@gmail.com>
parents:
1334
diff
changeset
|
53 |
retweets = int(tweet_pq("span.ProfileTweet-action--retweet span.ProfileTweet-actionCount").attr("data-tweet-stat-count").replace(",", "")) |
|
184372ec27e2
upgrade to python 3 and twitter api
ymh <ymh.work@gmail.com>
parents:
1334
diff
changeset
|
54 |
favorites = int(tweet_pq("span.ProfileTweet-action--favorite span.ProfileTweet-actionCount").attr("data-tweet-stat-count").replace(",", "")) |
|
184372ec27e2
upgrade to python 3 and twitter api
ymh <ymh.work@gmail.com>
parents:
1334
diff
changeset
|
55 |
date_sec = int(tweet_pq("small.time span.js-short-timestamp").attr("data-time")) |
|
184372ec27e2
upgrade to python 3 and twitter api
ymh <ymh.work@gmail.com>
parents:
1334
diff
changeset
|
56 |
id = tweet_pq.attr("data-tweet-id") |
|
184372ec27e2
upgrade to python 3 and twitter api
ymh <ymh.work@gmail.com>
parents:
1334
diff
changeset
|
57 |
permalink = tweet_pq.attr("data-permalink-path") |
|
1334
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
58 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
59 |
geo = '' |
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
60 |
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
|
61 |
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
|
62 |
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
|
63 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
64 |
yield { |
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
65 |
"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
|
66 |
"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
|
67 |
"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
|
68 |
"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
|
69 |
"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
|
70 |
"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
|
71 |
"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
|
72 |
"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
|
73 |
"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
|
74 |
"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
|
75 |
} |
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
76 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
77 |
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
|
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 |
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
|
80 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
81 |
# 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
|
82 |
# 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
|
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, 'since'): |
|
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 += ' 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
|
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, 'until'): |
|
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 += ' 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
|
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 |
params = { |
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
91 |
'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
|
92 |
'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
|
93 |
'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
|
94 |
'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
|
95 |
} |
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
96 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
97 |
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
|
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 |
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
|
100 |
|
|
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 |
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
|
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 |
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
|
105 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
106 |
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
|
107 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
108 |
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
|
109 |
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
|
110 |
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
|
111 |
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
|
112 |
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
|
113 |
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
|
114 |
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
|
115 |
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
|
116 |
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
|
117 |
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
|
118 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
119 |
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
|
120 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
121 |
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
|
122 |
|
|
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 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
125 |
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
|
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 |
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
|
128 |
|
|
1496
184372ec27e2
upgrade to python 3 and twitter api
ymh <ymh.work@gmail.com>
parents:
1334
diff
changeset
|
129 |
utils.set_logging(options) |
|
1334
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
130 |
|
|
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 |
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) |
|
1497
14a9bed2e3cd
Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents:
1496
diff
changeset
|
133 |
twitter_auth = twitter.OAuth(acess_token_key, access_token_secret, options.consumer_key, options.consumer_secret) |
|
1334
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
134 |
|
|
1497
14a9bed2e3cd
Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents:
1496
diff
changeset
|
135 |
t = twitter.Twitter(domain="api.twitter.com", auth=twitter_auth, secure=True) |
|
1334
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
136 |
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
|
137 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
138 |
conn_str = options.conn_str.strip() |
|
1496
184372ec27e2
upgrade to python 3 and twitter api
ymh <ymh.work@gmail.com>
parents:
1334
diff
changeset
|
139 |
if not re.match(r"^\w+://.+", conn_str): |
|
1334
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 = '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
|
141 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
142 |
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
|
143 |
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
|
144 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
145 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
146 |
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
|
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 |
try: |
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
149 |
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
|
150 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
151 |
results = None |
|
1496
184372ec27e2
upgrade to python 3 and twitter api
ymh <ymh.work@gmail.com>
parents:
1334
diff
changeset
|
152 |
print(options.query) |
|
1334
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
153 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
154 |
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
|
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 |
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
|
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 |
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
|
159 |
# 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
|
160 |
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
|
161 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
162 |
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
|
163 |
continue |
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
164 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
165 |
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
|
166 |
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
|
167 |
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 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
169 |
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
|
170 |
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
|
171 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
172 |
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
|
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 |
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
|
175 |
continue |
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
176 |
try: |
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
177 |
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
|
178 |
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
|
179 |
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
|
180 |
continue |
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
181 |
else: |
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
182 |
raise |
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
183 |
|
|
1497
14a9bed2e3cd
Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents:
1496
diff
changeset
|
184 |
processor = TwitterProcessorStatus(tweet, None, None, session, twitter_auth=twitter_auth, logger=logger) |
|
1334
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
185 |
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
|
186 |
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
|
187 |
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
|
188 |
|
|
1496
184372ec27e2
upgrade to python 3 and twitter api
ymh <ymh.work@gmail.com>
parents:
1334
diff
changeset
|
189 |
print("rate limit remaining %s of %s" % (str(tweet.rate_limit_remaining), str(tweet.headers['X-Rate-Limit-Limit'])) + term.clear_eol()) |
|
1334
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
190 |
move_up += 1 |
|
1496
184372ec27e2
upgrade to python 3 and twitter api
ymh <ymh.work@gmail.com>
parents:
1334
diff
changeset
|
191 |
rate_limit_limit = int(tweet.headers['X-Rate-Limit-Limit']) |
|
1334
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
192 |
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
|
193 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
194 |
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
|
195 |
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
|
196 |
else: |
|
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 = 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
|
198 |
|
|
1496
184372ec27e2
upgrade to python 3 and twitter api
ymh <ymh.work@gmail.com>
parents:
1334
diff
changeset
|
199 |
for i in range(time_to_sleep): |
|
1334
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
200 |
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
|
201 |
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
|
202 |
else: |
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
203 |
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
|
204 |
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
|
205 |
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
|
206 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
207 |
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
|
208 |
fmt = ("." + e.format) if e.format else "" |
|
1496
184372ec27e2
upgrade to python 3 and twitter api
ymh <ymh.work@gmail.com>
parents:
1334
diff
changeset
|
209 |
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))) |
|
1334
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
210 |
|
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
211 |
finally: |
|
e1d3c1469691
new command to search twitter using twitter front json api
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff
changeset
|
212 |
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
|
213 |
session.close() |