this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

followers/friends --sql/--attach options, closes #36

+61 -41
+4 -3
README.md
··· 92 92 93 93 ## Retrieving Twitter followers 94 94 95 - The `followers` command retrieves details of every follower of the specified account. You can use it to retrieve your own followers, or you can pass a screen_name to pull the followers for another account. 95 + The `followers` command retrieves details of every follower of the specified accounts. You can use it to retrieve your own followers, or you can pass one or more screen names to pull the followers for other accounts. 96 96 97 97 The following command pulls your followers and saves them in a SQLite database file called `twitter.db`: 98 98 ··· 102 102 103 103 To retrieve followers for another account, use: 104 104 105 - $ twitter-to-sqlite followers twitter.db --screen_name=cleopaws 105 + $ twitter-to-sqlite followers twitter.db cleopaws 106 + 107 + This command also accepts the `--ids`, `--sql` and `--attach` options. 106 108 107 109 See [Analyzing my Twitter followers with Datasette](https://simonwillison.net/2018/Jan/28/analyzing-my-twitter-followers/) for the original inspiration for this command. 108 - 109 110 110 111 ## Retrieving friends 111 112
+57 -38
twitter_to_sqlite/cli.py
··· 89 89 type=click.Path(file_okay=True, dir_okay=False, allow_dash=False), 90 90 required=True, 91 91 ) 92 + @add_identifier_options 92 93 @click.option( 93 94 "-a", 94 95 "--auth", ··· 96 97 default="auth.json", 97 98 help="Path to auth.json token file", 98 99 ) 99 - @click.option("--user_id", help="Numeric user ID") 100 - @click.option("--screen_name", help="Screen name") 100 + @click.option("--ids", is_flag=True, help="Treat input as user IDs, not screen names") 101 101 @click.option("--silent", is_flag=True, help="Disable progress bar") 102 - def followers(db_path, auth, user_id, screen_name, silent): 103 - "Save followers for specified user (defaults to authenticated user)" 104 - _shared_friends_followers(db_path, auth, user_id, screen_name, silent, "followers") 102 + def followers(db_path, identifiers, attach, sql, auth, ids, silent): 103 + "Save followers for specified users (defaults to authenticated user)" 104 + _shared_friends_followers( 105 + db_path, identifiers, attach, sql, auth, ids, silent, "followers" 106 + ) 105 107 106 108 107 - def _shared_friends_followers(db_path, auth, user_id, screen_name, silent, noun): 109 + def _shared_friends_followers( 110 + db_path, identifiers, attach, sql, auth, ids, silent, noun 111 + ): 108 112 assert noun in ("friends", "followers") 109 113 auth = json.load(open(auth)) 110 114 session = utils.session_for_auth(auth) 111 115 db = utils.open_database(db_path) 112 - fetched = [] 113 - # Get the follower count, so we can have a progress bar 114 - count = 0 115 116 116 - profile = utils.get_profile(db, session, user_id, screen_name) 117 - screen_name = profile["screen_name"] 118 - user_id = profile["id"] 117 + identifiers = utils.resolve_identifiers(db, identifiers, attach, sql) 119 118 120 - save_users_kwargs = {} 121 - if noun == "followers": 122 - save_users_kwargs["followed_id"] = user_id 123 - elif noun == "friends": 124 - save_users_kwargs["follower_id"] = user_id 119 + if not identifiers: 120 + profile = utils.get_profile(db, session) 121 + identifiers = [profile["screen_name"]] 122 + 123 + for identifier in identifiers: 124 + if ids: 125 + kwargs = {"user_id": identifier} 126 + else: 127 + kwargs = {"screen_name": identifier} 128 + 129 + fetched = [] 130 + # Get the follower count, so we can have a progress bar 131 + count = 0 132 + 133 + profile = utils.get_profile(db, session, **kwargs) 134 + screen_name = profile["screen_name"] 135 + user_id = profile["id"] 136 + 137 + save_users_kwargs = {} 138 + if noun == "followers": 139 + save_users_kwargs["followed_id"] = user_id 140 + elif noun == "friends": 141 + save_users_kwargs["follower_id"] = user_id 125 142 126 - def go(update): 127 - for users_chunk in utils.fetch_user_list_chunks( 128 - session, user_id, screen_name, noun=noun 129 - ): 130 - fetched.extend(users_chunk) 131 - utils.save_users(db, users_chunk, **save_users_kwargs) 132 - update(len(users_chunk)) 143 + def go(update): 144 + for users_chunk in utils.fetch_user_list_chunks( 145 + session, user_id, screen_name, noun=noun 146 + ): 147 + fetched.extend(users_chunk) 148 + utils.save_users(db, users_chunk, **save_users_kwargs) 149 + update(len(users_chunk)) 133 150 134 - if not silent: 135 - count = profile["{}_count".format(noun)] 136 - with click.progressbar( 137 - length=count, 138 - label="Importing {:,} {}s for @{}".format(count, noun, screen_name), 139 - ) as bar: 140 - go(bar.update) 141 - else: 142 - go(lambda x: None) 151 + if not silent: 152 + count = profile["{}_count".format(noun)] 153 + with click.progressbar( 154 + length=count, 155 + label="Importing {:,} {} for @{}".format(count, noun, screen_name), 156 + ) as bar: 157 + go(bar.update) 158 + else: 159 + go(lambda x: None) 143 160 144 161 145 162 @cli.command() ··· 148 165 type=click.Path(file_okay=True, dir_okay=False, allow_dash=False), 149 166 required=True, 150 167 ) 168 + @add_identifier_options 151 169 @click.option( 152 170 "-a", 153 171 "--auth", ··· 155 173 default="auth.json", 156 174 help="Path to auth.json token file", 157 175 ) 158 - @click.option("--user_id", help="Numeric user ID") 159 - @click.option("--screen_name", help="Screen name") 176 + @click.option("--ids", is_flag=True, help="Treat input as user IDs, not screen names") 160 177 @click.option("--silent", is_flag=True, help="Disable progress bar") 161 - def friends(db_path, auth, user_id, screen_name, silent): 162 - "Save friends for specified user (defaults to authenticated user)" 163 - _shared_friends_followers(db_path, auth, user_id, screen_name, silent, "friends") 178 + def friends(db_path, identifiers, attach, sql, auth, ids, silent): 179 + "Save friends for specified users (defaults to authenticated user)" 180 + _shared_friends_followers( 181 + db_path, identifiers, attach, sql, auth, ids, silent, "friends" 182 + ) 164 183 165 184 166 185 @cli.command()