def test_30_zip_single_file(self):
     uncompressed = BytesIO(b"Hello World\n")
     uncompressed.name = 'test_file'
     raw = BytesIO()
     raw.name = "test_file.zip"
     zip = zipfile.ZipFile(raw, 'w')
     try:
         zip.writestr("test_file", uncompressed.getvalue())
     finally:
         zip.close()
     raw.seek(0)
     self._check_decompressor(
         destream.decompressors.Unzip,
         raw, uncompressed)
 def test_30_7z_single_file(self):
     uncompressed = BytesIO(b"Hello World\n")
     uncompressed.name = None
     # no file, only the content is packed, use 7zr -si to make it
     raw = BytesIO(b"7z\xbc\xaf'\x1c\x00\x03\\\x01\xca\xbe\x11\x00\x00\x00"
         b"\x00\x00\x00\x00;\x00\x00\x00\x00\x00\x00\x00\xccl\x1bR\x00"
         b"$\x19I\x98o\x10\x11\xc8_\xe6\xd5\x8a\x02\x8f\x14\x00\x01\x04"
         b"\x06\x00\x01\t\x11\x00\x07\x0b\x01\x00\x01#\x03\x01\x01\x05]"
         b"\x00\x00\x00\x01\x0c\x0c\x00\x08\n\x01\xe3\xe5\x95\xb0\x00"
         b"\x00\x05\x01\x14\n\x01\x00\xc0\x8dZ!\xf62\xcf\x01\x15\x06"
         b"\x01\x00\x00\x00\x00\x00\x00\x00")
     self._check_decompressor(
         destream.decompressors.Un7z,
         raw, uncompressed)
     uncompressed = BytesIO(b"Hello World\n")
     uncompressed.name = 'a'
     # only one file, named, but same content
     raw = BytesIO(b"7z\xbc\xaf'\x1c\x00\x03+v\xeet\x11\x00\x00\x00\x00\x00"
         b"\x00\x00B\x00\x00\x00\x00\x00\x00\x00\x10\xb9\x06\x02\x00$"
         b"\x19I\x98o\x10\x11\xc8_\xe6\xd5\x8a\x02\x8f\x14\x00\x01\x04"
         b"\x06\x00\x01\t\x11\x00\x07\x0b\x01\x00\x01#\x03\x01\x01\x05"
         b"]\x00\x00\x01\x00\x0c\x0c\x00\x08\n\x01\xe3\xe5\x95\xb0\x00"
         b"\x00\x05\x01\x11\x05\x00a\x00\x00\x00\x14\n\x01\x00\x80]]\\"
         b"\xf62\xcf\x01\x15\x06\x01\x00 \x80\xa4\x81\x00\x00")
     raw.name = "test_file.7z"
     self._check_decompressor(
         destream.decompressors.Un7z,
         raw, uncompressed)
Exemple #3
0
def parse_attachment(message_part):
    content_disposition = message_part.get("Content-Disposition", None)
    if not content_disposition:
        return None
    dispo_type, dispo_dict = parse_dispositions(content_disposition)
    if not (dispo_type == "attachment" or (dispo_type == 'inline' and
            'filename' in dispo_dict)):
        return None

    content_type = message_part.get("Content-Type", None)
    file_data = message_part.get_payload(decode=True)
    if file_data is None:
        payloads = message_part.get_payload()
        file_data = '\n\n'.join([p.as_string() for p in payloads]).encode('utf-8')
    attachment = BytesIO(file_data)
    attachment.content_type = message_part.get_content_type()
    attachment.size = len(file_data)
    attachment.name = None
    attachment.create_date = None
    attachment.mod_date = None
    attachment.read_date = None
    attachment.name = get_attachment_name(
        attachment, dispo_dict, content_type=content_type
    )

    if "create-date" in dispo_dict:
        attachment.create_date = dispo_dict['create-date']  # TODO: datetime
    if "modification-date" in dispo_dict:
        attachment.mod_date = dispo_dict['modification-date']  # TODO: datetime
    if "read-date" in dispo_dict:
        attachment.read_date = dispo_dict['read-date']  # TODO: datetime
    return attachment
 def test_20_external_pipe_zstd(self):
     uncompressed = BytesIO(b"Hello World\n")
     uncompressed.name = 'test_file'
     raw = BytesIO(
         b'%\xb5/\xfd\x08@\x00\x0cHello World\n\xc0\x00\x00')
     raw.name = "test_file.zst"
     self._check_decompressor(
         destream.decompressors.Unzstd,
         raw, uncompressed)
 def test_20_external_pipe_lzma(self):
     uncompressed = BytesIO(b"Hello World\n")
     uncompressed.name = 'test_file'
     raw = BytesIO(
         b']\x00\x00\x80\x00\xff\xff\xff\xff\xff\xff\xff\xff\x00'
         b'$\x19I\x98o\x10\x11\xc8_\xe6\xd5\x8a\x04\xda\x01\xc7'
         b'\xff\xff\x0b8\x00\x00')
     raw.name = "test_file.lzma"
     self._check_decompressor(
         destream.decompressors.Unlzma,
         raw, uncompressed)
 def test_20_external_pipe_xz(self):
     uncompressed = BytesIO(b"Hello World\n")
     uncompressed.name = 'test_file'
     raw = BytesIO(
         b'\xfd7zXZ\x00\x00\x04\xe6\xd6\xb4F\x02\x00!\x01\x16\x00\x00\x00'
         b't/\xe5\xa3\x01\x00\x0bHello World\n\x00"\xe0u?\xd5\xed8>\x00\x01'
         b'$\x0c\xa6\x18\xd8\xd8\x1f\xb6\xf3}\x01\x00\x00\x00\x00\x04YZ')
     raw.name = "test_file.xz"
     self._check_decompressor(
         destream.decompressors.Unxz,
         raw, uncompressed)
 def test_30_rar_single_file(self):
     uncompressed = BytesIO(b"Hello World\n")
     uncompressed.name = 'a'
     raw = BytesIO(
         b"Rar!\x1a\x07\x00\xcf\x90s\x00\x00\r\x00\x00\x00\x00\x00\x00\x00"
         b"\x98\xdct \x90#\x00\x19\x00\x00\x00\x0c\x00\x00\x00\x03\xe3\xe5"
         b"\x95\xb0\x05|[D\x1d3\x01\x00\xa4\x81\x00\x00a\x00\xc0\x0c\x0c"
         b"\xcb\xec\xcb\xf1\x14'\x04\x18\x81\x0e\xec\x9aL\xff\xe3?\xfe\xcf"
         b"\x05z\x99\xd5\x10\xc4={\x00@\x07\x00")
     raw.name = "test_file.rar"
     self._check_decompressor(
         destream.decompressors.Unrar,
         raw, uncompressed)
 def test_40_zip_multiple_files(self):
     uncompressed = BytesIO(b"Hello World\n")
     uncompressed.name = None
     raw = BytesIO()
     raw.name = "test_file.zip"
     zip = zipfile.ZipFile(raw, 'w')
     try:
         for filename in ('a/test_file1', 'b/test_file2'):
             zip.writestr(filename, uncompressed.getvalue())
     finally:
         zip.close()
     raw.seek(0)
     self._check_decompressor(
         destream.decompressors.Unzip,
         raw, uncompressed)
def create_img():
    image = Image.new("RGB", (500, 250), (30, 60, 90))
    output = BytesIO()
    image.save(output, 'PNG')
    output.name = 'img_for_test.png'
    output.seek(0)
    return output
 def test_post_exercise_image_assessment_item_id(self):
     File.objects.all().delete()
     item = AssessmentItem.objects.create()
     img = BytesIO(b'mybinarydata')
     img.name = 'myimage.jpg'
     self.client.post(reverse('exercise_image_upload'), {0: img, 'assessment_item_id': item.id})
     self.assertEqual(File.objects.get().assessment_item, item)
def create_dummy_image():
    file = BytesIO()
    image = Image.new('RGBA', size=(50, 50), color=(155, 0, 0))
    image.save(file, 'png')
    file.name = 'test_image.png'
    file.seek(0)
    return file
Exemple #12
0
def get_cam(message):
    """Get cam snapshot"""

    cam = CAMS.get(message.text.lower())

    try:
        url = 'http://{user}:{password}@{ip}{path}'.format(**cam)

        response = requests.get(url, stream=True)

        # start chat photo upload
        bot.send_chat_action(message.chat.id, 'upload_photo')

        # create Byte stream
        file = BytesIO(response.content)
        # add name to validate telegram extension check
        file.name = 'snapshot.jpg'

        # send the actual snapshot
        bot.send_photo(message.chat.id, file)

        file.close()
    except requests.ConnectionError:
        bot.reply_to(message, 'Câmera Offline')
    except KeyError:
        bot.reply_to(message, 'Erro de configuração na câmera "{}"'.format(
            message.text.upper()))
Exemple #13
0
    def test_upload_picture(self):
        """
        Ensure we can create picture
        """

        url = reverse('pictures-list')
        self.client.force_authenticate(user=self.test_user)

        # In-memory image file for test upload
        file_obj = BytesIO()
        image = Image.new("RGBA", size=(50,50), color=(256,0,0))
        image.save(file_obj, 'png')
        file_obj.name = 'test.png'
        file_obj.seek(0)

        data = {
            'user': self.test_user,
            'image': file_obj
        }

        response = self.client.post(url, data)
        response_data = json.loads(json.dumps(response.data))

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response_data['image'], "%s/%s" % (self.test_user.id, file_obj.name))
        self.assertEqual(response_data['average_rate'], '0.00')
def get_cadc_headers(uri):
    """
    Creates the FITS headers object by fetching the FITS headers of a CADC
    file. The function takes advantage of the fhead feature of the CADC
    storage service and retrieves just the headers and no data, minimizing
    the transfer time.

    The file must be public, because the header retrieval is done as an
    anonymous user.

    :param uri: CADC URI
    :return: a string of keyword/value pairs.
    """
    file_url = parse.urlparse(uri)
    # create possible types of subjects
    subject = net.Subject()
    client = CadcDataClient(subject)
    # do a fhead on the file
    archive, file_id = file_url.path.split('/')
    b = BytesIO()
    b.name = uri
    client.get_file(archive, file_id, b, fhead=True)
    fits_header = b.getvalue().decode('ascii')
    b.close()
    return fits_header
Exemple #15
0
    def validate(self, instance, value):
        """Checks if value is an open PNG file, valid filename, or png.Image

        Returns an open bytestream of the image
        """
        # Pass if already validated
        if getattr(value, '__valid__', False):
            return value
        # Validate that value is PNG
        if isinstance(value, png.Image):
            pass
        else:
            value = super(ImagePNG, self).validate(instance, value)
            try:
                png.Reader(value).validate_signature()
            except png.FormatError:
                self.error(instance, value, extra='Open file is not PNG.')
            value.seek(0)
        # Write input to new bytestream
        output = BytesIO()
        output.name = self.filename
        output.__valid__ = True
        if isinstance(value, png.Image):
            value.save(output)
        else:
            fid = value
            fid.seek(0)
            output.write(fid.read())
            fid.close()
        output.seek(0)
        return output
def create_in_memory_image_file(name='test_image', image_format='png', size=(512, 256), color=(128, 128, 128)):
    image = PILImage.new('RGBA', size=size, color=color)
    file = BytesIO()
    file.name = '{}.{}'.format(name, image_format)
    image.save(file, format=image_format)
    file.seek(0)
    return file
Exemple #17
0
 def create_test_photo(self, size=(50, 50)):
     file = BytesIO()
     image = Image.new('RGBA', size, color=(155, 0, 0))
     image.save(file, 'png')
     file.name = 'test.png'
     file.seek(0)
     return file
    def test_printProgressBarReporting(self):
        """
        L{StdioClient._printProgressBar} prints a progress description,
        including percent done, amount transferred, transfer rate, and time
        remaining, all based the given start time, the given L{FileWrapper}'s
        progress information and the reactor's current time.
        """
        # Use a short, known console width because this simple test doesn't
        # need to test the console padding.
        self.setKnownConsoleSize(10, 34)
        clock = self.client.reactor = Clock()
        wrapped = BytesIO(b"x")
        wrapped.name = b"sample"
        wrapper = cftp.FileWrapper(wrapped)
        wrapper.size = 1024 * 10
        startTime = clock.seconds()
        clock.advance(2.0)
        wrapper.total += 4096

        self.client._printProgressBar(wrapper, startTime)

        if _PY3:
            result = b"\rb'sample' 40% 4.0kB 2.0kBps 00:03 "
        else:
            result = "\rsample 40% 4.0kB 2.0kBps 00:03 "
        self.assertEqual(self.client.transport.value(), result)
Exemple #19
0
    def test_extract(self):
        fake_f = BytesIO("""
            <html>
                <head>
                    <meta charset="utf-8">
                    <meta name="haystack-test" content="test 1234">
                    <title>Test Title ☃&#x2603;</title>
                </head>
                    <body>foobar</body>
            </html>
        """.encode('utf-8'))
        fake_f.name = "test.html"
        extracted = self.loop.run_until_complete(self.solr.extract(fake_f))

        # Verify documented response structure:
        self.assertIn('contents', extracted)
        self.assertIn('metadata', extracted)

        self.assertIn('foobar', extracted['contents'])

        m = extracted['metadata']

        self.assertEqual([fake_f.name], m['stream_name'])

        self.assertIn('haystack-test', m, "HTML metadata should have been extracted!")
        self.assertEqual(['test 1234'], m['haystack-test'])

        # Note the underhanded use of a double snowman to verify both that Tika
        # correctly decoded entities and that our UTF-8 characters survived the
        # round-trip:
        self.assertEqual(['Test Title ☃☃'], m['title'])
Exemple #20
0
    def parse_postmark(self, obj):
        from_field = (obj['FromFull']['Name'], obj['FromFull']['Email'])
        tos = [(o['Name'], o['Email']) for o in obj['ToFull']]
        ccs = [(o['Name'], o['Email']) for o in obj['CcFull']]
        attachments = []
        for a in obj['Attachments']:
            attachment = BytesIO(base64.b64decode(a['Content']))
            attachment.content_type = a['ContentType']
            attachment.size = a['ContentLength']
            attachment.name = a['Name']
            attachment.create_date = None
            attachment.mod_date = None
            attachment.read_date = None
            attachments.append(attachment)

        return ParsedEmail(None, **{
            'postmark_msgobj': obj,
            'date': parse_date(obj['Date']),
            'subject': obj['Subject'],
            'body': obj['TextBody'],
            'html': obj['HtmlBody'],
            'from_': from_field,
            'to': tos,
            'cc': ccs,
            'resent_to': [],
            'resent_cc': [],
            'attachments': attachments
        })
Exemple #21
0
 def upload_asset(self, name="asset-1"):
     """
     Post to the asset upload url
     """
     f = BytesIO(name)
     f.name = name + ".txt"
     return self.client.post(self.url, {"name": name, "file": f})
Exemple #22
0
 def analyse_source(self, source):
     module = BytesIO(
         textwrap.dedent(source).encode('utf-8')
     )
     module.name = '<test>'
     analyser = ASTAnalyser(module)
     return analyser.analyse()
Exemple #23
0
    def test_srv_add_book(self):  # {{{
        with self.create_server(auth=True, auth_mode='basic') as server:
            server.handler.ctx.user_manager.add_user('12', 'test')
            server.handler.ctx.user_manager.add_user('ro', 'test', readonly=True)
            conn = server.connect()

            ae = self.assertEqual

            def a(filename, data=None, status=OK, method='POST', username='******', add_duplicates='n', job_id=1):
                r, data = make_request(conn, '/cdb/add-book/{}/{}/{}'.format(job_id, add_duplicates, quote(filename.encode('utf-8'))),
                                       username=username, password='******', prefix='', method=method, data=data)
                ae(status, r.status)
                return data

            def d(book_ids, username='******', status=OK):
                book_ids = ','.join(map(str, book_ids))
                r, data = make_request(conn, '/cdb/delete-books/{}'.format(book_ids),
                                       username=username, password='******', prefix='', method='POST')
                ae(status, r.status)
                return data

            a('test.epub', None, username='******', status=FORBIDDEN)
            content = b'content'
            filename = 'test add - XXX.txt'
            data = a(filename, content)
            s = BytesIO(content)
            s.name = filename
            mi = get_metadata(s, stream_type='txt')
            ae(data,  {'title': mi.title, 'book_id': data['book_id'], 'authors': mi.authors, 'languages': mi.languages, 'id': '1', 'filename': filename})
            r, q = make_request(conn, '/get/txt/{}'.format(data['book_id']), username='******', password='******', prefix='')
            ae(r.status, OK)
            ae(q, content)
            d((1,), username='******', status=FORBIDDEN)
            d((1, data['book_id']))
Exemple #24
0
 def generate_photo_file(self):
     file = BytesIO()
     image = Image.new('RGBA', size=(100, 100), color=(155, 0, 0))
     image.save(file, 'gif')
     file.name = 'test.gif'
     file.seek(0)
     return file
Exemple #25
0
def _convert_to_png(image_bytestream):
    png_image = BytesIO()
    Image.open(image_bytestream).save(png_image, format="PNG")
    png_image.name = 'cover.png'
    png_image.seek(0)

    return png_image
Exemple #26
0
    def test_binary_gpg_data(self):
        url = reverse('account:register')
        client = Client()

        self.assertEqual(User.objects.count(), 0)

        get = client.get(url)
        self.assertEqual(get.status_code, 200)
        self.assertTrue(get.context['user'].is_anonymous)
        self.assertTrue('form' in get.context)

        with open(os.path.join(testdata_path, 'non_armored.gpg'), 'rb') as stream:
            data = stream.read()
        data = BytesIO(data)
        data.name = 'non_armored.gpg'
        data.content_type = 'application/pgp-encrypted'

        with self.mock_celery() as mock:
            response = client.post(url, {
                'username_0': NODE, 'username_1': DOMAIN, 'email': EMAIL,
                'gpg_key': data,
            }, follow=True)
        self.assertEqual(User.objects.count(), 0)
        self.assertNoTasks(mock)
        self.assertFormError(response, 'form', 'gpg_key', ['Please upload an ASCII armored GPG key.'])
 def test_30_tar_single_file(self):
     uncompressed = BytesIO(b"Hello World\n")
     uncompressed.name = 'test_file'
     raw = BytesIO()
     raw.name = "test_file.tar"
     tar = tarfile.open(fileobj=raw, mode='w')
     try:
         tarinfo = tarfile.TarInfo(uncompressed.name)
         tarinfo.size = len(uncompressed.getvalue())
         tar.addfile(tarinfo, uncompressed)
         uncompressed.seek(0)
     finally:
         tar.close()
     raw.seek(0)
     self._check_decompressor(
         destream.decompressors.Untar,
         raw, uncompressed)
Exemple #28
0
 def post_random(self, msg, *args):
     """Posts a random picture. Where they are from?! Who knows… :3"""
     rand_filename = random.choice(os.listdir(os.path.join(config.workdir, "picturedump")))
     with open(os.path.join(config.workdir, "picturedump", rand_filename), "rb") as fh:
         data = BytesIO(fh.read())
         # rather stupid hack below to convince aiohttp to set the right content mimetype
         data.name = rand_filename
         self.bot.send_photo(msg.chat.id, photo=data)
Exemple #29
0
 def get_sample_asset(self, name, asset_type='text'):
     """
     Returns an in-memory file of the specified type with the given name for testing
     """
     if asset_type == 'text':
         sample_asset = BytesIO(name)
         sample_asset.name = '{name}.txt'.format(name=name)
     elif asset_type == 'image':
         image = Image.new("RGB", size=(50, 50), color=(256, 0, 0))
         sample_asset = BytesIO()
         image.save(unicode(sample_asset), 'jpeg')
         sample_asset.name = '{name}.jpg'.format(name=name)
         sample_asset.seek(0)
     elif asset_type == 'opendoc':
         sample_asset = BytesIO(name)
         sample_asset.name = '{name}.odt'.format(name=name)
     return sample_asset
 def test_upload_file(self):
     # This is a 1x1 black png
     simple_png = BytesIO(
         b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDATx\x9cc````\x00\x00\x00\x05\x00\x01\xa5\xf6E@\x00\x00\x00\x00IEND\xaeB`\x82"
     )
     simple_png.name = "test.png"
     factory = APIRequestFactory()
     factory.post("/", data={"image": simple_png})
Exemple #31
0
 def get_file_from_image(image):
     bio = BytesIO()
     bio.name = "board.png"
     image.save(bio, 'PNG')
     bio.seek(0)
     return bio
 async def memorize(self, file_name=str):
     resp, _ = await self.req()
     photo = BytesIO(resp)
     photo.name = file_name + '.jpg'
     return photo
Exemple #33
0
    def take_video(self) -> Tuple[BytesIO, BytesIO, int, int]:
        def process_video_frame(frame_local):
            if self._flip_vertically or self._flip_horizontally:
                if self._hw_accel:
                    frame_loc_ = cv2.UMat(frame_local)
                    frame_loc_ = cv2.flip(frame_loc_, self._flip)
                    frame_local = cv2.UMat.get(frame_loc_)
                    del frame_loc_
                else:
                    frame_local = cv2.flip(frame_local, self._flip)
            # Todo: check memory leaks
            if self._rotate_code > -10:
                frame_local = cv2.rotate(frame_local,
                                         rotateCode=self._rotate_code)
            return frame_local

        def write_video():
            cv2.setNumThreads(self._threads)
            out = cv2.VideoWriter(
                filepath,
                fourcc=cv2.VideoWriter_fourcc(*self._fourcc),
                fps=fps_cam,
                frameSize=(width, height),
            )
            while video_lock.locked():
                try:
                    frame_local = frame_queue.get(block=False)
                except Exception as ex:
                    logger.warning("Reading video frames queue exception %s",
                                   ex)
                    frame_local = frame_queue.get()

                out.write(process_video_frame(frame_local))
                frame_local = None
                del frame_local

            while not frame_queue.empty():
                frame_local = frame_queue.get()
                out.write(process_video_frame(frame_local))
                frame_local = None
                del frame_local

            out.release()
            video_written_event.set()

        with self._camera_lock:
            cv2.setNumThreads(
                self._threads)  # TOdo: check self set and remove!
            self.cam_cam.open(self._host)
            self._set_cv2_params()
            success, frame = self.cam_cam.read()

            if not success:
                logger.debug("failed to get camera frame for video")
                # Todo: get picture from imgs?

            frame = process_video_frame(frame)
            height, width, channels = frame.shape
            thumb_bio = self._create_thumb(frame)
            del frame, channels
            fps_cam = self.cam_cam.get(
                cv2.CAP_PROP_FPS
            ) if self._stream_fps == 0 else self._stream_fps

            filepath = os.path.join("/tmp/", "video.mp4")
            frame_queue: Queue = Queue(fps_cam * self._video_buffer_size)
            video_lock = threading.Lock()
            video_written_event = threading.Event()
            video_written_event.clear()
            with video_lock:
                threading.Thread(target=write_video, args=()).start()
                t_end = time.time() + self._video_duration
                while success and time.time() <= t_end:
                    success, frame_loc = self.cam_cam.read()
                    try:
                        frame_queue.put(frame_loc, block=False)
                    except Exception as ex:
                        logger.warning(
                            "Writing video frames queue exception %s",
                            ex.with_traceback)
                        frame_queue.put(frame_loc)
                    frame_loc = None
                    del frame_loc
            video_written_event.wait()

        self.cam_cam.release()
        video_bio = BytesIO()
        video_bio.name = "video.mp4"
        with open(filepath, "rb") as video_file:
            video_bio.write(video_file.read())
        os.remove(filepath)
        video_bio.seek(0)
        return video_bio, thumb_bio, width, height
Exemple #34
0
    def _create_timelapse(
            self, printing_filename: str, gcode_name: str,
            info_mess: Message) -> Tuple[BytesIO, BytesIO, int, int, str, str]:
        if not printing_filename:
            raise ValueError("Gcode file name is empty")

        while self.light_need_off:
            time.sleep(1)

        lapse_dir = f"{self._base_dir}/{printing_filename}"

        lock_file = Path(f"{lapse_dir}/lapse.lock")
        if not lock_file.is_file():
            lock_file.touch()

        # Todo: check for nonempty photos!
        photos = glob.glob(f"{glob.escape(lapse_dir)}/*.{self._img_extension}")
        photos.sort(key=os.path.getmtime)
        photo_count = len(photos)

        if photo_count == 0:
            raise ValueError(
                f"Empty photos list for {printing_filename} in lapse path {lapse_dir}"
            )

        info_mess.edit_text(text="Creating thumbnail")
        last_photo = photos[-1]
        img = cv2.imread(last_photo)
        height, width, layers = img.shape
        thumb_bio = self._create_thumb(img)

        video_filename = Path(printing_filename).name
        video_filepath = f"{lapse_dir}/{video_filename}.mp4"
        if Path(video_filepath).is_file():
            os.remove(video_filepath)

        lapse_fps = self._calculate_fps(photo_count)

        with self._camera_lock:
            cv2.setNumThreads(
                self._threads)  # TOdo: check self set and remove!
            out = cv2.VideoWriter(
                video_filepath,
                fourcc=cv2.VideoWriter_fourcc(*self._fourcc),
                fps=lapse_fps,
                frameSize=(width, height),
            )

            info_mess.edit_text(text="Images recoding")
            last_update_time = time.time()
            for fnum, filename in enumerate(photos):
                if time.time() >= last_update_time + 3:
                    info_mess.edit_text(
                        text=f"Images recoded {fnum}/{photo_count}")
                    last_update_time = time.time()

                out.write(cv2.imread(filename))

            info_mess.edit_text(
                text=
                f"Repeating last image for {self._last_frame_duration} seconds"
            )
            for _ in range(lapse_fps * self._last_frame_duration):
                out.write(img)

            out.release()
            cv2.destroyAllWindows()
            del out

        del photos, img, layers

        # Todo: some error handling?

        video_bio = BytesIO()
        video_bio.name = f"{video_filename}.mp4"
        target_video_file = f"{self._ready_dir}/{printing_filename}.mp4"
        with open(video_filepath, "rb") as fh:
            video_bio.write(fh.read())
        if self._ready_dir and os.path.isdir(self._ready_dir):
            info_mess.edit_text(text="Copy lapse to target ditectory")
            Path(target_video_file).parent.mkdir(parents=True, exist_ok=True)
            with open(target_video_file, "wb") as cpf:
                cpf.write(video_bio.getvalue())
        video_bio.seek(0)

        os.remove(f"{lapse_dir}/lapse.lock")

        return video_bio, thumb_bio, width, height, video_filepath, gcode_name
Exemple #35
0
    def to_python(self, data):
        """Check that the file-upload field data contains a valid image (GIF,
        JPG, PNG, etc. -- whatever Pillow supports).

        Vendored from django.forms.fields.ImageField to add EXIF data
        removal. Can't use super() because we need to patch in the
        .png.fp object for some unholy (and possibly buggy) reason.
        """
        f = super().to_python(data)
        if f is None:
            return None

        # We need to get a file object for Pillow. We might have a path or we might
        # have to read the data into memory.
        if hasattr(data, "temporary_file_path"):
            with open(data.temporary_file_path(), "rb") as temp_fp:
                file = BytesIO(temp_fp.read())
        else:
            if hasattr(data, "read"):
                file = BytesIO(data.read())
            else:
                file = BytesIO(data["content"])

        try:
            # load() could spot a truncated JPEG, but it loads the entire
            # image in memory, which is a DoS vector. See #3848 and #18520.
            image = Image.open(file)
            # verify() must be called immediately after the constructor.
            image.verify()

            # Annotating so subclasses can reuse it for their own validation
            f.image = image
            # Pillow doesn't detect the MIME type of all formats. In those
            # cases, content_type will be None.
            f.content_type = Image.MIME.get(image.format)
        except Exception as exc:
            # Pillow doesn't recognize it as an image.
            raise ValidationError(
                _("Upload a valid image. The file you uploaded was either not an "
                  "image or a corrupted image.")) from exc
        if hasattr(f, "seek") and callable(f.seek):
            f.seek(0)

        stream = BytesIO()

        extension = ".jpg"
        if image.mode == "RGBA":
            extension = ".png"
        elif image.mode != "RGB":
            image = image.convert("RGB")

        stream.name = Path(data.name).stem + extension
        image.fp = file
        if hasattr(image, "png"):  # Yeah, idk what's up with this
            image.png.fp = file
        image_data = image.getdata()
        image_without_exif = Image.new(image.mode, image.size)
        image_without_exif.putdata(image_data)
        if self.max_height and self.max_width:
            image_without_exif.thumbnail((self.max_width, self.max_height))
        image_without_exif.save(
            stream, quality="web_high" if extension == ".jpg" else 95)
        stream.seek(0)
        return File(stream, name=data.name)
Exemple #36
0
async def sticker(context):
    """ Fetches images/stickers and add them to your pack. """
    user = await bot.get_me()
    if not user.username:
        user.username = user.first_name
    message = await context.get_reply_message()
    custom_emoji = False
    animated = False
    emoji = ""
    await context.edit("收集图像/贴纸中 . . .")
    if message and message.media:
        if isinstance(message.media, MessageMediaPhoto):
            photo = BytesIO()
            photo = await bot.download_media(message.photo, photo)
        elif "image" in message.media.document.mime_type.split('/'):
            photo = BytesIO()
            await context.edit("下载图片中 . . .")
            await bot.download_file(message.media.document, photo)
            if (DocumentAttributeFilename(file_name='sticker.webp')
                    in message.media.document.attributes):
                emoji = message.media.document.attributes[1].alt
                custom_emoji = True
        elif (DocumentAttributeFilename(file_name='AnimatedSticker.tgs')
              in message.media.document.attributes):
            photo = BytesIO()
            await bot.download_file(message.media.document,
                                    "AnimatedSticker.tgs")
            for index in range(len(message.media.document.attributes)):
                try:
                    emoji = message.media.document.attributes[index].alt
                    break
                except:
                    pass
            custom_emoji = True
            animated = True
            photo = 1
        else:
            await context.edit("`出错了呜呜呜 ~ 不支持此文件类型。`")
            return
    else:
        await context.edit("`出错了呜呜呜 ~ 请回复带有图片/贴纸的消息。`")
        return

    if photo:
        split_strings = context.text.split()
        if not custom_emoji:
            emoji = "👀"
        pack = 1
        sticker_already = False
        if len(split_strings) == 3:
            pack = split_strings[2]
            emoji = split_strings[1]
        elif len(split_strings) == 2:
            if split_strings[1].isnumeric():
                pack = int(split_strings[1])
            else:
                emoji = split_strings[1]

        pack_name = f"{user.username}_{pack}"
        pack_title = f"@{user.username} 的私藏 ({pack})"
        command = '/newpack'
        file = BytesIO()

        if not animated:
            await context.edit("调整图像大小中 . . .")
            image = await resize_image(photo)
            file.name = "sticker.png"
            image.save(file, "PNG")
        else:
            pack_name += "_animated"
            pack_title += " (animated)"
            command = '/newanimated'

        response = request.urlopen(
            request.Request(f'http://t.me/addstickers/{pack_name}'),
            context=ssl.create_default_context(cafile=certifi.where()))
        if not response.status == 200:
            await context.edit("连接到 Telegram 服务器失败 . . .")
            return
        http_response = response.read().decode("utf8").split('\n')

        if "  A <strong>Telegram</strong> user has created the <strong>Sticker&nbsp;Set</strong>." not in \
                http_response:
            for _ in range(20):  # 最多重试20次
                try:
                    async with bot.conversation('Stickers') as conversation:
                        await conversation.send_message('/addsticker')
                        await conversation.get_response()
                        await bot.send_read_acknowledge(conversation.chat_id)
                        await conversation.send_message(pack_name)
                        chat_response = await conversation.get_response()
                        while chat_response.text == "Whoa! That's probably enough stickers for one pack, give it a break. \
A pack can't have more than 120 stickers at the moment.":
                            pack += 1
                            pack_name = f"{user.username}_{pack}"
                            pack_title = f"@{user.username} 的私藏 ({pack})"
                            await context.edit("切换到私藏 " + str(pack) +
                                               " 上一个贴纸包已满 . . .")
                            await conversation.send_message(pack_name)
                            chat_response = await conversation.get_response()
                            if chat_response.text == "Invalid pack selected.":
                                await add_sticker(conversation, command,
                                                  pack_title, pack_name,
                                                  animated, message, context,
                                                  file, emoji)
                                await context.edit(
                                    f"这张图片/贴纸已经被添加到 [这个](t.me/addstickers/{pack_name}) 贴纸包。",
                                    parse_mode='md')
                                return
                        await upload_sticker(animated, message, context, file,
                                             conversation)
                        await conversation.get_response()
                        await conversation.send_message(emoji)
                        await bot.send_read_acknowledge(conversation.chat_id)
                        await conversation.get_response()
                        await conversation.send_message('/done')
                        await conversation.get_response()
                        await bot.send_read_acknowledge(conversation.chat_id)
                        break
                except AlreadyInConversationError:
                    if not sticker_already:
                        await context.edit("另一个命令正在添加贴纸, 重新尝试中")
                        sticker_already = True
                    else:
                        pass
                    await sleep(.5)
                except Exception:
                    raise
        else:
            await context.edit("贴纸包不存在,正在创建 . . .")
            async with bot.conversation('Stickers') as conversation:
                await add_sticker(conversation, command, pack_title, pack_name,
                                  animated, message, context, file, emoji)

        notification = await context.edit(
            f"这张图片/贴纸已经被添加到 [这个](t.me/addstickers/{pack_name}) 贴纸包。",
            parse_mode='md')
        await sleep(5)
        try:
            await notification.delete()
        except:
            pass
Exemple #37
0
 def get_cover(self):
     response = requests.get(self.get_image_link(0),
                             verify=ApiConfig.verify_ssl)
     file = BytesIO(response.content)
     file.name = "cover.jpg"
     return file
Exemple #38
0
    def make_full_profile(self, avatar_data, user, xp, nxp, lvl, minone, elo, ldb, desc, bg=None):
        img = Image.new("RGBA", (340, 390), (17, 17, 17, 255))
        if bg is not None:
            bg_width, bg_height = bg.size
            ratio = bg_height / 390
            bg = bg.resize((int(bg_width / (ratio)), int(bg_height / ratio)))
            if bg.size[0] < 340:
                ratio = bg_width / 340
                bg = bg.resize((int(bg_width / (ratio)), int(bg_height / ratio)))
            bg = bg.convert("RGBA")
            bg.putalpha(128)
            offset = 0
            if bg.size[0] >= 340:
                offset = (int((-(bg.size[0] - 340) / 2)), 0)
            if bg.size[0] < 340:
                offset = (0, int((-(bg.size[1] - 390) / 2)))

            img.paste(bg, offset, bg)
        img = self.add_corners(img, 10)
        draw = ImageDraw.Draw(img)
        usercolor = (255, 255, 0)  # user.color.to_rgb()
        aviholder = self.add_corners(Image.new("RGBA", (140, 140), (255, 255, 255, 255)), 10)
        nameplate = self.add_corners(Image.new("RGBA", (180, 60), (0, 0, 0, 255)), 10)
        xptot = self.add_corners(Image.new("RGBA", (310, 20), (215, 215, 215, 255)), 10)
        img.paste(aviholder, (10, 10), aviholder)
        img.paste(nameplate, (155, 10), nameplate)
        img.paste(xptot, (15, 340), xptot)

        fontpath = str(bundled_data_path(self) / "cambria.ttc")

        font1 = ImageFont.truetype(fontpath, 18)
        font2 = ImageFont.truetype(fontpath, 22)
        font3 = ImageFont.truetype(fontpath, 32)

        avatar = Image.open(avatar_data)
        avatar_size = 130, 130
        avatar.thumbnail(avatar_size)
        img.paste(avatar, (15, 15))
        lxp = xp - minone
        lnxp = nxp - minone
        lprc = ceil(lxp / (lnxp / 100))
        b_offset = floor(lprc * 3.1)
        xpbar = self.add_corners(Image.new("RGBA", (b_offset, 20), usercolor), 10)
        img.paste(xpbar, (12, 340), xpbar)

        lvl_str = _("Level:")
        ldb_str = _("Ranking:")
        rank_str = _("Role:")
        prog_str = _("Progress:")

        draw.text((10, 180), lvl_str, fill="white", font=font3)
        draw.text((10, 220), ldb_str, fill="white", font=font3)
        draw.text((10, 260), rank_str, fill="white", font=font3)
        nick = user.display_name
        if font2.getsize(nick)[0] > 150:
            nick = nick[:15] + "..."

        draw.text((154, 316), f"{lprc}%", fill=usercolor, font=font1)
        draw.text((100, 360), (prog_str + f" {xp}/{nxp}"), fill=usercolor, font=font1)
        draw.text(((font3.getsize(lvl_str)[0] + 20), 180), f"{lvl}", fill=usercolor, font=font3)
        draw.text(((font3.getsize(ldb_str)[0] + 20), 220), f"{ldb}", fill=usercolor, font=font3)
        draw.text(((font3.getsize(rank_str)[0] + 20), 260), f"{elo}", fill=usercolor, font=font3)

        draw.text((162, 14), f"{nick}", fill=usercolor, font=font2)
        draw.text((162, 40), f"{user.name}#{user.discriminator}", fill=usercolor, font=font1)
        margin = 162
        offset = 70
        count = 0
        for line in textwrap.wrap(desc, width=20):
            count += 1
            if count == 6:
                draw.text((margin, offset), f"{line}...", fill=usercolor, font=font1)
                break
            draw.text((margin, offset), f"{line}", fill=usercolor, font=font1)
            offset += font1.getsize(line)[1]
        temp = BytesIO()
        img.save(temp, format="PNG")
        temp.name = "profile.png"
        return temp
Exemple #39
0
def givefile(filename, content):
    """returns a file dummy object with the given content"""
    file = BytesIO(content)
    file.name = filename
    return file
Exemple #40
0
def bot_voice_message(VoiceMessage):
    bio = BytesIO(VoiceMessage)
    bio.name = 'voicemail.wav'
    bot.send_voice(chat_id=telegram_settings["chat_id"], voice=bio)
Exemple #41
0
    def do(self):
        ############################################################
        # Imports.
        script = self.script
        from mayavi.sources.array_source import ArraySource
        from mayavi.modules.outline import Outline
        from mayavi.modules.glyph import Glyph
        from mayavi.modules.vector_cut_plane import VectorCutPlane

        ############################################################
        # Create a new scene and set up the visualization.
        s = self.new_scene()

        d = ArraySource()
        sc, vec = self.make_data()
        d.origin = (-5, -5, -5)
        d.scalar_data = sc
        d.vector_data = vec

        script.add_source(d)

        # Create an outline for the data.
        o = Outline()
        script.add_module(o)
        # Glyphs for the scalars
        g = Glyph()
        script.add_module(g)
        g.glyph.glyph_source.glyph_position = 'center'
        g.glyph.glyph.vector_mode = 'use_normal'
        g.glyph.glyph.scale_factor = 0.5
        g.actor.property.line_width = 1.0

        v = VectorCutPlane()
        glyph = v.glyph
        gs = glyph.glyph_source
        gs.glyph_position = 'tail'
        gs.glyph_source = gs.glyph_list[1]
        script.add_module(v)
        v.implicit_plane.trait_set(normal=(0, 1, 0), origin=(0, 3, 0))

        v = VectorCutPlane()
        glyph = v.glyph
        gs = glyph.glyph_source
        gs.glyph_source = gs.glyph_list[2]
        gs.glyph_position = 'head'
        script.add_module(v)
        v.implicit_plane.trait_set(normal=(0, 1, 0), origin=(0, -2, 0))

        # Set the scene to a suitable view.
        self.set_view(s)

        self.check()

        ############################################################
        # Test if the modules respond correctly when the components
        # are changed.

        g.actor = g.actor.__class__()
        glyph = g.glyph
        g.glyph = glyph.__class__()
        g.glyph = glyph

        glyph = v.glyph
        v.glyph = glyph.__class__()
        v.glyph = glyph
        v.actor = v.actor.__class__()
        v.cutter = v.cutter.__class__()
        ip = v.implicit_plane
        v.implicit_plane = ip.__class__()
        v.implicit_plane = ip

        s.render()

        self.check()

        ############################################################
        # Test if saving a visualization and restoring it works.

        # Save visualization.
        f = BytesIO()
        f.name = abspath('test.mv2')  # We simulate a file.
        script.save_visualization(f)
        f.seek(0)  # So we can read this saved data.

        # Remove existing scene.
        engine = script.engine
        engine.close_scene(s)

        # Load visualization
        script.load_visualization(f)
        s = engine.current_scene
        # Set the scene to a suitable view.
        self.set_view(s)

        self.check()

        ############################################################
        # Test if the MayaVi2 visualization can be deepcopied.

        # Pop the source object.
        sources = s.children
        s.children = []
        # Add it back to see if that works without error.
        s.children.extend(sources)

        self.set_view(s)

        self.check()

        # Now deepcopy the source and replace the existing one with
        # the copy.  This basically simulates cutting/copying the
        # object from the UI via the right-click menu on the tree
        # view, and pasting the copy back.
        sources1 = copy.deepcopy(sources)
        s.children[:] = sources

        self.set_view(s)
        self.check()
Exemple #42
0
 def upload_asset(self, name="asset-1"):
     f = BytesIO(name)
     f.name = name + ".txt"
     return self.client.post(self.url, {"name": name, "file": f})
Exemple #43
0
    def get_bracket(self):
        """
        Creates playoff bracket image for current season
        """
        try:
            playoffs = self.get_playoffs()
            if "rounds" not in playoffs or not playoffs["rounds"]:
                logger.warning(f"No playoffs available for season {self.season}")
                return
            series = [
                serie
                for round in playoffs["rounds"]
                for serie in round["series"]
                if "matchupTeams" in serie and serie["round"]["number"] > 0
            ]
            bracket = {
                serie["seriesCode"]: self._get_matchup_info(serie) for serie in series
            }

            # Locations for logos in bracket
            # Round 1
            if "A" in bracket.keys():
                bracket["A"]["location"] = {"top": (1175, 50), "bottom": (1175, 185)}
            if "B" in bracket.keys():
                bracket["B"]["location"] = {"top": (1175, 280), "bottom": (1175, 405)}
            if "C" in bracket.keys():
                bracket["C"]["location"] = {"top": (1175, 505), "bottom": (1175, 625)}
            if "D" in bracket.keys():
                bracket["D"]["location"] = {"top": (1175, 730), "bottom": (1175, 865)}
            if "E" in bracket.keys():
                bracket["E"]["location"] = {"top": (15, 505), "bottom": (15, 625)}
            if "F" in bracket.keys():
                bracket["F"]["location"] = {"top": (15, 730), "bottom": (15, 865)}
            if "G" in bracket.keys():
                bracket["G"]["location"] = {"top": (15, 50), "bottom": (15, 185)}
            if "H" in bracket.keys():
                bracket["H"]["location"] = {"top": (15, 280), "bottom": (15, 405)}

            # Round 2
            if "I" in bracket.keys():
                bracket["I"]["location"] = {"top": (1000, 350), "bottom": (1000, 120)}
                bracket["I"]["matchup"] = {
                    "top": bracket["B"]["winner"],
                    "bottom": bracket["A"]["winner"],
                }
            if "J" in bracket.keys():
                bracket["J"]["location"] = {"top": (1000, 800), "bottom": (1000, 570)}
                bracket["J"]["matchup"] = {
                    "top": bracket["D"]["winner"],
                    "bottom": bracket["C"]["winner"],
                }
            if "K" in bracket.keys():
                bracket["K"]["location"] = {"top": (185, 800), "bottom": (185, 570)}
                bracket["K"]["matchup"] = {
                    "top": bracket["F"]["winner"],
                    "bottom": bracket["E"]["winner"],
                }
            if "L" in bracket.keys():
                bracket["L"]["location"] = {"top": (185, 350), "bottom": (185, 120)}
                bracket["L"]["matchup"] = {
                    "top": bracket["H"]["winner"],
                    "bottom": bracket["G"]["winner"],
                }

            # Round 3, Conference/Semi finals
            if "M" in bracket.keys():
                bracket["M"]["location"] = {"top": (835, 235), "bottom": (835, 675)}
                bracket["M"]["matchup"] = {
                    "top": bracket["I"]["winner"],
                    "bottom": bracket["J"]["winner"],
                }
            if "N" in bracket.keys():
                bracket["N"]["location"] = {"top": (355, 235), "bottom": (355, 675)}
                bracket["N"]["matchup"] = {
                    "top": bracket["L"]["winner"],
                    "bottom": bracket["K"]["winner"],
                }

            # Round 4, Stanley cup final
            if "O" in bracket.keys():
                bracket["O"]["location"] = {"top": (665, 450), "bottom": (530, 450)}
                bracket["O"]["matchup"] = {
                    "top": bracket["M"]["winner"],
                    "bottom": bracket["N"]["winner"],
                }

            # Insert teams into bracket
            for key, value in bracket.items():
                self._insert_team_to_bracket(
                    value["matchup"]["top"], value["location"]["top"]
                )
                self._insert_team_to_bracket(
                    value["matchup"]["bottom"], value["location"]["bottom"]
                )

                if key == "O":
                    text_x_loc = 665
                    text_y_loc = 560
                else:
                    text_x_loc = value["location"]["top"][0] + 70
                    text_y_loc = int(
                        (
                            value["location"]["top"][1]
                            + value["location"]["bottom"][1]
                            + 90
                        )
                        / 2
                    )

                self._insert_status_to_bracket(
                    value["status"], (text_x_loc, text_y_loc)
                )

            # Create in-memory image
            file = BytesIO()
            self.BRACKET_IMG.save(file, "PNG")
            file.seek(0)
            if self.save_img:
                file.name = f"NHL{self.season}.png"
                img = Image.open(file)
                img.save(file.name)
            return file
        except Exception:
            logger.exception("Error getting playoff bracket")
    async def kangcmd(self, message):  # noqa: C901 # TODO: reduce complexity a LOT
        """Use in reply or with an attached media:
           .kang <pack name> [emojis]
           If pack is not matched the most recently created will be used instead"""
        args = utils.get_args(message)
        if len(args) != 1 and len(args) != 2:
            logger.debug("wrong args len(%s) or bad args(%s)", len(args), args)
            await message.edit(_("Provide a pack name and optionally emojis too"))
            return

        if not message.is_reply:
            if message.sticker or message.photo:
                logger.debug("user sent photo/sticker directly not reply")
                sticker = message
            else:
                logger.debug("user didnt send any sticker/photo or reply")
                async for sticker in message.client.iter_messages(message.to_id, 10):
                    if sticker.sticker or sticker.photo:
                        break  # Changes message into the right one
        else:
            sticker = await message.get_reply_message()
        if not (sticker.sticker or sticker.photo):
            await message.edit(_("Reply to a sticker or photo to nick it"))
            return
        logger.debug("user did send photo/sticker")
        if len(args) > 1:
            emojis = args[1]
        elif sticker.sticker:
            emojis = sticker.file.emoji
        else:
            emojis = self.config["DEFAULT_STICKER_EMOJI"]
        logger.debug(emojis)
        animated = sticker.file.mime_type == "application/x-tgsticker"
        try:
            img = BytesIO()
            await sticker.download_media(file=img)
            img.seek(0)
            logger.debug(img)
            if animated:
                async with self._lock:
                    conv = message.client.conversation("t.me/" + self.config["STICKERS_USERNAME"],
                                                       timeout=5, exclusive=True)
                    async with conv:
                        first = await conv.send_message("/cancel")
                        await conv.get_response()
                        await conv.send_message("/addsticker")
                        buttons = (await conv.get_response()).buttons
                        if buttons is not None:
                            logger.debug("there are buttons, good")
                            button = click_buttons(buttons, args[0])
                            await button.click()
                        else:
                            logger.warning("there's no buttons!")
                            await message.client.send_message("t.me/" + self.config["STICKERS_USERNAME"], "/cancel")
                            await message.edit("Something went wrong")
                            return
                        # We have sent the pack we wish to modify.
                        # Upload sticker
                        r0 = await conv.get_response()
                        if ".PSD" in r0.message:
                            logger.error("bad response from stickerbot 0")
                            logger.error(r0)
                            await message.edit(_("<code>That isn't an animated sticker pack</code>"))
                            msgs = []
                            async for msg in message.client.iter_messages(entity="t.me/"
                                                                          + self.config["STICKERS_USERNAME"],
                                                                          min_id=first.id, reverse=True):
                                msgs += [msg.id]
                            logger.debug(msgs)
                            await message.client.delete_messages("t.me/" + self.config["STICKERS_USERNAME"],
                                                                 msgs + [first])
                            return
                        uploaded = await message.client.upload_file(img, file_name="AnimatedSticker.tgs")
                        m1 = await conv.send_file(uploaded, force_document=True)
                        m2 = await conv.send_message(emojis)
                        await conv.send_message("/done")
                        # Block now so that we mark it all as read
                        await message.client.send_read_acknowledge(conv.chat_id)
                        r1 = await conv.get_response(m1)
                        r2 = await conv.get_response(m2)
                        if "/done" not in r2.message:
                            # That's an error
                            logger.error("Bad response from StickerBot 1")
                            logger.error(r0)
                            logger.error(r1)
                            logger.error(r2)
                            await message.edit(_("<code>Something went wrong internally!</code>"))
                            return
                    msgs = []
                    async for msg in message.client.iter_messages(entity="t.me/" + self.config["STICKERS_USERNAME"],
                                                                  min_id=first.id,
                                                                  reverse=True):
                        msgs += [msg.id]
                    logger.debug(msgs)
                    await message.client.delete_messages("t.me/" + self.config["STICKERS_USERNAME"], msgs + [first])
                if "emoji" in r2.message:
                    # The emoji(s) are invalid.
                    logger.error("Bad response from StickerBot 2")
                    logger.error(r2)
                    await message.edit(_("<code>Please provide valid emoji(s).</code>"))
                    return

            else:
                try:
                    thumb = BytesIO()
                    task = asyncio.ensure_future(utils.run_sync(resize_image, img, self.config["STICKER_SIZE"], thumb))
                    thumb.name = "sticker.png"
                    # The data is now in thumb.
                    # Lock access to @Stickers
                    async with self._lock:
                        # Without t.me/ there is ambiguity; Stickers could be a name,
                        # in which case the wrong entity could be returned
                        # TODO should this be translated?
                        conv = message.client.conversation("t.me/" + self.config["STICKERS_USERNAME"],
                                                           timeout=5, exclusive=True)
                        async with conv:
                            first = await conv.send_message("/cancel")
                            await conv.get_response()
                            await conv.send_message("/addsticker")
                            r0 = await conv.get_response()
                            buttons = r0.buttons
                            if buttons is not None:
                                logger.debug("there are buttons, good")
                                button = click_buttons(buttons, args[0])
                                m0 = await button.click()
                            elif "/newpack" in r0.message:
                                await message.edit("<code>Please create a pack first</code>")
                                return
                            else:
                                logger.warning("there's no buttons!")
                                m0 = await message.client.send_message("t.me/" + self.config["STICKERS_USERNAME"],
                                                                       "/cancel")
                                await message.edit("<code>Something went wrong</code>")
                                return
                            # We have sent the pack we wish to modify.
                            # Upload sticker
                            r0 = await conv.get_response()
                            if ".TGS" in r0.message:
                                logger.error("bad response from stickerbot 0")
                                logger.error(r0)
                                await message.edit(_("<code>That's an animated pack</code>"))
                                msgs = []
                                async for msg in message.client.iter_messages(entity="t.me/"
                                                                              + self.config["STICKERS_USERNAME"],
                                                                              min_id=first.id,
                                                                              reverse=True):
                                    msgs += [msg.id]
                                logger.debug(msgs)
                                await message.client.delete_messages("t.me/" + self.config["STICKERS_USERNAME"],
                                                                     msgs + [first])
                                return
                            if "120" in r0.message:
                                logger.error("bad response from stickerbot 0")
                                logger.error(r0)
                                await message.edit(_("<code>That pack is full. Delete some stickers or try making a "
                                                     "new pack.</code>"))
                                msgs = []
                                async for msg in message.client.iter_messages(entity="t.me/"
                                                                              + self.config["STICKERS_USERNAME"],
                                                                              min_id=first.id,
                                                                              reverse=True):
                                    if msg.id != m0.id:
                                        msgs += [msg.id]
                                logger.debug(msgs)
                                await message.client.delete_messages("t.me/" + self.config["STICKERS_USERNAME"],
                                                                     msgs + [first])
                                return
                            await task  # We can resize the thumbnail while the sticker bot is processing other data
                            thumb.seek(0)
                            m1 = await conv.send_file(thumb, allow_cache=False, force_document=True)
                            r1 = await conv.get_response(m1)
                            m2 = await conv.send_message(emojis)
                            r2 = await conv.get_response(m2)
                            if "/done" in r2.message:
                                await conv.send_message("/done")
                            else:
                                logger.error(r1)
                                logger.error(r2)
                                logger.error("Bad response from StickerBot 0")
                                await message.edit(_("<code>Something went wrong internally</code>"))
                            await message.client.send_read_acknowledge(conv.chat_id)
                            if "/done" not in r2.message:
                                # That's an error
                                logger.error("Bad response from StickerBot 1")
                                logger.error(r1)
                                logger.error(r2)
                                await message.edit(_("<code>Something went wrong internally!</code>"))
                                return
                            msgs = []
                            async for msg in message.client.iter_messages(entity="t.me/"
                                                                          + self.config["STICKERS_USERNAME"],
                                                                          min_id=first.id,
                                                                          reverse=True):
                                msgs += [msg.id]
                        logger.debug(msgs)
                        await message.client.delete_messages("t.me/" + self.config["STICKERS_USERNAME"], msgs + [first])
                        if "emoji" in r2.message:
                            # The emoji(s) are invalid.
                            logger.error("Bad response from StickerBot 2")
                            logger.error(r2)
                            await message.edit(_("<code>Please provide valid emoji(s).</code>"))
                            return
                finally:
                    thumb.close()
        finally:
            img.close()
        packurl = utils.escape_html(f"https://t.me/addstickers/{button.text}")
        await message.edit(_("<code>Sticker added to</code> <a href='{}'>pack</a><code>!</code>").format(packurl))
 def test_upload_file(self):
     # This is a 1x1 black png
     simple_png = BytesIO(b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDATx\x9cc````\x00\x00\x00\x05\x00\x01\xa5\xf6E@\x00\x00\x00\x00IEND\xaeB`\x82')
     simple_png.name = 'test.png'
     factory = APIRequestFactory()
     factory.post('/', data={'image': simple_png})
Exemple #46
0
def nc4_dataset_from_s3(s3_client,bucket_name,key,filename='file.nc4'):
    buff=BytesIO()
    s3_client.download_fileobj(bucket_name,key,buff)
    buff.name = filename
    buff.seek(0)
    return netCDF4.Dataset(filename,memory=buff.read(),diskless=True,mode='r')
 def get_sample_asset(self, name, extension=".txt"):
     """Returns an in-memory file with the given name for testing"""
     f = BytesIO(name)
     f.name = name + extension
     return f
Exemple #48
0
	def __init__(self, file=None, res_name_or_index=None,
			sfntVersion="\000\001\000\000", flavor=None, checkChecksums=0,
			verbose=None, recalcBBoxes=True, allowVID=False, ignoreDecompileErrors=False,
			recalcTimestamp=True, fontNumber=-1, lazy=None, quiet=None,
			_tableCache=None):

		"""The constructor can be called with a few different arguments.
		When reading a font from disk, 'file' should be either a pathname
		pointing to a file, or a readable file object.

		It we're running on a Macintosh, 'res_name_or_index' maybe an sfnt
		resource name or an sfnt resource index number or zero. The latter
		case will cause TTLib to autodetect whether the file is a flat file
		or a suitcase. (If it's a suitcase, only the first 'sfnt' resource
		will be read!)

		The 'checkChecksums' argument is used to specify how sfnt
		checksums are treated upon reading a file from disk:
			0: don't check (default)
			1: check, print warnings if a wrong checksum is found
			2: check, raise an exception if a wrong checksum is found.

		The TTFont constructor can also be called without a 'file'
		argument: this is the way to create a new empty font.
		In this case you can optionally supply the 'sfntVersion' argument,
		and a 'flavor' which can be None, 'woff', or 'woff2'.

		If the recalcBBoxes argument is false, a number of things will *not*
		be recalculated upon save/compile:
			1) 'glyf' glyph bounding boxes
			2) 'CFF ' font bounding box
			3) 'head' font bounding box
			4) 'hhea' min/max values
			5) 'vhea' min/max values
		(1) is needed for certain kinds of CJK fonts (ask Werner Lemberg ;-).
		Additionally, upon importing an TTX file, this option cause glyphs
		to be compiled right away. This should reduce memory consumption
		greatly, and therefore should have some impact on the time needed
		to parse/compile large fonts.

		If the recalcTimestamp argument is false, the modified timestamp in the
		'head' table will *not* be recalculated upon save/compile.

		If the allowVID argument is set to true, then virtual GID's are
		supported. Asking for a glyph ID with a glyph name or GID that is not in
		the font will return a virtual GID.   This is valid for GSUB and cmap
		tables. For SING glyphlets, the cmap table is used to specify Unicode
		values for virtual GI's used in GSUB/GPOS rules. If the gid N is requested
		and does not exist in the font, or the glyphname has the form glyphN
		and does not exist in the font, then N is used as the virtual GID.
		Else, the first virtual GID is assigned as 0x1000 -1; for subsequent new
		virtual GIDs, the next is one less than the previous.

		If ignoreDecompileErrors is set to True, exceptions raised in
		individual tables during decompilation will be ignored, falling
		back to the DefaultTable implementation, which simply keeps the
		binary data.

		If lazy is set to True, many data structures are loaded lazily, upon
		access only.  If it is set to False, many data structures are loaded
		immediately.  The default is lazy=None which is somewhere in between.
		"""

		for name in ("verbose", "quiet"):
			val = locals().get(name)
			if val is not None:
				deprecateArgument(name, "configure logging instead")
			setattr(self, name, val)

		self.lazy = lazy
		self.recalcBBoxes = recalcBBoxes
		self.recalcTimestamp = recalcTimestamp
		self.tables = {}
		self.reader = None

		# Permit the user to reference glyphs that are not int the font.
		self.last_vid = 0xFFFE # Can't make it be 0xFFFF, as the world is full unsigned short integer counters that get incremented after the last seen GID value.
		self.reverseVIDDict = {}
		self.VIDDict = {}
		self.allowVID = allowVID
		self.ignoreDecompileErrors = ignoreDecompileErrors

		if not file:
			self.sfntVersion = sfntVersion
			self.flavor = flavor
			self.flavorData = None
			return
		if not hasattr(file, "read"):
			closeStream = True
			# assume file is a string
			if res_name_or_index is not None:
				# see if it contains 'sfnt' resources in the resource or data fork
				from . import macUtils
				if res_name_or_index == 0:
					if macUtils.getSFNTResIndices(file):
						# get the first available sfnt font.
						file = macUtils.SFNTResourceReader(file, 1)
					else:
						file = open(file, "rb")
				else:
					file = macUtils.SFNTResourceReader(file, res_name_or_index)
			else:
				file = open(file, "rb")
		else:
			# assume "file" is a readable file object
			closeStream = False
			file.seek(0)

		if not self.lazy:
			# read input file in memory and wrap a stream around it to allow overwriting
			file.seek(0)
			tmp = BytesIO(file.read())
			if hasattr(file, 'name'):
				# save reference to input file name
				tmp.name = file.name
			if closeStream:
				file.close()
			file = tmp
		self._tableCache = _tableCache
		self.reader = SFNTReader(file, checkChecksums, fontNumber=fontNumber)
		self.sfntVersion = self.reader.sfntVersion
		self.flavor = self.reader.flavor
		self.flavorData = self.reader.flavorData
Exemple #49
0
    def _gen_img(self, player, show_unhit):
        """
		Creates a visualization of the board.
		Returns a bytes image of the board.
		
		Params:
		player = int, Which player's board to print.
		show_unhit = int, Should unhit ships be shown.
		"""
        path = bundled_data_path(self.cog)
        img = Image.open(path / 'board.png')
        hit = Image.open(path / 'hit.png')
        miss = Image.open(path / 'miss.png')
        ships = [[
            Image.open(path / 'len5.png'),
            Image.open(path / 'len4.png'),
            Image.open(path / 'len3.png'),
            Image.open(path / 'len3.png'),
            Image.open(path / 'len2.png')
        ],
                 [
                     Image.open(path / 'len5destroyed.png'),
                     Image.open(path / 'len4destroyed.png'),
                     Image.open(path / 'len3destroyed.png'),
                     Image.open(path / 'len3destroyed.png'),
                     Image.open(path / 'len2destroyed.png')
                 ]]

        #place ships
        for index, pos in enumerate(self.ship_pos[player]):
            x, y, d = pos
            if show_unhit and not all(self.key[player][index].values()
                                      ):  #show a non-damaged ship
                if d == 'd':  #down
                    ships[0][index] = ships[0][index].rotate(90, expand=True)
                img.paste(ships[0][index],
                          box=((x * 30) + 32, (y * 30) + 32),
                          mask=ships[0][index])
            elif all(self.key[player][index].values()):  #show a damaged ship
                if d == 'd':  #down
                    ships[1][index] = ships[1][index].rotate(90, expand=True)
                img.paste(ships[1][index],
                          box=((x * 30) + 32, (y * 30) + 32),
                          mask=ships[1][index])

        #place hit/miss markers
        for y in range(10):
            for x in range(10):
                if self.board[player][((y) * 10) + x] == 1:  #miss
                    img.paste(miss,
                              box=((x * 30) + 32, (y * 30) + 32),
                              mask=miss)
                elif self.board[player][((y) * 10) + x] == 2:  #hit
                    img.paste(hit,
                              box=((x * 30) + 32, (y * 30) + 32),
                              mask=hit)

        temp = BytesIO()
        temp.name = 'board.png'
        img.save(temp)
        temp.seek(0)
        return temp
Exemple #50
0
    def upload_predictions(self,
                           file_path: str = "predictions.csv",
                           tournament: int = 8,
                           model_id: str = None,
                           df: pd.DataFrame = None) -> str:
        """Upload predictions from file.
        Will read TRIGGER_ID from the environment if this model is enabled with
        a Numerai Compute cluster setup by Numerai CLI.

        Args:
            file_path (str): CSV file with predictions that will get uploaded
            tournament (int): ID of the tournament (optional, defaults to 8)
                -- DEPRECATED there is only one tournament nowadays
            model_id (str): Target model UUID (required for accounts with
                multiple models)
            df (pandas.DataFrame): pandas DataFrame to upload, if function is
                given df and file_path, df will be uploaded.

        Returns:
            str: submission_id

        Example:
            >>> api = NumerAPI(secret_key="..", public_id="..")
            >>> model_id = api.get_models()['uuazed']
            >>> api.upload_predictions("prediction.cvs", model_id=model_id)
            '93c46857-fed9-4594-981e-82db2b358daf'
            >>> # upload from pandas DataFrame directly:
            >>> api.upload_predictions(df=predictions_df, model_id=model_id)
        """
        self.logger.info("uploading predictions...")

        # write the pandas DataFrame as a binary buffer if provided
        buffer_csv = None

        if df is not None:
            buffer_csv = BytesIO(df.to_csv(index=False).encode())
            buffer_csv.name = file_path

        auth_query = '''
            query($filename: String!
                  $tournament: Int!
                  $modelId: String) {
                submission_upload_auth(filename: $filename
                                       tournament: $tournament
                                       modelId: $modelId) {
                    filename
                    url
                }
            }
            '''
        arguments = {
            'filename': os.path.basename(file_path),
            'tournament': tournament,
            'modelId': model_id
        }
        submission_resp = self.raw_query(auth_query,
                                         arguments,
                                         authorization=True)
        submission_auth = submission_resp['data']['submission_upload_auth']

        # get compute id if available and pass it along
        headers = {"x_compute_id": os.getenv("NUMERAI_COMPUTE_ID")}
        with open(file_path, 'rb') if df is None else buffer_csv as fh:
            requests.put(submission_auth['url'],
                         data=fh.read(),
                         headers=headers)
        create_query = '''
            mutation($filename: String!
                     $tournament: Int!
                     $modelId: String
                     $triggerId: String) {
                create_submission(filename: $filename
                                  tournament: $tournament
                                  modelId: $modelId
                                  triggerId: $triggerId) {
                    id
                }
            }
            '''
        arguments = {
            'filename': submission_auth['filename'],
            'tournament': tournament,
            'modelId': model_id,
            'triggerId': os.getenv('TRIGGER_ID', None)
        }
        create = self.raw_query(create_query, arguments, authorization=True)
        submission_id = create['data']['create_submission']['id']
        return submission_id
Exemple #51
0
def send_image_to_user(bot, chat_id, image):
    bio = BytesIO()
    bio.name = 'masked.jpeg'
    image.save(bio, 'JPEG')
    bio.seek(0)
    bot.send_photo(chat_id, photo=bio)
Exemple #52
0
 def get_byte_object(self, link: str, name: str):
     buf = BytesIO(requests.get(self.base_url+link, headers=self.headers, cookies=self.cookies).content)
     buf.name = name + '.pdf'
     return buf
Exemple #53
0
def create_test_file(data, file_type):
    io = BytesIO()
    sheet = pe.Sheet(data)
    io = sheet.save_to_memory(file_type, io)
    io.name = 'test.' + file_type
    return io
Exemple #54
0
async def pic2sticker(context):
    """ Fetches images and send it as sticker. """
    pic_round = False
    if len(context.parameter) >= 1:
        pic_round = True

    if redis_status():
        if redis.get("sticker.round"):
            pic_round = True

    message = await context.get_reply_message()
    if message and message.media:
        if isinstance(message.media, MessageMediaPhoto):
            photo = BytesIO()
            photo = await bot.download_media(message.photo, photo)
        elif isinstance(message.media, MessageMediaWebPage):
            try:
                await context.edit(lang('sticker_type_not_support'))
            except:
                pass
            return
        elif "image" in message.media.document.mime_type.split('/'):
            photo = BytesIO()
            try:
                await context.edit(lang('sticker_downloading'))
            except:
                pass
            await bot.download_file(message.media.document, photo)
            if (DocumentAttributeFilename(file_name='sticker.webp') in
                    message.media.document.attributes):
                try:
                    await context.edit(lang('sticker_type_not_support'))
                except:
                    pass
                return
        else:
            try:
                await context.edit(lang('sticker_type_not_support'))
            except:
                pass
            return
    else:
        try:
            await context.edit(lang('sticker_reply_not_sticker'))
        except:
            pass
        return

    if photo:
        file = BytesIO()
        try:
            await context.edit(lang('sticker_resizing'))
        except:
            pass
        image = await resize_image(photo)
        if pic_round:
            try:
                await context.edit(lang('us_static_rounding'))
            except:
                pass
            image = await rounded_image(image)
        file.name = "sticker.webp"
        image.save(file, "WEBP")
        file.seek(0)
        try:
            await context.edit(lang('us_static_uploading'))
        except:
            pass
        await bot.send_file(context.chat_id, file, force_document=False)
        try:
            await context.delete()
        except:
            pass
Exemple #55
0
    def _zip_func(self, func_path, namespace, func_name):

        if not os.path.exists(func_path):
            raise ContextException(
                "Function file or path not found by CodeUri '{}'".format(
                    func_path))

        if self.deploy_namespace and self.deploy_namespace != namespace:
            namespace = self.deploy_namespace

        buff = BytesIO()

        zip_file_name = str(namespace) + '-' + str(func_name) + '-latest.zip'
        time_data = time.strftime("-%Y-%m-%d-%H-%M-%S",
                                  time.localtime(int(time.time())))
        zip_file_name_cos = str(namespace) + '-' + str(
            func_name) + '-latest' + time_data + '.zip'

        cwd = os.getcwd()
        os.chdir(self.template_file_dir)

        zip_file_path = os.path.join(_BUILD_DIR, zip_file_name)

        if os.path.exists(zip_file_path):
            os.remove(zip_file_path)

        try:

            try:
                os.mkdir(_BUILD_DIR)
            except:
                pass

            if os.path.isdir(func_path):
                os.chdir(func_path)
                with ZipFile(buff, mode='w',
                             compression=ZIP_DEFLATED) as zip_object:
                    for current_path, sub_folders, files_name in os.walk(
                            _CURRENT_DIR):
                        if not str(current_path).startswith("./.") and not str(
                                current_path).startswith(r".\."):
                            for file in files_name:
                                zip_object.write(
                                    os.path.join(current_path, file))

                os.chdir(cwd)
                buff.seek(0)
                buff.name = zip_file_name

                if not os.path.exists(_BUILD_DIR):
                    os.mkdir(_BUILD_DIR)

                # a temporary support for upload func from local zipfile
                with open(zip_file_path, 'wb') as f:
                    f.write(buff.read())
                    buff.seek(0)

            else:
                if str(func_path).endswith(".zip"):
                    with open(func_path, "rb") as f:
                        buff.write(f.read())
                else:
                    (filepath, filename) = os.path.split(func_path)
                    if filepath:
                        os.chdir(filepath)

                    with ZipFile(buff, mode='w',
                                 compression=ZIP_DEFLATED) as zip_object:
                        zip_object.write(filename)
                    os.chdir(cwd)

                buff.seek(0)
                buff.name = zip_file_name

            # a temporary support for upload func from local zipfile
            with open(zip_file_path, 'wb') as f:
                f.write(buff.read())
                buff.seek(0)

        except Exception as e:
            raise PackageException(
                "Package Error. Please check CodeUri in YAML.")

        Operation("Compress function '{}' to zipfile '{}' success".format(
            zip_file_path, zip_file_name)).success()
        return buff, zip_file_name, zip_file_name_cos
Exemple #56
0
async def sticker(context):
    """ Fetches images/stickers and add them to your pack. """
    user = await bot.get_me()
    if not user.username:
        user.username = user.first_name
    message = await context.get_reply_message()
    custom_emoji = False
    animated = False
    emoji = ""
    await context.edit("偷贴纸已进行20%")
    if message and message.media:
        if isinstance(message.media, MessageMediaPhoto):
            photo = BytesIO()
            photo = await bot.download_media(message.photo, photo)
        elif "image" in message.media.document.mime_type.split('/'):
            photo = BytesIO()
            await context.edit("偷贴纸已进行40%")
            await bot.download_file(message.media.document, photo)
            if (DocumentAttributeFilename(file_name='sticker.webp')
                    in message.media.document.attributes):
                emoji = message.media.document.attributes[1].alt
                custom_emoji = True
        elif (DocumentAttributeFilename(file_name='AnimatedSticker.tgs')
              in message.media.document.attributes):
            photo = BytesIO()
            await bot.download_file(message.media.document,
                                    "AnimatedSticker.tgs")
            for index in range(len(message.media.document.attributes)):
                try:
                    emoji = message.media.document.attributes[index].alt
                    break
                except:
                    pass
            custom_emoji = True
            animated = True
            photo = 1
        else:
            await context.edit("`err`")
            return
    else:
        await context.edit("`err`")
        return

    if photo:
        split_strings = context.text.split()
        if not custom_emoji:
            emoji = "👀"
        pack = 1
        if len(split_strings) == 3:
            pack = split_strings[2]
            emoji = split_strings[1]
        elif len(split_strings) == 2:
            if split_strings[1].isnumeric():
                pack = int(split_strings[1])
            else:
                emoji = split_strings[1]

        pack_name = f"{user.username}_{pack}"
        pack_title = f"@{user.username} 的私藏 ({pack})"
        command = '/newpack'
        file = BytesIO()

        if not animated:
            await context.edit("偷贴纸已进行60%")
            image = await resize_image(photo)
            file.name = "sticker.png"
            image.save(file, "PNG")
        else:
            pack_name += "_animated"
            pack_title += " (animated)"
            command = '/newanimated'

        response = request.urlopen(
            request.Request(f'http://t.me/addstickers/{pack_name}'))
        http_response = response.read().decode("utf8").split('\n')

        if "  A <strong>Telegram</strong> user has created the <strong>Sticker&nbsp;Set</strong>." not in \
                http_response:
            async with bot.conversation('Stickers') as conversation:
                await conversation.send_message('/addsticker')
                await conversation.get_response()
                await bot.send_read_acknowledge(conversation.chat_id)
                await conversation.send_message(pack_name)
                chat_response = await conversation.get_response()
                while chat_response.text == "Whoa! That's probably enough stickers for one pack, give it a break. \
A pack can't have more than 120 stickers at the moment.":
                    pack += 1
                    pack_name = f"{user.username}_{pack}"
                    pack_title = f"@{user.username} 的私藏 ({pack})"
                    await context.edit("偷贴纸已进行8" + str(pack) + "%")
                    await conversation.send_message(pack_name)
                    chat_response = await conversation.get_response()
                    if chat_response.text == "Invalid pack selected.":
                        await add_sticker(conversation, command, pack_title,
                                          pack_name, animated, message,
                                          context, file, emoji)
                        await context.edit(
                            f"偷贴纸已进行100%[꧁♛꧂](t.me/addstickers/{pack_name}) ",
                            parse_mode='md')
                        return
                await upload_sticker(animated, message, context, file,
                                     conversation)
                await conversation.get_response()
                await conversation.send_message(emoji)
                await bot.send_read_acknowledge(conversation.chat_id)
                await conversation.get_response()
                await conversation.send_message('/done')
                await conversation.get_response()
                await bot.send_read_acknowledge(conversation.chat_id)
        else:
            await context.edit("adding")
            async with bot.conversation('Stickers') as conversation:
                await add_sticker(conversation, command, pack_title, pack_name,
                                  animated, message, context, file, emoji)

        notification = await context.edit(
            f"偷贴纸已进行100%[꧁♛꧂](t.me/addstickers/{pack_name}) ", parse_mode='md')
        await sleep(3)
        try:
            await notification.delete()
        except:
            pass
async def f_load(message):
    clrs = {
        "red": 1,
        "lime": 2,
        "green": 3,
        "blue": 4,
        "cyan": 5,
        "brown": 6,
        "purple": 7,
        "pink": 8,
        "orange": 9,
        "yellow": 10,
        "white": 11,
        "black": 12,
    }
    clr = randint(1, 12)
    text = message.pattern_match.group(1)
    reply = await message.get_reply_message()
    if text in clrs:
        clr = clrs[text]
        text = None
    if not text:
        if not reply:
            await bruh(message, message.sender)
            return
        if not reply.text:
            await bruh(message, reply.sender)
            return
        text = reply.pattern_match.group(1)

    if text.split(" ")[0] in clrs:
        clr = clrs[text.split(" ")[0]]
        text = " ".join(text.split(" ")[1:])

    if text == "colors":
        await message.edit("Cores disponíveis:\n" +
                           ("\n".join([f"• `{i}`"
                                       for i in list(clrs.keys())])))
        return

    url = "https://raw.githubusercontent.com/KeyZenD/AmongUs/master/"
    font = ImageFont.truetype(BytesIO(get(url + "bold.ttf").content), 60)
    imposter = Image.open(BytesIO(get(f"{url}{clr}.png").content))
    text_ = "\n".join(["\n".join(wrap(part, 30)) for part in text.split("\n")])
    w, h = ImageDraw.Draw(Image.new("RGB",
                                    (1, 1))).multiline_textsize(text_,
                                                                font,
                                                                stroke_width=2)
    text = Image.new("RGBA", (w + 30, h + 30))
    ImageDraw.Draw(text).multiline_text((15, 15),
                                        text_,
                                        "#FFF",
                                        font,
                                        stroke_width=2,
                                        stroke_fill="#000")
    w = imposter.width + text.width + 10
    h = max(imposter.height, text.height)
    image = Image.new("RGBA", (w, h))
    image.paste(imposter, (0, h - imposter.height), imposter)
    image.paste(text, (w - text.width, 0), text)
    image.thumbnail((512, 512))
    output = BytesIO()
    output.name = "imposter.webp"
    image.save(output)
    output.seek(0)
    await message.delete()
    await message.client.send_file(message.to_id, output, reply_to=reply)
Exemple #58
0
def show(update, context):
    keyboard = [['/showanother', '/cancel']]
    reply_markup = ReplyKeyboardMarkup(keyboard,
                                       one_time_keyboard=True,
                                       resize_keyboard=True)
    global WEEKS
    WEEKS = update.message.text
    index = 0
    scheduleAndMemberList = []
    totalUnformatted = []
    for elem in LINKS:
        modulesAndSemesterAndUnformatted = timetableParser(elem)
        modulesAndSemester = modulesAndSemesterAndUnformatted[0]
        unformatted = modulesAndSemesterAndUnformatted[1]
        totalUnformatted.append(unformatted)
        userLessons = lessonsGenerator(modulesAndSemester)
        lessonsInWeek = dataStructureOrganizer(userLessons)[str(WEEKS)]
        scheduleAndMemberList.append(lessonsInWeek + "@" + str(index))
        index += 1

    chat_id = update.message.chat_id
    update.message.reply_text(
        "Here is week " + WEEKS +
        " schedule for your group, darker shade of red represents more people are unavailable, /showanother to see another week, or /cancel to restart\nPlease wait while your timetable is loading.",
        reply_markup=reply_markup)

    unformattedInList = []
    for userUnformatted in totalUnformatted:
        for unformatted in userUnformatted:
            if unformatted not in unformattedInList:
                unformattedInList.append(unformatted)
    if len(unformattedInList) != 0:
        unformattedInString = " ".join(unformattedInList)
        context.bot.send_message(
            chat_id=chat_id,
            text=
            "TAKE NOTE\nThe following module\s doesn't follow the standard 13 weeks per semester convention, and cannot be displayed: "
            + unformattedInString)

    earliestAndLatestTime = findEarliestAndLatestTime(scheduleAndMemberList)
    # timetableWithDays: 1 hr slot, timetableWhiteBlack: 2hr slot
    new_im = dynamicImager(earliestAndLatestTime, "timetable")
    imageTimestamp = dynamicImager(earliestAndLatestTime, "timestamp")
    timestampDrawer(imageTimestamp, earliestAndLatestTime)

    pixels = new_im.load()
    pixelDict = {}
    pixelMemberSet = set()

    for scheduleAndMember in scheduleAndMemberList:
        scheduleAndMemberArr = scheduleAndMember.split("@")
        schedule = scheduleAndMemberArr[0]
        member = scheduleAndMemberArr[1]
        tempList = schedule.split('+')
        for elem in tempList:
            # [0] = startTime-endTime, [1] = day
            auxList = elem.split(':')
            # [0] = startTime, [1] = endTime
            startEndTime = auxList[0].split('-')

            day = auxList[1]
            timetableStartTime = earliestAndLatestTime[0]
            pixelList = timeToPixelConverter(startEndTime, timetableStartTime,
                                             day)
            widthPixelLists = pixelList[0]
            heightPixelList = pixelList[1]

            for value in widthPixelLists:
                keyWithMember = str(value[0]) + ":" + str(value[1]) + ":" + \
                    str(heightPixelList[0]) + ":" + \
                    str(heightPixelList[1]) + ":" + member
                key = str(value[0]) + ":" + str(value[1]) + ":" + \
                    str(heightPixelList[0]) + ":" + str(heightPixelList[1])
                # if pixel belongs to same member, skip. Because it means the user has 2 mods in 1 timeslot
                if keyWithMember not in pixelMemberSet:
                    if key in pixelDict:
                        pixelDict[key] += 1
                    else:
                        pixelDict[key] = 1
                pixelMemberSet.add(keyWithMember)

    for key, value in pixelDict.items():
        widthAndHeight = key.split(":")
        fraction = round(value / int(MEMBERS), 1)
        if fraction == 0:  # items in this dict is guranteed to have > 1 member not free
            fraction = 0.1
        RGB = RGBshade[fraction]
        for i in range(int(widthAndHeight[0]), int(widthAndHeight[1])):
            for j in range(int(widthAndHeight[2]), int(widthAndHeight[3])):
                pixels[i, j] = RGB

    dst = Image.new('RGB',
                    (new_im.width, new_im.height + imageTimestamp.height - 60))
    dst.paste(imageTimestamp, (0, -60))
    dst.paste(new_im, (0, imageTimestamp.height - 60))

    bio = BytesIO()
    bio.name = 'image.png'
    dst.save(bio, 'png')
    bio.seek(0)

    context.bot.send_photo(chat_id=chat_id, photo=bio)
    return WEEKCOUNT
Exemple #59
0
    def do(self):
        ############################################################
        # Imports.
        from mayavi.modules.api import ScalarCutPlane
        from mayavi.modules.labels import Labels
        from mayavi.sources.vtk_xml_file_reader import VTKXMLFileReader

        ############################################################
        # Create a new scene and set up the visualization.
        s = self.new_scene()
        script = mayavi = self.script

        # Read a VTK (old style) data file.
        r = VTKXMLFileReader()
        r.initialize(get_example_data('fire_ug.vtu'))
        script.add_source(r)

        # Create the filters.
        cp = ScalarCutPlane()
        script.add_module(cp)
        l = Labels(object=cp)
        script.add_module(l)

        s.scene.isometric_view()
        GUI.process_events()
        self.check(saved=False)
        ############################################################
        # Test if saving a visualization and restoring it works.

        # Save visualization.
        f = BytesIO()
        f.name = abspath('test.mv2')  # We simulate a file.
        script.save_visualization(f)
        f.seek(0)

        # Remove existing scene.
        engine = script.engine
        engine.close_scene(s)

        # Load visualization
        script.load_visualization(f)
        s = engine.current_scene
        s.scene.isometric_view()

        # Seems to be needed for the test to pass. :(  Just flushes the
        # pipeline.
        s.children[0].pipeline_changed = True
        GUI.process_events()

        # Check.
        # Now do the check.
        self.check(saved=True)

        ############################################################
        # Test if the Mayavi2 visualization can be deep-copied.

        # Pop the source object.
        source = s.children.pop()
        # Add it back to see if that works without error.
        s.children.append(source)
        GUI.process_events()

        # Now do the check.
        s.scene.isometric_view()
        self.check(saved=True)

        # Now deepcopy the source and replace the existing one with
        # the copy.  This basically simulates cutting/copying the
        # object from the UI via the right-click menu on the tree
        # view, and pasting the copy back.
        source1 = copy.deepcopy(source)
        s.children[0] = source1
        GUI.process_events()

        # Now do the check.
        s.scene.isometric_view()
        self.check(saved=True)

        GUI.process_events()
Exemple #60
0
def h5_dataset_from_s3(s3_client,bucket_name,key,filename='file.h5'):
    buff=BytesIO()
    s3_client.download_fileobj(bucket_name,key,buff)
    buff.name = filename
    buff.seek(0)
    return h5py.File(buff,'r') # returns an hdf5 file object