Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Progress Bars #490

Merged
merged 52 commits into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
95e5106
add tqdm
igorbrigadir Jun 19, 2021
a33408a
test tqdm
igorbrigadir Jun 19, 2021
00797ef
tqdm test
igorbrigadir Jun 20, 2021
423ab5a
stash changes
igorbrigadir Jun 21, 2021
73a32a1
merge main
igorbrigadir Jun 21, 2021
fa42b74
set default progress bar properties
igorbrigadir Jun 21, 2021
32ce735
addhide progress bar option
igorbrigadir Jun 21, 2021
af6c40c
improve progress bar close behaviour
igorbrigadir Jun 21, 2021
aa89d07
fix following command
igorbrigadir Jun 21, 2021
3694b83
Merge branch 'main' into progress-bars
igorbrigadir Jun 21, 2021
7a3350d
use simple bar format for now
igorbrigadir Jun 22, 2021
30098ac
file based progress for flatten command
igorbrigadir Jun 22, 2021
89047be
add stubs and make FileSizeProgressBar aware of errors
igorbrigadir Jun 23, 2021
02b9260
Merge branch 'main' into progress-bars
igorbrigadir Jun 27, 2021
c17d16c
allow friends and followers commands to skip a user lookup if id is k…
igorbrigadir Jun 27, 2021
23dddbb
add _ensure_user convenience function
igorbrigadir Jun 28, 2021
f8935b8
friends and followers docs string for limit note on min. 1000 results
igorbrigadir Jun 28, 2021
1a2c2eb
friends and followers progress bars
igorbrigadir Jun 28, 2021
920e79c
black format
igorbrigadir Jun 28, 2021
98a1d3e
help strings
igorbrigadir Jun 28, 2021
e70f5fb
use counts endpoint for search progress bar
igorbrigadir Jun 28, 2021
be9afa4
change default timestamp progress bar start time
igorbrigadir Jun 28, 2021
bc4c2ab
mentions progressbar
igorbrigadir Jun 28, 2021
e76592a
handle errors in file size progress bar
igorbrigadir Jun 28, 2021
293bafc
rework search & conversation to use the same method
igorbrigadir Jun 28, 2021
c3d6526
timeline progressbars
igorbrigadir Jun 28, 2021
4654725
merge main
igorbrigadir Jul 2, 2021
54e71c2
move os import
igorbrigadir Jul 2, 2021
8e6302e
Merge branch 'main' into progress-bars
igorbrigadir Jul 2, 2021
40aca41
fix exception handling when counts fail
igorbrigadir Jul 2, 2021
568d300
move default start time to command2.py
igorbrigadir Jul 2, 2021
e2d4156
fix non utx dates for time calculations and imporove search progress bar
igorbrigadir Jul 2, 2021
8ff8b6e
formatting
igorbrigadir Jul 2, 2021
1317f90
better earrly stop behaviour for timestamp progress bar
igorbrigadir Jul 2, 2021
3fcaefc
Merge branch 'main' into progress-bars
igorbrigadir Jul 2, 2021
f144bb2
add fancy progress bar output with humanize library
igorbrigadir Jul 2, 2021
37f0242
fix ensure_user_id after refactor
igorbrigadir Jul 3, 2021
ccdb1ea
merge latest main
igorbrigadir Jul 3, 2021
ff2f290
black format
igorbrigadir Jul 3, 2021
9faa6d1
merge main
igorbrigadir Jul 3, 2021
628b55a
fix black format mistake
igorbrigadir Jul 3, 2021
7cc16ad
pbar for search early stopping and timelines and id_exists fix
igorbrigadir Jul 3, 2021
79f98b0
timeline_tweets progress bar based on user counts
igorbrigadir Jul 3, 2021
ea576e7
fix _timeline_tweets search not working when start_time was not set
igorbrigadir Jul 3, 2021
a31b162
black format
igorbrigadir Jul 3, 2021
3059283
remove unused code
igorbrigadir Jul 3, 2021
e264195
remove unused imports
igorbrigadir Jul 3, 2021
84260b1
fix twarc2 timeline progress bar
igorbrigadir Jul 3, 2021
78c1034
fix filesize progress bar - turn off for pipes
igorbrigadir Jul 3, 2021
2949aee
better mentions progress bar help messages
igorbrigadir Jul 3, 2021
2d39b45
better filesize progress message
igorbrigadir Jul 3, 2021
58198c8
following and follower progress tweak
igorbrigadir Jul 3, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
python-dateutil
requests_oauthlib
tqdm
humanize
click
click-plugins
click-config-file
64 changes: 37 additions & 27 deletions twarc/client2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""

import re
import ssl
import json
import time
import logging
Expand All @@ -23,8 +22,6 @@

log = logging.getLogger("twarc")

TWITTER_EPOCH = datetime.datetime(2006, 3, 21, tzinfo=datetime.timezone.utc)


class Twarc2:
"""
Expand Down Expand Up @@ -227,12 +224,6 @@ def search_all(
"""
url = "https://api.twitter.com/2/tweets/search/all"

# start time defaults to the beginning of Twitter to override the
# default of the last month. Only do this if start_time is not already
# specified and since_id isn't being used
if start_time is None and since_id is None:
start_time = TWITTER_EPOCH

return self._search(
url,
query,
Expand Down Expand Up @@ -319,12 +310,6 @@ def counts_all(
"""
url = "https://api.twitter.com/2/tweets/counts/all"

# start time defaults to the beginning of Twitter to override the
# default of the last month. Only do this if start_time is not already
# specified and since_id isn't being used
if start_time is None and since_id is None:
start_time = TWITTER_EPOCH

return self._search(
url,
query,
Expand Down Expand Up @@ -713,7 +698,7 @@ def mentions(
exclude_replies,
)

def following(self, user):
def following(self, user, user_id=None):
"""
Retrieve the user profiles of accounts followed by the given user.

Expand All @@ -725,13 +710,13 @@ def following(self, user):
Returns:
generator[dict]: A generator, dict for each page of results.
"""
user_id = self._ensure_user_id(user)
user_id = self._ensure_user_id(user) if not user_id else user_id
params = expansions.USER_EVERYTHING.copy()
params["max_results"] = 1000
url = f"https://api.twitter.com/2/users/{user_id}/following"
return self.get_paginated(url, params=params)

def followers(self, user):
def followers(self, user, user_id=None):
"""
Retrieve the user profiles of accounts following the given user.

Expand All @@ -743,7 +728,7 @@ def followers(self, user):
Returns:
generator[dict]: A generator, dict for each page of results.
"""
user_id = self._ensure_user_id(user)
user_id = self._ensure_user_id(user) if not user_id else user_id
params = expansions.USER_EVERYTHING.copy()
params["max_results"] = 1000
url = f"https://api.twitter.com/2/users/{user_id}/followers"
Expand Down Expand Up @@ -875,18 +860,24 @@ def connect(self):
resource_owner_secret=self.access_token_secret,
)

def _id_exists(self, user):
"""
Returns True if the user id exists
"""
try:
error_name = next(self.user_lookup([user]))["errors"][0]["title"]
return error_name != "Not Found Error"
except KeyError:
return True

def _ensure_user_id(self, user):
"""
Always return a valid user id, look up if not numeric.
"""
user = str(user)
is_numeric = re.match(r"^\d+$", user)

def id_exists(user):
try:
error_name = next(self.user_lookup([user]))["errors"][0]["title"]
return error_name != "Not Found Error"
except KeyError:
return True

if len(user) > 15 or (is_numeric and id_exists(user)):
if len(user) > 15 or (is_numeric and self._id_exists(user)):
return user
else:
results = next(self.user_lookup([user], usernames=True))
Expand All @@ -897,6 +888,25 @@ def id_exists(user):
else:
raise ValueError(f"No such user {user}")

def _ensure_user(self, user):
"""
Always return a valid user object.
"""
user = str(user)
is_numeric = re.match(r"^\d+$", user)

lookup = []
if len(user) > 15 or (is_numeric and self._id_exists(user)):
lookup = expansions.ensure_flattened(list(self.user_lookup([user])))
else:
lookup = expansions.ensure_flattened(
list(self.user_lookup([user], usernames=True))
)
if lookup:
return lookup[-1]
else:
raise ValueError(f"No such user {user}")

def _check_for_disconnect(self, data):
"""
Look for disconnect errors in a response, and reconnect if found. The
Expand Down
Loading