Ejemplo n.º 1
0
def poll_for_updates(ctx):
    has_snaps = False

    for snap in reversed(ctx.storage.snapchat.get_snaps()):
        has_snaps = True
        sender = snap["sender"]

        blob = ctx.storage.snapchat.get_blob(snap["id"])
        if blob is None:
            continue

        if snap["media_type"] in (MEDIA_VIDEO, MEDIA_VIDEO_NOAUDIO):
            blob = convert_to_gif(blob)

        if blob is not None:
            ulim = requests.post("https://api.imgur.com/3/upload.json",
                                 headers={
                                     "Authorization":
                                     "Client-ID " + ctx.config.imgur_clientid
                                 },
                                 data={
                                     "image": blob
                                 }).json()
            if ulim["status"] != 200:
                link = "(unavailable)"
            else:
                link = ulim["data"]["link"]
        else:
            link = ctx._("(could not convert video)")

        for client_name, client in ctx.bot.clients.items():
            for channel in client.channels:
                c_ctx = HookContext(service, ctx.bot, client, channel)

                if not c_ctx.config.announce:
                    continue

                c_ctx.message(
                    ctx._("New snap from {sender} ({dt})! {link}").format(
                        sender=sender,
                        link=link,
                        dt=humanize.naturaltime(
                            datetime.fromtimestamp(snap["sent"] / 1000.0))))

        ctx.storage.snapchat.mark_viewed(snap["id"])

    if has_snaps:
        ctx.storage.snapchat._request(
            "clear", {"username": ctx.storage.snapchat.username})
Ejemplo n.º 2
0
def setup_webserver(ctx):
    ctx.storage.application = Application([
        (r"/", IndexHandler),
        (r"/(\S+)/.*", MainHandler),
        (r".*", NotFoundHandler)
    ],
        template_path=os.path.join(base_path, "templates"),
        static_path=os.path.join(base_path, "static"),
        autoreload=False,
        compiled_template_cache=False,
        ui_modules={
            "NavBar": NavBarModule,
            "Title": TitleModule,
            "Footer": FooterModule
        }
    )
    ctx.storage.application._ctx = HookContext(service, ctx.bot)
    ctx.storage.application.name = None

    @ctx.bot.event_loop.schedule
    def _callback():
        ctx.storage.http_server = HTTPServer(ctx.storage.application,
                                             io_loop=ctx.bot.event_loop.io_loop)
        ctx.storage.http_server.listen(ctx.config.port, ctx.config.address)
        service.logger.info("web server ready")
Ejemplo n.º 3
0
def poll_for_updates(ctx):
    has_snaps = False

    for snap in reversed(ctx.storage.snapchat.get_snaps()):
        has_snaps = True
        sender = snap["sender"]

        blob = ctx.storage.snapchat.get_blob(snap["id"])
        if blob is None:
            continue

        if snap["media_type"] in (MEDIA_VIDEO, MEDIA_VIDEO_NOAUDIO):
            blob = convert_to_gif(blob)

        if blob is not None:
            ulim = requests.post("https://api.imgur.com/3/upload.json",
                                 headers={"Authorization": "Client-ID " + ctx.config.imgur_clientid},
                                 data={"image": blob}).json()
            if ulim["status"] != 200:
                link = "(unavailable)"
            else:
                link = ulim["data"]["link"]
        else:
            link = ctx._("(could not convert video)")

        for client_name, client in ctx.bot.clients.items():
            for channel in client.channels:
                c_ctx = HookContext(service, ctx.bot, client, channel)

                if not c_ctx.config.announce:
                    continue

                c_ctx.message(
                    ctx._("New snap from {sender}! {link} ({dt})").format(
                        sender=sender,
                        link=link,
                        dt=humanize.naturaltime(datetime.fromtimestamp(snap["sent"] / 1000.0))
                    )
                )

        ctx.storage.snapchat.mark_viewed(snap["id"])

    if has_snaps:
        ctx.storage.snapchat._request("clear", {
            "username": ctx.storage.snapchat.username
        })
Ejemplo n.º 4
0
    def _run_request(self, name):
        service, application_factory = self._get_application_factory(name)

        application = application_factory(self.settings)
        application._ctx = self.application._ctx
        application.ctx = HookContext(service, self.application._ctx.bot)
        application.name = name

        path = self.request.path[len(name) + 1:]

        request = copy.copy(self.request)
        request.path = path

        application(request)
        self._handled = True
Ejemplo n.º 5
0
    def _run_request(self, name):
        service, application_factory = self._get_application_factory(name)

        application = application_factory(self.settings)
        application._ctx = self.application._ctx
        application.ctx = HookContext(service, self.application._ctx.bot)
        application.name = name

        uri = self.request.uri[len(name) + 1:]

        application(HTTPRequest(self.request.method, uri,
                                self.request.version, self.request.headers,
                                self.request.body, self.request.remote_ip,
                                self.request.protocol, self.request.host,
                                self.request.files, self.request.connection))
        self._handled = True
Ejemplo n.º 6
0
        def _callback(future):
            if future.exception() is not None:
                self.send_error(500)
                raise future.exception()
            try:
                self.finish()
            except:
                pass

            for client_name, client in self.application.ctx.bot.clients.items():
                for channel in client.channels:
                    c_ctx = HookContext(service, self.application.ctx.bot, client, channel)

                    if not c_ctx.config.announce:
                        continue

                    for line in get_log(head, "HEAD"):
                        client.notice(channel, self.application.ctx._("Update! {line}").format(
                            line=line
                        ))
Ejemplo n.º 7
0
def _get_application_confs(bot):
    for hook in bot.get_hooks("services.net.webserver"):
        conf = hook(HookContext(hook.service, bot))

        if conf:
            yield hook.service, conf