kensanata / mastodon-archive

Archive your statuses, favorites and media using the Mastodon API (i.e. login required)
https://alexschroeder.ch/software/Mastodon_Archive
GNU General Public License v3.0
362 stars 33 forks source link

mastodon-archive media: TypeError: can only concatenate str (not "bytes") to str #64

Closed melissaboiko closed 4 years ago

melissaboiko commented 4 years ago
$ mastodon-archive media elilla@queer.party
1132 urls in your backup (half of them are previews)
Downloading |###                             | 110/1132Traceback (most recent call last):
  File "/usr/local/bin/mastodon-archive", line 10, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.7/dist-packages/mastodon_archive/__init__.py", line 293, in main
    args.command(args)
  File "/usr/local/lib/python3.7/dist-packages/mastodon_archive/media.py", line 66, in media
    file_name = media_dir + path
TypeError: can only concatenate str (not "bytes") to str

This is with 1.3.1 installed today via pip3 .

melissaboiko commented 4 years ago

After some investigation of the offending case:

    for url in reversed(urls):
        bar.next()
        path = urlparse(url).path
        file_name = media_dir + path

Here url is None, which makes urlparse(url).path be an empty bytes rather than a string.

My urls object at that point had 10 None urls.

kensanata commented 4 years ago

Ah, interesting! When I tried it a few hours ago I could not reproduce it but that would explain it and it would also be easy to avoid: just skip them, right? Or is the attachment still visible via the web interface?

melissaboiko commented 4 years ago

I have no idea what it means to have an empty URL at this point of the code, but it seems resilient to skip them. I would like to understand why these URLs are coming like that, but I don’t know how…

melissaboiko commented 4 years ago

oh I added some logging and I think I figured it out!

All the 10 statuses are audio attachments, and audio attachments have no preview_url! you could filter the Nones out already before urls.append(url) I think

kensanata commented 4 years ago

Yeah, 2ded461 should do it, I guess?

melissaboiko commented 4 years ago

Yeah, I did a little thing to fix the preview count too, feel free to ignore or adapt to your tastes if you want. ty for the work!

kensanata commented 4 years ago

Thanks! Works for me.