Re: How to download an image with libsoup and save it to filesystem in gjs



Hi Michael,

Not an answer to your question as such, but you might find the following sample code helpful - it downloads an image and saves it to a file:

let Gtk = imports.gi.Gtk
let Soup = imports.gi.Soup
let Gio = imports.gi.Gio

Gtk.init(null)

let session = new Soup.Session({})
let url = "" href="http://i.imgur.com/LaqDoW7.png">http://i.imgur.com/LaqDoW7.png"

session.queue_message(
    Soup.Message.new_from_uri("GET", new Soup.URI(url)),
    (sess, msg) => {
        let file = Gio.File.new_for_path('imgur.png')
        let outstream = file.replace(null, false, Gio.FileCreateFlags.NONE, null)
        outstream.write_bytes(
            msg.response_body.flatten().get_as_bytes(),
            null)
        Gtk.main_quit()
    }, null)

Gtk.main()

Cheers,
Sam

On 11 June 2017 at 01:42, <philip chimento gmail com> wrote:
Hi,

Don't forget to reply-all to the list, please — that will help others who run into the same problem.

You aren't using ByteArray correctly there. I suggest double-checking the documentation that I linked.

However, getting the same error actually points to "image.data" being the culprit. Does "image.data" by itself on the line also throw the same exception? It looks like you should use image.get_data() instead, according to the libsoup docs: http://devdocs.baznga.org/soup24~2.56.0/soup.buffer#method-get_data

On Sat, Jun 10, 2017 at 5:10 PM Michael Teuscher <mk teuscher gmail com> wrote:
Thanks for the link, but now the error is getting even worse.

Same line number, same error, completely different code

let a = new ByteArray(image.data.length);
I've got absolutely no clue why 

2017-06-11 1:12 GMT+02:00 <philip chimento gmail com>:
Aha, that is easier, thanks. GLib.Bytes.new_from_bytes doesn't seem to be used correctly - it takes at least an offset and a length parameter? What you want in this case, I think, is a ByteArray. See the documentation here: https://github.com/GNOME/gjs/blob/master/doc/ByteArray.md

GJS console is just an executable ("/usr/bin/gjs") with which you can execute a GJS script directly.


On Sat, Jun 10, 2017 at 3:46 PM Michael Teuscher <mk teuscher gmail com> wrote:
Huff, dont have a lot of experience with the gjs-console, but I can tell you which line fails:


-> let bytes = GLib.Bytes.prototype.new_from_bytes(image.data);
This one is failing with JS ERROR: Error: Failed to convert UTF-8 string to JS string: Invalid byte sequence

I really dont know what is wrong with that line.



2017-06-11 0:35 GMT+02:00 <philip chimento gmail com>:
Is it possible to make a minimal example which isolates the problem? For example, just a script that can be run with gjs-console instead of the whole shell extension?

Your analysis seems plausible. Is the ostream.write_bytes() call trying to convert to UTF8? It should not be, if it's correctly annotated.



On Sat, Jun 10, 2017 at 3:27 PM Michael Teuscher <mk teuscher gmail com> wrote:
Hello,

Thanks for your response, I published the program on my git server.

https://git.scaly.ch/projects/TS/repos/gnome-spotlight/browse

libsoup is returning a byte array but GIO (or something else) tries to convert it to UTF 8 when I want to write it down into a file.

Thanks a lot

Regards
Michael Teuscher



_______________________________________________
_javascript_-list mailing list
_javascript_-list gnome org
https://mail.gnome.org/mailman/listinfo/_javascript_-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]