Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #8 from nnadeau/feature/dump-users
Browse files Browse the repository at this point in the history
  • Loading branch information
nnadeau authored Jan 22, 2022
2 parents 92d3b02 + 68d1df3 commit 49f8e71
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,4 @@ cython_debug/
out/
logs/
*.log
*.json
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ disable = "C0330, C0326, W1203"

[tool.pylint.format]
max-line-length = "88"
good-names="i,j,k,e"
good-names="i,j,k,e,f"

[tool.pylint.master]
init-hook = 'import sys; import os; sys.path.append(os.getcwd() + "/src");'
Expand Down
76 changes: 76 additions & 0 deletions src/twitter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Twitter handler."""
import datetime
import json
from pathlib import Path
from typing import Optional

import fire
import tweepy
Expand Down Expand Up @@ -32,6 +35,79 @@ def _authenticate():
return api


def _fetch_people_from_user(api: tweepy.API, people_type: str, screen_name: str):
LOGGER.info(f"Fetching {people_type.lower()} from {screen_name}")
if people_type.lower() == "friends":
cursor = tweepy.Cursor(
method=api.get_friends, count=200, screen_name=screen_name
).items()
elif people_type.lower() == "followers":
cursor = tweepy.Cursor(
method=api.get_followers, count=200, screen_name=screen_name
).items()
else:
raise ValueError("People must be FRIENDS or FOLLOWERS")

# pylint: disable=protected-access
people = [person._json for person in cursor]
return people


def dump_users(
people_type: Optional[str] = None,
screen_name: Optional[str] = None,
output: Optional[str] = None,
):
"""Fetch and dump users.
Args:
people_type (Optional[str], optional):
Type of people to fetch; FRIENDS or FOLLOWERS.
Defaults to None.
screen_name (Optional[str], optional):
User to fetch people from.
Defaults to None.
input (Optional[str], optional):
Alternative source of users from list.
Defaults to None.
output (Optional[str], optional):
Output path to dump users.
Defaults to None.
Raises:
ValueError: [description]
ValueError: [description]
"""
# authenticate first; needed in many places
api = _authenticate()

# if we didn't specify someone else, default to authenticated user
if not screen_name:
screen_name = api.verify_credentials().screen_name

# prepare paths
if output:
output_path = Path(output)
elif people_type:
output_path = Path().cwd() / f"{screen_name}-{people_type}.json".lower()
else:
raise ValueError("Invalid output configuration.")

# choose where get people from: list or user
if people_type:
people = _fetch_people_from_user(
api=api, people_type=people_type, screen_name=screen_name
)
else:
raise ValueError("Either PEOPLE_TYPE or PATH must not be NONE")
LOGGER.info(f"Retrieved {len(people)} people")

# dump to file
LOGGER.info(f"Dumping results to {output_path.resolve()}")
with open(output_path, "w") as f:
json.dump(obj=people, fp=f, indent=4)


def prune_tweets(
days: int = settings.twitter.tweet_prune_days,
delete_liked: bool = settings.twitter.is_delete_liked,
Expand Down

0 comments on commit 49f8e71

Please sign in to comment.