Example #1
0
    def json_fetched(self, session, message, data):
        if self.cancellable.is_cancelled():
            return self.emit('failed', _('Plugin was disabled while the ' +
                                         'cover was being downloaded'))

        # LastFM always return code 200, but we might still get
        # something else in who knows what cases.
        if 200 <= message.get_property('status-code') < 400:
            r = message.get_property('response-body').flatten().get_data()
            rjson = json.loads(r)
            album = rjson.get('album', {})
            if album:
                covers = dict((img['size'], img['#text'])
                              for img in album['image'])
                cover = covers.get('mega', covers.get('extralarge', None))
                if cover:
                    msg = Soup.Message.new('GET', cover)
                    session.queue_message(msg, self.cover_fetched, data)
                else:
                    self.emit('failed', _("Didn't found satisfactory cover"))
                return
        elif 100 <= message.get_property('status-code'):
            # Status codes less than 100 usually mean a failure on our side
            # (e.g. no internet connection)
            self.emit('failed', _('Could not download cover, failure on ' +
                                   'our side'))
            return

        self.emit('failed', _('Could not get album data from LastFM'))
        self.tried_urls[self.url] = GLib.get_real_time()
Example #2
0
    def __init__(self, filepath, size=128, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.props.name = "stash-container"
        self.path = filepath

        self.pixbuf_original = GdkPixbuf.PixbufAnimation.new_from_file(
            filepath)
        self.pixbuf_original_height = self.pixbuf_original.get_height()
        self.pixbuf_original_width = self.pixbuf_original.get_width()
        self.iter = self.pixbuf_original.get_iter()
        for i in range(0, 250):
            timeval = GLib.TimeVal()
            timeval.tv_sec = int(str(GLib.get_real_time())[:-3])
            self.iter.advance(timeval)
            self.queue_draw()

        self.ratio_h_w = self.pixbuf_original_height / self.pixbuf_original_width
        self.ratio_w_h = self.pixbuf_original_width / self.pixbuf_original_height

        if self.ratio_w_h > 1:
            self.set_size_request(size, int((10 / 16) * size) + 1)
        else:
            self.set_size_request(size, size)

        drawing_area = Gtk.DrawingArea()
        drawing_area.props.expand = True
        drawing_area.props.halign = drawing_area.props.valign = Gtk.Align.FILL
        drawing_area.connect("draw", self.draw)
        drawing_area.props.can_focus = False

        self.attach(drawing_area, 0, 0, 1, 1)
        self.props.halign = self.props.valign = Gtk.Align.CENTER
Example #3
0
 def on_token(self, session, message, data=None):
     status = message.status_code
     if not 200 <= status < 400:
         logger.error('Token request failed (HTTP {0})'.format(status))
     else:
         self.edit_token = message.response_body.data
         self.edit_token_expire = int(GLib.get_real_time() + 1.5E9) #µs
     self.status.update({'PROGRESS': False, 'OK': 200 <= status < 400})
     self.notify('status')
Example #4
0
    def do_fade_inout(self, fadein):
        fadein = bool(fadein)
        self.fading_in = fadein
        now = GLib.get_real_time()

        fraction = self.get_opacity()
        if not fadein:
            fraction = 1.0 - fraction
        self.fade_start_time = now - fraction * self.FADETIME

        if self.iteration_source is None:
            self.iteration_source = GLib.timeout_add(self.MS,
                    self.fade_iteration_callback)
Example #5
0
    def do_fade_inout(self, fadein):
        fadein = bool(fadein)
        self.fading_in = fadein
        now = GLib.get_real_time()

        fraction = self.get_opacity()
        if not fadein:
            fraction = 1.0 - fraction
        self.fade_start_time = now - fraction * self.FADETIME

        if self.iteration_source is None:
            self.iteration_source = GLib.timeout_add(self.MS,
                    self.fade_iteration_callback)
Example #6
0
    def fade_iteration_callback(self):
        delta = GLib.get_real_time() - self.fade_start_time
        fraction = delta / self.FADETIME

        if self.fading_in:
            self.set_opacity(fraction)
        else:
            self.set_opacity(1.0 - fraction)

        if not self.is_composited():
            self.queue_draw()

        if fraction >= 1.0:
            self.iteration_source = None
            self.emit('fade-finished', self.fading_in)
            return False
        return True
Example #7
0
    def fade_iteration_callback(self):
        delta = GLib.get_real_time() - self.fade_start_time
        fraction = delta / self.FADETIME

        if self.fading_in:
            self.set_opacity(fraction)
        else:
            self.set_opacity(1.0 - fraction)

        if not self.is_composited():
            self.queue_draw()

        if fraction >= 1.0:
            self.iteration_source = None
            self.emit('fade-finished', self.fading_in)
            return False
        return True
Example #8
0
    def cover_fetched(self, session, message, data):
        if self.cancellable and self.cancellable.is_cancelled():
            return self.emit('failed', _('Plugin was disabled while the ' +
                                         'cover was being downloaded'))

        if 200 <= message.get_property('status-code') < 400:
            data['body'] = message.get_property('response-body')
            data['file'] = Gio.file_new_for_path(self.cover_path)
            data['file'].replace_async(None, False, Gio.FileCreateFlags.NONE,
                                       GLib.PRIORITY_DEFAULT, self.cancellable,
                                       self.cover_file_opened, data)
        elif 100 <= message.get_property('status-code'):
            # Status codes less than 100 usually mean a failure on our side
            # (e.g. no internet connection)
            self.emit('failed', _('Could not download cover, failure on ' +
                                   'our side'))
        else:
            self.tried_urls[self.url] = GLib.get_real_time()
            self.emit('failed', _('Server did not return a cover'))
Example #9
0
def time_ago(timestamp):
    """
    String representation of how long ago timestamp is from current date
    """
    seconds = GLib.get_real_time() / 1E6 - timestamp
    if seconds < 0:
        logger.warning('Timestamp in the future')
        return _('From the future')
    elif seconds < 60:
        return _('Just now')
    elif seconds < 3600:
        minutes = int(seconds / 60)
        min_fmt = ngettext('{0} minute ago', '{0} minutes ago', minutes)
        return min_fmt.format(minutes)
    elif seconds < 86400:
        hours = int(seconds / 3600)
        hour_fmt = ngettext('{0} hour ago', '{0} hours ago', hours)
        return hour_fmt.format(hours)
    else:
        days = int(seconds / 86400)
        day_fmt = ngettext('{0} day ago', '{0} days ago', days)
        return day_fmt.format(days)
Example #10
0
 def should_download(self, url):
     hour_us = 3600000000
     return GLib.get_real_time() - self.tried_urls.get(url, 0) > hour_us
Example #11
0
 def cover_written(self, stream, result, data):
     stream.write_bytes_finish(result)
     stream.close(None)  # Operation is cheap enough to not care about async
     self.tried_urls[self.url] = GLib.get_real_time()
     self.emit('cover-found', self.cover)
Example #12
0
 def token_valid(self):
     return GLib.get_real_time() < self.edit_token_expire
Example #13
0
 def _start_progress_countdown(self):
     self._stop_progress_countdown()
     time_seconds = GLib.get_real_time() / 1000000
     self.counter = math.floor(self.counter_max - (time_seconds % self.counter_max))
     self._timeout_id = GLib.timeout_add_seconds(1, self.__update_counter,
                                                 None)