Skip to content

Instantly share code, notes, and snippets.

@H3wastooshort
Last active June 19, 2023 21:17
Show Gist options
  • Save H3wastooshort/1c89e791bb966815fee61aa2eb561fce to your computer and use it in GitHub Desktop.
Save H3wastooshort/1c89e791bb966815fee61aa2eb561fce to your computer and use it in GitHub Desktop.
Search the lemmyverse for the subreddits you are subscribed to on reddit.
lemmy_communities_url = "https://browse.feddit.de/communities.json?nocache="
import json, urllib.request, time, sys, os
print("H3's Reddit to Lemmy script v0.4.1")
savefile_name = ""
use_savefile = 0
savefile_data = {}
if len(sys.argv) >= 2:
savefile_name = sys.argv[1]
if os.path.isfile(savefile_name):
sf = open(savefile_name,"r")
savefile_data = json.loads(sf.read())
use_savefile = 2
sf.close()
else:
use_savefile = 1
if use_savefile == 1:
print("New savefile " + savefile_name + " will be created on save.")
elif use_savefile == 2:
print("Using data from savefile " + savefile_name)
print("Only new communities will be shown. If you dont want this, don't specify a savefile.")
else:
print(u"You can specify a save-file with\u001b[1m reddit_to_lemmy.py [filename] \u001b[0m")
print("This can be used to come back in a few weeks and see what new communities appeared")
subs = []
if use_savefile != 2:
print()
print("1. Go to https://www.reddit.com/subreddits/mine")
print("2. Right-click on \"multireddit of your subscriptions\" in the sidebar, and copy the url.")
print("3. Paste the URL below")
multireddit_url = input("Multireddit URL: ")
print("Parsed subs:",end=" ")
multireddit_url = multireddit_url[multireddit_url.index("/r/")+3:] #cut out reddit.com url part
subs = multireddit_url.split("+")
if use_savefile == 1:
savefile_data["reddit_subs"] = subs
else:
subs = savefile_data["reddit_subs"]
print("Read subs:",end=" ")
print(len(subs))
lemmy_communities_url = lemmy_communities_url + str(int(time.time()))
print("Getting lemmy communities from " + lemmy_communities_url)
communities_json = urllib.request.urlopen(lemmy_communities_url).read()
communities = json.loads(communities_json)
found_by_c = {}
found_by_s = {}
found_total = 0
for c in communities:
name = c["community"]["name"]
url = c["url"]
if not (c["community"]["removed"] or c["community"]["deleted"]):
if name in subs:
found_total += 1
if not name in found_by_c.keys():
found_by_c[name] = []
found_by_c[name].append(url)
if not url in found_by_s.keys():
found_by_s[url] = []
found_by_s[url].append(name)
if use_savefile == 2:
#make copy of old data
old_c = savefile_data["lemmy_by_c"].copy()
old_s = savefile_data["lemmy_by_s"].copy()
old_t = savefile_data["lemmy_total"]
if use_savefile in [1,2]:
#put new data into savefile dict (only saved on user request) and pointers into shorter variable names
new_c = savefile_data["lemmy_by_c"] = found_by_c.copy()
new_s = savefile_data["lemmy_by_s"] = found_by_s.copy()
savefile_data["lemmy_total"] = found_total
if use_savefile == 2:
#keep only difference
for cmn in new_c.keys():
if cmn in old_c.keys():
#clear aready known communities
found_by_c[cmn]=[e for e in found_by_c[cmn] if e not in old_c[cmn]]
#remove communities that have been cleared fully
if len(found_by_c[cmn])==0:
found_by_c.pop(cmn)
for srv in new_s.keys():
if srv in old_s.keys():
#clear already known servers
found_by_s[srv]=[e for e in found_by_s[srv] if e not in old_s[srv]]
#remove servers that have been cleared fully
if len(found_by_s[srv])==0:
found_by_s.pop(srv)
found_total -= savefile_data["lemmy_total"]
print("Matched %d unique communities (%d in total) on %d servers." % (len(found_by_c),found_total,len(found_by_s)))
while True:
print()
print("How do you want the list grouped?")
print("1: by community")
print("2: by server")
print("3: not at all")
print("4: save save-file")
print("other: quit")
list_mode = input("Mode (1|2|3|4): ")
print("\n")
if list_mode=='1':
for cmn in found_by_c.keys():
print(cmn+":")
for srv in found_by_c[cmn]:
print("\t"+srv)
elif list_mode=='2':
for srv in found_by_s.keys():
print(srv+":")
for cmn in found_by_s[srv]:
print("\t"+cmn)
elif list_mode=='3':
for cmn in found_by_c.keys():
for srv in found_by_c[cmn]:
print(cmn + "@" + srv)
elif list_mode=='4':
sf = open(savefile_name, "w")
sf.write(json.dumps(savefile_data))
sf.close()
print("Saved " + savefile_name)
else:
quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment