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

Add ability to download twitter bookmarks #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 10 additions & 1 deletion scraper/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ def main():
help="Twitter hashtag. Scrape tweets from a hashtag.",
)

parser.add_argument(
"--bookmarks",
action='store_true',
help="Twitter bookmarks. Scrape tweets from your bookmarks.",
)

parser.add_argument(
"-ntl",
"--no_tweets_limit",
Expand Down Expand Up @@ -130,11 +136,13 @@ def main():
tweet_type_args.append(args.hashtag)
if args.query is not None:
tweet_type_args.append(args.query)
if args.bookmarks is not False:
tweet_type_args.append(args.query)

additional_data: list = args.add.split(",")

if len(tweet_type_args) > 1:
print("Please specify only one of --username, --hashtag, or --query.")
print("Please specify only one of --username, --hashtag, --bookmarks, or --query.")
sys.exit(1)

if args.latest and args.top:
Expand All @@ -153,6 +161,7 @@ def main():
no_tweets_limit= args.no_tweets_limit if args.no_tweets_limit is not None else True,
scrape_username=args.username,
scrape_hashtag=args.hashtag,
scrape_bookmarks=args.bookmarks,
scrape_query=args.query,
scrape_latest=args.latest,
scrape_top=args.top,
Expand Down
27 changes: 27 additions & 0 deletions scraper/twitter_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(
scrape_username=None,
scrape_hashtag=None,
scrape_query=None,
scrape_bookmarks=False,
scrape_poster_details=False,
scrape_latest=True,
scrape_top=False,
Expand All @@ -58,6 +59,7 @@ def __init__(
"type": None,
"username": None,
"hashtag": None,
"bookmarks": False,
"query": None,
"tab": None,
"poster_details": False,
Expand All @@ -72,6 +74,7 @@ def __init__(
max_tweets,
scrape_username,
scrape_hashtag,
scrape_bookmarks,
scrape_query,
scrape_latest,
scrape_top,
Expand All @@ -83,6 +86,7 @@ def _config_scraper(
max_tweets=50,
scrape_username=None,
scrape_hashtag=None,
scrape_bookmarks=False,
scrape_query=None,
scrape_latest=True,
scrape_top=False,
Expand All @@ -99,6 +103,7 @@ def _config_scraper(
"hashtag": str(scrape_hashtag).replace("#", "")
if scrape_hashtag is not None
else None,
"bookmarks": scrape_bookmarks,
"query": scrape_query,
"tab": "Latest" if scrape_latest else "Top" if scrape_top else "Latest",
"poster_details": scrape_poster_details,
Expand All @@ -112,6 +117,9 @@ def _config_scraper(
elif scrape_hashtag is not None:
self.scraper_details["type"] = "Hashtag"
self.router = self.go_to_hashtag
elif scrape_bookmarks is not False:
self.scraper_details["type"] = "Bookmarks"
self.router = self.go_to_bookmarks
elif scrape_query is not None:
self.scraper_details["type"] = "Query"
self.router = self.go_to_search
Expand Down Expand Up @@ -339,6 +347,20 @@ def go_to_hashtag(self):
sleep(3)
pass

def go_to_bookmarks(self):
if (
self.scraper_details["bookmarks"] is False
or self.scraper_details["bookmarks"] == ""
):
print("Bookmarks is not set.")
sys.exit(1)
else:
url = f"https://twitter..com/i/bookmarks"

self.driver.get(url)
sleep(3)
pass

def go_to_search(self):
if self.scraper_details["query"] is None or self.scraper_details["query"] == "":
print("Query is not set.")
Expand Down Expand Up @@ -378,6 +400,7 @@ def scrape_tweets(
no_tweets_limit=False,
scrape_username=None,
scrape_hashtag=None,
scrape_bookmarks=False,
scrape_query=None,
scrape_latest=True,
scrape_top=False,
Expand All @@ -388,6 +411,7 @@ def scrape_tweets(
max_tweets,
scrape_username,
scrape_hashtag,
scrape_bookmarks,
scrape_query,
scrape_latest,
scrape_top,
Expand All @@ -409,6 +433,9 @@ def scrape_tweets(
self.scraper_details["tab"], self.scraper_details["hashtag"]
)
)
elif self.scraper_details["type"] == "Bookmarks":
print(
"Scraping Tweets from bookmarks...".format(self.scraper_details["username"]))
elif self.scraper_details["type"] == "Query":
print(
"Scraping {} Tweets from {} search...".format(
Expand Down