Skip to content

Instantly share code, notes, and snippets.

@fnordomat
Created September 10, 2022 09:23
Show Gist options
  • Save fnordomat/edf3191817fd5c516eb08ff74d2c7312 to your computer and use it in GitHub Desktop.
Save fnordomat/edf3191817fd5c516eb08ff74d2c7312 to your computer and use it in GitHub Desktop.
# Will probably trigger rate limiting.
# It would be a lot smarter to mirror the whole history and then search offline.
from mastodon import Mastodon as M
from optparse import OptionParser
import sys
import re
parser = OptionParser()
parser.add_option("-a", "--api-token", dest="apitoken",
help="use this APITOKEN", metavar="APITOKEN")
parser.add_option("-s", "--server", dest="server",
help="use this INSTANCE", metavar="INSTANCE")
(options, args) = parser.parse_args()
if len(args) < 1:
print("required: search query", file=sys.stderr)
sys.exit(-1)
if options.server is None:
print("required option: -s", file=sys.stderr)
sys.exit(-1)
if options.apitoken is None:
print("required option: -a", file=sys.stderr)
sys.exit(-1)
m = M(
api_base_url=options.server,
access_token=options.apitoken
)
myid = m.me().id
def searchmytoots(f, first_only=False):
done = False
maxid = None
while not done:
print("--- searching <%s ---" % maxid)
tranche = m.account_statuses(id=myid, max_id=maxid)
maxid = min([a["id"] for a in tranche])
for s in tranche:
if f(s):
print()
print("found matching toot:")
print(s.get('created_at'))
print(s.get('url'))
# print(s.get('content'))
print()
if first_only:
return True
print("found no match")
return False
searchmytoots(lambda x: re.search(args[0], x.get("content")), first_only=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment