Created
April 1, 2017 23:00
-
-
Save Muratam/3ccb432529774b24e48f99fbd457ab70 to your computer and use it in GitHub Desktop.
twitter検索をして後ろ3件のツイートを確認するやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from twitter import Api | |
from requests_oauthlib import OAuth1Session | |
from urllib.parse import parse_qs | |
from pprint import pprint | |
import requests | |
import re | |
import sys | |
def search_many(api, search_str, limit): | |
max_id = None | |
founds = [] | |
for i in range(limit): | |
found = api.GetSearch(term=search_str, count=100, | |
result_type='recent', max_id=max_id) | |
if not found: | |
return founds | |
max_id = min([_.id for _ in found]) - 1 | |
founds += found | |
print(len(founds)) | |
return founds | |
def search_by_ids(api, name, tw_id): | |
founds = api.GetUserTimeline( | |
screen_name=name, since_id=tw_id - 1, count=100) | |
id_index = [_[0] for _ in enumerate(founds) if _[1].id == tw_id] | |
if not id_index: | |
return | |
id_index = id_index[0] | |
founds = [_[1] for _ in enumerate(founds) if abs(id_index - _[0]) < 3] | |
for found in founds: | |
print(found.user.screen_name + " : " + found.text.replace("\n", "")) | |
def main(args): | |
print("usage : python3 %s iikanji 1" % (args[0])) | |
with open("token") as f: | |
c_key, c_secret, a_key, a_secret = f.read().split("\n") | |
api = Api(c_key, c_secret, a_key, a_secret) | |
query = args[1] | |
limit = args[2] if len(args) > 3 else 10 | |
founds = search_many(api, query, limit) | |
for found in founds: | |
name, tw_id = found.user.screen_name, int(found.id) | |
search_by_ids(api, name, tw_id) | |
if __name__ == "__main__": | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment