[O] Use write function in data processing

This commit is contained in:
Hykilpikonna
2021-11-22 14:00:28 -05:00
parent a73a792189
commit 9e27a3c725
+7 -28
View File
@@ -18,23 +18,7 @@ class UserPopularity(NamedTuple):
popularity: int popularity: int
@dataclass def process_users_popularity(user_dir: str = './data/twitter/user/') -> None:
class Posting:
# Which platform did the user post on
platform: str
# Username on that platform
username: str
# Full text of the post's content
text: str
# Popularity of the post
popularity: int
# Is it a repost
repost: bool
# Date
date: datetime
def process_users_popularity(user_dir: str = './data/twitter/user/') -> list[UserPopularity]:
""" """
After downloading a wide range of users using download_users_start in raw_collect/twitter.py, After downloading a wide range of users using download_users_start in raw_collect/twitter.py,
this function will read the user files and rank the users by popularity. this function will read the user files and rank the users by popularity.
@@ -44,19 +28,18 @@ def process_users_popularity(user_dir: str = './data/twitter/user/') -> list[Use
:param user_dir: Download directory of users data, should be the same as the downloads dir in :param user_dir: Download directory of users data, should be the same as the downloads dir in
download_user_start. (Default: "./data/twitter/user/") download_user_start. (Default: "./data/twitter/user/")
:return: List of users' screen names and popularity, sorted descending by popularity. :return: None
""" """
user_dir = normalize_directory(user_dir) + '/users' user_dir = normalize_directory(user_dir)
users = [] users = []
# Loop through all the files # Loop through all the files
for filename in os.listdir(user_dir): for filename in os.listdir(f'{user_dir}/users'):
# Only check json files and ignore macos dot files # Only check json files and ignore macos dot files
if filename.endswith('.json') and not filename.startswith('.'): if filename.endswith('.json') and not filename.startswith('.'):
# Read # Read
with open(f'{user_dir}/{filename}', 'r', encoding='utf-8') as f: user = json.loads(read(f'{user_dir}/users/{filename}'))
user = json.load(f) users.append(UserPopularity(user['screen_name'], user['followers_count']))
users.append(UserPopularity(user['screen_name'], user['followers_count']))
# Log progress # Log progress
if len(users) % 2000 == 0: if len(users) % 2000 == 0:
@@ -66,11 +49,7 @@ def process_users_popularity(user_dir: str = './data/twitter/user/') -> list[Use
users.sort(key=lambda x: x.popularity, reverse=True) users.sort(key=lambda x: x.popularity, reverse=True)
# Save data # Save data
with open(f'{user_dir}/../processed_popularity.json', 'w', encoding='utf-8') as f: write(f'{user_dir}/processed/popularity.json', json_stringify(users, indent=None))
f.write(json_stringify(users, indent=None))
# Return
return users
def load_users_popularity(user_dir: str = './data/twitter/user/') -> list[UserPopularity]: def load_users_popularity(user_dir: str = './data/twitter/user/') -> list[UserPopularity]: