Skip to content

Commit

Permalink
fixed #2 hopefully for last time
Browse files Browse the repository at this point in the history
  • Loading branch information
jheidecker committed Jul 8, 2023
1 parent 0867ce0 commit c79c01a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ cli.py: error: the following arguments are required: -l/--local, -u/--username,
### Include and exclude instances
By default lemmony tries to subscribe to as many communities as it can based on the global lists provided by [lemmyverse.net](lemmyverse.net) You can include and exclude instances using the optional `-i` and `-e` flags respectively. This way you cal only subscribe to favorites or exclude instances you do not want to subscribe to. The flags cannot be used at the same time. For example:
By default lemmony tries to subscribe to as many communities as it can based on the global lists provided by [lemmyverse.net](lemmyverse.net) You can include and exclude instances using the optional `-i` and `-e` flags respectively. This way you can only subscribe to favorite instances, or exclude instances you do not want. The flags cannot be used at the same time. For example:
To add communities from all instances except `lemmy.world`:
Expand Down
43 changes: 42 additions & 1 deletion lemmony/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def main():
parser.add_argument('-r', '--rate-limit', help='if specified, will rate limit requests to LOCAL to this many per second (default: 15)', type=int, default=15)
parser.add_argument('-t', '--top-only', help='if specified, only discover top X communities based on active users per day (Lemmy only) (default: get all non-empty communities)', type=int)
parser.add_argument('-k', '--skip-kbin', help='if specified, will not discover kbin communities (will still subscribe if they are communities on instance)', action='store_true')
parser.add_argument('-x', '--unsubscribe-all', help='forgo all other functions and unsubscribe the USER from all communities on instance', action='store_true')
args = parser.parse_args()

# define local instance, username, password and include/exclude lists
Expand All @@ -26,6 +27,7 @@ def main():
no_pending = args.no_pending
subscribe_only = args.subscribe_only
discover_only = args.discover_only
unsubscribe_all = args.unsubscribe_all
skip_kbin = args.skip_kbin

if args.top_only is not None:
Expand Down Expand Up @@ -191,14 +193,53 @@ def subscribe():
time.sleep(rl_sleep)
sub_resp = curSession.post('https://'+local_instance+'/api/v3/community/follow', data='{"community_id": ' + str(community_id) + ', "follow": true, "auth": "' + auth_token + '"}', headers={"Cookie": "jwt=" + auth_token, "Content-Type": "application/json"})
print('\r\033[K', end='\r')
print(str(idx) + "/" + local_community_count + " " + str(community_id) + ": " + str(sub_resp.json()['community_view']['subscribed']), end="\r")
print(str(idx) + "/" + local_community_count + " " + str(community_id), end="\r")
print('\r\033[K', end='\r')
print('done.')

def unsubscribe():
# fetch a list of communities by id that the user is not already subscribed to
print('re-fetching communities ready for un-subscription (this might take a while)...')
local_community_id_list = []
new_results = True
page = 1
while new_results:
time.sleep(rl_sleep)
actor_resp = curSession.get('https://'+local_instance+'/api/v3/community/list?type_=All&limit=50&page=' + str(page) + '&auth=' + auth_token, headers={"Content-Type": "application/json"})
if actor_resp.json()['communities'] != []:
for community in actor_resp.json()['communities']:
if community['subscribed'] == 'NotSubscribed':
#print("f:subscribed:skipped: " + community['community']['actor_id'])
continue
else:
local_community_id_list.append(community['community']['id'])
#print("f:non-subscribed:added " + community['community']['actor_id'])
print(page * 50, end="\r")
page += 1
else:
new_results = False
print('done.')

# store and display total in the list for displaying progress
local_community_count = str(len(local_community_id_list))
print('found ' + local_community_count + ' communities to un-subscribe from.')

# unsubscribe the user to all unsubscribed communities
print('subscribing ' + username + ' to communities (this will take a while)...')
for idx, community_id in enumerate(local_community_id_list, 1):
time.sleep(rl_sleep)
sub_resp = curSession.post('https://'+local_instance+'/api/v3/community/follow', data='{"community_id": ' + str(community_id) + ', "follow": false, "auth": "' + auth_token + '"}', headers={"Cookie": "jwt=" + auth_token, "Content-Type": "application/json"})
print('\r\033[K', end='\r')
print(str(idx) + "/" + local_community_count + " " + str(community_id), end="\r")
print('\r\033[K', end='\r')
print('done.')

if discover_only == True:
discover()
elif subscribe_only == True:
subscribe()
elif unsubscribe_all == True:
unsubscribe()
else:
discover()
subscribe()
Expand Down

0 comments on commit c79c01a

Please sign in to comment.