halcy / Mastodon.py

Python wrapper for the Mastodon ( https://github.com/mastodon/mastodon/ ) API.
MIT License
882 stars 150 forks source link

search not working anymore? #382

Open nkurtys opened 2 weeks ago

nkurtys commented 2 weeks ago

Until recently my mastodon search tool was working fine with using snowflake IDs. But now it throws an error that my Date is invalid.

raise ex_type('Mastodon API returned error', response_object.status_code, response_object.reason, error_msg)
mastodon.errors.MastodonAPIError: ('Mastodon API returned error', 422, 'Unknown Error', 'Invalid date 2024-10-16T09:07:12Z')

The timeline functions are still working though. I tested it.


Relevant code:

def saveDatabase(table=None, query=None):
#Log into App
    App = makeApp("mastodon.social")

    #Access Database
    connection = sqlite3.connect("WebApp/app/test.db")
    cursor = connection.cursor()
    #cursor.execute("DROP TABLE IF EXISTS " + table)
    cursor.execute("CREATE TABLE IF NOT EXISTS " + table + " (id int NOT NULL UNIQUE, created_at, language, uri, url, content)")
    print("Connected to table") 

    # Define initial parameters
    limit = 40
    min_id = None
    max_id = None
    now = datetime.now()
    print("Transforming Dates...")
    max_id = ( int( (now).timestamp() ) << 16 ) * 1000
    print("Dates succesfully transformed.")

    start_time = now.strftime("%H:%M:%S")
    current_time = now.strftime("%H:%M:%S")
    print("Started fetching and waiting for ratelimit to reset...", start_time)

    # Fetch a page of statuses
    if query ==  None:
        return "No query submitted"
        #Fetch a page of statuses
    else:
        #search user timeline and public hashtags for query
        results_query = App.search(query, max_id=max_id)
        statuses = results_query["statuses"] + App.timeline_hashtag(query, limit=limit, max_id=max_id)
        print(len(statuses))
Ryuno-Ki commented 2 weeks ago

How do you call saveDatabase? (Query is passed in as argument)

nkurtys commented 2 weeks ago

Thank you for responding so quickly! I call it via my Flask Web App. When I type a word into the field from tablename it executes the first elif statement and calls saveDatabase.

@app.route('/search')
def search():
    #TODO change sdave to save 60 toots 
    query = request.args.get("query", "")
    start_date = request.args.get("start_date", "")
    end_date = request.args.get("end_date", "")
    tablename = request.args.get("tablename", "")
    if query:
        #search toots for query and display it on the website
        result = functions.searchInstance(instance="mastodon.social", query=query, start_date=start_date, end_date=end_date)
    elif tablename:
        #setup new table in database
        result = functions.saveDatabase(table=tablename, query=tablename)
    else:
        result = False
        print("No query")

    return render_template('search.html', title='Search', posts=result, query=query)
nkurtys commented 2 weeks ago

So in the mastodon documentation I saw that search v1 is completely removed now from the api. So maybe thats whats causing the library to throw an error? https://docs.joinmastodon.org/methods/search/

halcy commented 2 weeks ago

oof. potentially. you could switch to search_v2, if you're happy to drop support for versions that don't have it (probably old), or wait for me to have enough motivation to actually do maint work, or add some logic to use whatever is availble in your app

nkurtys commented 1 week ago

Hello again, i tested it with search_v2 but the same error happens.