예제 #1
0
async def main() -> None:
    """
    Generate all badges
    """
    access_token = os.getenv("ACCESS_TOKEN")
    if not access_token:
        # access_token = os.getenv("GITHUB_TOKEN")
        raise Exception("A personal access token is required to proceed!")
    user = os.getenv("GITHUB_ACTOR")
    exclude_repos = os.getenv("EXCLUDED")
    exclude_repos = ({x.strip()
                      for x in exclude_repos.split(",")}
                     if exclude_repos else None)
    exclude_langs = os.getenv("EXCLUDED_LANGS")
    exclude_langs = ({x.strip()
                      for x in exclude_langs.split(",")}
                     if exclude_langs else None)
    # Convert a truthy value to a Boolean
    ignore_forked_repos = os.getenv("EXCLUDE_FORKED_REPOS")
    ignore_forked_repos = (not not ignore_forked_repos
                           and ignore_forked_repos.strip().lower() != "false")
    async with aiohttp.ClientSession() as session:
        s = Stats(user,
                  access_token,
                  session,
                  exclude_repos=exclude_repos,
                  exclude_langs=exclude_langs,
                  ignore_forked_repos=ignore_forked_repos)
        await asyncio.gather(generate_languages(s), generate_overview(s),
                             generate_linkedin(s))

    hti = Html2Image(output_path='generated')
    hti.screenshot(other_file='generated/linkedin-banner.svg',
                   save_as='linkedin-banner.jpg',
                   size=(1584, 396))
예제 #2
0
def test_screenshot_url_sizes_missing_custom_names():
    hti = Html2Image(output_path=OUTPUT_PATH)

    test_sizes = [
        (100, 100),
        (100, 1000),
    ]

    paths = hti.screenshot(
        url=[
            'https://www.python.org',
            'https://www.wikipedia.org/',
            'https://www.example.org/',
        ],
        save_as=[
            "python_100_100.png",
            "wikipedia_100_1000.png",
            "example_100_1000.png",
        ],
        size=test_sizes,
    )

    for wanted_size, path in zip(test_sizes, paths):
        img = Image.open(path)
        assert wanted_size == img.size  # default size
예제 #3
0
    def make_heatmap(locations, filename='heatmap.html', overlay=''):
        startingLocation = [49.006239, 8.411572]
        hmap = folium.Map(location=startingLocation, zoom_start=14)

        # Creates a heatmap element
        hm_wide = HeatMap(locations, min_opacity=.5, radius=21, blur=25)

        # Adds the heatmap element to the map
        hmap.add_child(hm_wide)

        # Saves the map to heatmap.hmtl
        hmap.save(filename)
        hti = Html2Image()

        png_file = hti.screenshot(url=filename,
                                  save_as=os.path.basename(filename) +
                                  '.png')[0]
        shutil.move(png_file, filename + '.png')
        img = Image.open(filename + '.png')
        draw = ImageDraw.Draw(img)
        # font = ImageFont.truetype(<font-file>, <font-size>)
        font = ImageFont.truetype(
            "C:/Users/Simon/AppData/Local/Microsoft/Windows/Fonts/TTNorms-Bold.otf",
            100)
        # draw.text((x, y),"Sample Text",(r,g,b))
        draw.text((1400, 0), overlay, (0, 0, 0), font=font, align='right')
        img = img.resize([int(x * 0.5) for x in img.size])
        img.save(filename + '.png')
        return img
    def saveImage(frame, css, name):
        hti = Html2Image()
        hti.output_path = 'images/'
        html = frame.to_html()
        hti.screenshot(html_str=html, css_str=css, save_as=name + ".png")
        # TODO Crop png using PIL

        # VVVV Code for saving html as pdf
        """# make temporary html file under random name
예제 #5
0
def test_screenshot_multiple_urls():
    hti = Html2Image(output_path=OUTPUT_PATH)
    paths = hti.screenshot(
        url=['https://www.python.org', "https://www.example.org/"],
        save_as="mixed_urls.png",
    )

    for path in paths:
        img = Image.open(path)
        assert (1920, 1080) == img.size  # default size
예제 #6
0
def test_screenshot_url_custom_size():
    hti = Html2Image(output_path=OUTPUT_PATH)

    test_size = (334, 485)

    paths = hti.screenshot(
        url='https://www.python.org',
        save_as="pyorg_custom_size.png",
        size=test_size,
    )

    img = Image.open(paths[0])
    assert test_size == img.size  # default size
예제 #7
0
def convert_midi_to_image(url):
    file = requests.get(url)
    open('input.mid', 'wb').write(file.content)
    pm = PrettyMIDI("input.mid")
    preset = Preset(plot_width=1000, plot_height=500)
    plotter = Plotter(preset=preset)
    plotter.save(pm, 'imageHtml.html')
    hti = Html2Image()
    hti.screenshot(html_file='imageHtml.html',
                   save_as='image.png',
                   size=(1000, 500))
    return discord.File(
        r'image.png') if os.path.getsize('image.png') <= 16000000 else None
예제 #8
0
def test_screenshot_other_svg():
    hti = Html2Image(output_path=OUTPUT_PATH)

    paths = hti.screenshot(other_file='./examples/star.svg',
                           save_as="star_svg.png")

    img = Image.open(paths[0])
    pixels = img.load()

    assert (1920, 1080) == img.size  # default size

    # check colors at top left corner
    assert pixels[0, 0] == (0, 0, 0, 0)  # full transparency no color
예제 #9
0
def test_extend_size_param():
    hti = Html2Image(output_path=OUTPUT_PATH)

    assert hti._extend_size_param([(50, 50)], 1) \
        == [(50, 50)]

    assert hti._extend_size_param([(50, 50)], 3) \
        == [(50, 50), (50, 50), (50, 50)]

    assert hti._extend_size_param([(50, 50), (70, 60), (80, 90)], 5) \
        == [(50, 50), (70, 60), (80, 90), (80, 90), (80, 90)]

    assert hti._extend_size_param([], 3) \
        == [(1920, 1080), (1920, 1080), (1920, 1080)]
예제 #10
0
def test_extend_save_as_param():
    hti = Html2Image(output_path=OUTPUT_PATH)

    assert hti._extend_save_as_param(['a.png', 'b.png'], 2) == \
        ['a.png', 'b.png']

    assert hti._extend_save_as_param(['a.png', 'b.png'], 4) == \
        ['a.png', 'b_0.png', 'b_1.png', 'b_2.png']

    assert hti._extend_save_as_param(['a.png', 'b.png'], 0) == \
        ['a.png', 'b.png']

    assert hti._extend_save_as_param(['a.png', 'b.png', None, 65], 2) == \
        ['a.png', 'b.png']
예제 #11
0
def test_screensshot_file():
    hti = Html2Image(output_path=OUTPUT_PATH)

    paths = hti.screenshot(
        html_file="./examples/blue_page.html",
        css_file="./examples/blue_background.css",
        save_as="from_file.png",
    )

    img = Image.open(paths[0])
    pixels = img.load()

    assert (1920, 1080) == img.size  # default size

    # check colors at top left corner
    assert pixels[0, 0] == (0, 0, 255, 255)  # blue + no transparency
예제 #12
0
def test_screenshot_string():
    hti = Html2Image(output_path=OUTPUT_PATH)

    html = "Hello"
    css = "body{background: blue; font-size: 50px;}"

    paths = hti.screenshot(html_str=html,
                           css_str=css,
                           save_as="blue_big_hello.png")

    img = Image.open(paths[0])
    pixels = img.load()

    assert (1920, 1080) == img.size  # default size

    # check colors at top left corner
    assert pixels[0, 0] == (0, 0, 255, 255)  # blue + no transparency
예제 #13
0
def genearated_certificate_store_in_image(self, user_id):
    generated_certifices = GeneratedCertificate.objects.all()
    for gen_cert in generated_certifices:
        verify_uuid = gen_cert.verify_uuid
        if verify_uuid:
            log.info("verify uuid %s", verify_uuid)
            filename_org = verify_uuid + '.jpg'
            log.info("defindes html2image with path")
            hti = Html2Image(output_path=settings.MEDIA_ROOT)
            log.info("htp defineddddddd")
            certificate_url = settings.LMS_ROOT_URL + '/certificates/' + verify_uuid
            log.info("before image create")
            certificate_image = hti.screenshot(url=certificate_url,
                                               save_as=filename_org)
            log.info("after image create %s", certificate_image)
            bucket_name = BUCKET_NAME + '/user_accomplishments'
            with open(settings.MEDIA_ROOT + filename_org, 'rb') as model_excel:
                filename = model_excel
                log.info("file nameeeee --- %s", filename)
                conn = boto.connect_s3(AWS_ACCESS_KEY_ID,
                                       AWS_SECRET_ACCESS_KEY)
                log.info("connection -------------- %s", conn)
                log.info("bucket name ------------- %s", bucket_name)
                bucket = conn.get_bucket(BUCKET_NAME)
                log.info("bucket --------- %s", bucket)
                log.info("Start File Storageeeeeeeeeeeeeee Image")
                fs = FileSystemStorage()
                filename = fs.save(settings.MEDIA_ROOT + filename_org,
                                   filename)
                uploaded_file_url = fs.url(filename)
                k = Key(bucket)
                k.key = '/user_accomplishments/' + filename_org
                k.set_contents_from_filename(filename)
                k.make_public()
                log.info("store completed image certificate")
                imageurl = 'https://s3-ap-southeast-1.amazonaws.com/' + bucket_name + '/' + filename_org
                log.info("starting a file url in ocal in tableeeeeeeeeee")
                custom_certificate_obj, created = CustomCertificatePath.objects.get_or_create(
                    verify_uuid=verify_uuid,
                    file_path=imageurl,
                    file_name=filename)
예제 #14
0
def program_certificate_store_in_image(self, verify_uuid):
    filename_org = verify_uuid + '.jpg'
    log.info("defindes html2image with path")
    hti = Html2Image(output_path=settings.MEDIA_ROOT)
    certificate_url = settings.LMS_ROOT_URL + '/program/accomplishments/' + verify_uuid
    certificate_image = hti.screenshot(url=certificate_url,
                                       save_as=filename_org)
    bucket_name = BUCKET_NAME + '/user_accomplishments'
    with open(settings.MEDIA_ROOT + filename_org, 'rb') as model_excel:
        filename = model_excel
        conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
        bucket = conn.get_bucket(BUCKET_NAME)
        fs = FileSystemStorage()
        filename = fs.save(settings.MEDIA_ROOT + filename_org, filename)
        uploaded_file_url = fs.url(filename)
        k = Key(bucket)
        k.key = '/user_accomplishments/' + filename_org
        k.set_contents_from_filename(filename)
        k.make_public()
        imageurl = 'https://s3-ap-southeast-1.amazonaws.com/' + bucket_name + '/' + filename_org
        log.info("starting a file url in ocal in tableeeeeeeeeee")
        custom_certificate_obj, created = ProgramCertificatePath.objects.get_or_create(
            verify_uuid=verify_uuid, file_path=imageurl, file_name=filename)
예제 #15
0
def main(args):
    kwargs = {}
    for param in args.keys():
        if not param in [
                "outputfolder", "repo", "HCTI_API_USER_ID", "HCTI_API_KEY"
        ]:
            if args[param]:
                kwargs[param] = args[param]

    commits = []

    hti = Html2Image(output_path=args['outputfolder'])

    for commit in Repository(args['repo'], **kwargs).traverse_commits():
        print(commit.author.name, commit.msg)
        newcommit = {
            "author": commit.author.name,
            "msg": commit.msg,
            "date": commit.author_date,
            "modifications": []
        }

        if hasattr(commit, "modified_files"):
            for m in commit.modified_files:
                newmod = {
                    "filename": m.filename,
                    "change_type": m.change_type.name,
                    #"source_code":m.source_code
                    "hlcode": "",
                    "images": []
                }

                print(
                    m.change_type.name,
                    " modified {}".format(m.filename),
                )

                if m.filename.lower().endswith(
                    ('.png', '.jpg', '.jpeg', 'gif')) == False:
                    fragment = None
                    if m.change_type.name == "ADD":
                        lexer = guess_lexer(m.source_code)
                        fragment = highlight(
                            m.source_code, lexer,
                            HtmlFormatter(wrapcode=True, linenos=True))
                        newmod["hlcode"] = fragment
                        newmod["images"].append(
                            makeFragmentImage(fragment, hti, args))

                    if m.change_type.name == "MODIFY":
                        lexer = guess_lexer(m.source_code)
                        #print (m.changed_methods)
                        lines = str.splitlines(m.source_code)

                        for c in m.changed_methods:
                            code = "\n".join(lines[c.__dict__['start_line'] -
                                                   1:c.__dict__['start_line'] +
                                                   c.__dict__['nloc']])
                            fragment = highlight(
                                m.source_code, lexer,
                                HtmlFormatter(wrapcode=True, linenos=True))
                            newmod[
                                "hlcode"] += "<br /><br />" + fragment  #linenostart=c.__dict__['start_line'],wrapcode=True,linenos=True))
                            newmod["images"].append(
                                makeFragmentImage(fragment, hti, args))
                            #print(code)
                            #print(newmod["hlcode"])

                newcommit["modifications"].append(newmod)
        commits.append(newcommit)

    if not exists(args['outputfolder']):
        os.makedirs(args['outputfolder'])

    outputfile = os.path.join(args['outputfolder'], "index.html")
    of = open(outputfile, "w")

    template = env.get_template('pretty.html')

    content = {"commits": commits}
    output = template.render(content)
    #print(output)
    of.write(output)
    of.close()
예제 #16
0
def sendEmail(user_settings):
    while True:
        #if not sunday - return
        weekday = datetime.today().weekday()
        time = datetime.now()
        hour = time.hour
        minute = time.minute
        second = time.second

        if weekday == 6 and hour == 12 and minute == 0 and second == 0:
            pass
        else:
            continue

        config_path, db_path, _ = user_settings()
        with open(config_path, 'r') as file:
            config_settings = list(csv.reader(file))
            strTos = config_settings[3]
            strTos = list(filter(('Email').__ne__, strTos))

        # Define these once; use them twice!
        strFrom = '*****@*****.**'

        ###########

        #we need to edit this so that it takes from GUI
        #we will DEFINITELY have to 'import GUI'

        ###########
        sec_per_week = 7 * 24 * 60 * 60
        tsamp = 1
        nsamp = 10
        current_time = int(datetime.now().timestamp())
        information = Reader(db_path, 'sensor_db.db').query_by_time(
            current_time - 604800, current_time, '*')

        #information = Reader(db_path,'sensor_db.db').query_by_num(table="SensorData", num=60480) #num is the number of data points. We need to figure out what num is for a whole week
        #information = Reader(db_path,'sensor_db.db').query_by_num(table="SensorData", num= sec_per_week / (tsamp * nsamp)) #num is the number of data points. We need to figure out what num is for a whole week
        #Daniel will apply SQL lite later

        def funcTime(x):
            return x[0]

        timeInfo = list(map(funcTime, information))
        timeInfo = mdates.epoch2num(timeInfo)

        def func1(x):
            return x[1:]

        information = list(map(func1, information))
        #print('information')
        #print(information)

        param_list = [
            'pH', 'TDS (ppm)', 'Rela. Humidity (%)',
            'Air Temp (\N{DEGREE SIGN}C)', 'Water Temp (\N{DEGREE SIGN}C)',
            'Water Level (cm)'
        ]
        plt.figure(figsize=(11, 8))
        plt.suptitle('Weekly Graphs', fontsize=20, color='#4e2a84')
        #allOfData = {}
        for i in range(len(param_list)):

            def func2(x):
                return x[i]

            partOfInfo = list(map(func2, information))
            #print('part of info')
            #print(partOfInfo)
            #allOfData[param_list[i]] = partOfInfo
            plt.subplot(3, 2, 1 + i)
            plt.plot(timeInfo, partOfInfo, color='#4e2a84')
            plt.title(param_list[i])
            ax = plt.gca()
            ax.axes.xaxis_date()
            ax.axes.xaxis.set_major_formatter(mdates.DateFormatter('%a'))

        title = 'thisweek.png'
        table = 'table.png'

        plt.tight_layout()
        plt.savefig(title)

        avgs = []
        mins = []
        maxs = []
        rounding = [2, 2, 1, 1, 2, 2]
        for i in range(len(param_list)):

            def func2(x):
                return x[i]

            partOfInfo = list(map(func2, information))
            partOfInfo = [x for x in partOfInfo if x]
            avgs.append(round(sum(partOfInfo) / len(partOfInfo), rounding[i]))
            mins.append(round(min(partOfInfo), rounding[i]))
            maxs.append(round(max(partOfInfo), rounding[i]))

        for strTo in strTos:
            # Create the root message and fill in the from, to, and subject headers
            msgRoot = MIMEMultipart('related')
            msgRoot['Subject'] = 'NU-ESW Aquaponics Weekly Update'
            msgRoot['From'] = strFrom
            msgRoot['To'] = strTo
            msgRoot.preamble = 'This is a multi-part message in MIME format.'

            # Encapsulate the plain and HTML versions of the message body in an
            # 'alternative' part, so message agents can decide which they want to display.
            msgAlternative = MIMEMultipart('alternative')
            msgRoot.attach(msgAlternative)

            msgText = MIMEText('This is the alternative plain text message.')
            msgAlternative.attach(msgText)

            tableHtml = '''
                <h2>Weekly Statistics</h2>
                <div class="griddy">
                    <div class="item"><b>Metric</b></div>
                    <div class="item"><b>Average</b></div>
                    <div class="item"><b>Minimum</b></div>
                    <div class="item"><b>Maximum</b></div>
                    <div class="item">pH</div>
                    <div class="item">''' + str(avgs[0]) + '''</div>
                    <div class="item">''' + str(mins[0]) + '''</div>
                    <div class="item">''' + str(maxs[0]) + '''</div>
                    <div class="item">TDS (ppm)</div>
                    <div class="item">''' + str(avgs[1]) + '''</div>
                    <div class="item">''' + str(mins[1]) + '''</div>
                    <div class="item">''' + str(maxs[1]) + '''</div>
                    <div class="item">Humidity (%)</div>
                    <div class="item">''' + str(avgs[2]) + '''</div>
                    <div class="item">''' + str(mins[2]) + '''</div>
                    <div class="item">''' + str(maxs[2]) + '''</div>
                    <div class="item">Air Temp (C)</div>
                    <div class="item">''' + str(avgs[3]) + '''</div>
                    <div class="item">''' + str(mins[3]) + '''</div>
                    <div class="item">''' + str(maxs[3]) + '''</div>
                    <div class="item">Water Temp (C)</div>
                    <div class="item">''' + str(avgs[4]) + '''</div>
                    <div class="item">''' + str(mins[4]) + '''</div>
                    <div class="item">''' + str(maxs[4]) + '''</div>
                    <div class="item">Water Level (cm)</div>
                    <div class="item">''' + str(avgs[5]) + '''</div>
                    <div class="item">''' + str(mins[5]) + '''</div>
                    <div class="item">''' + str(maxs[5]) + '''</div>
                </div>
            '''

            tableCss = '''
                h2 {
                    color: #4e2a84;
                    font-size: 30px;
                    text-align: center;
                }
                .container {
                    height: 250px;
                    width: 100%;
                    margin: 0 auto;
                }
                .griddy {
                    max-width: 60%;
                    margin: 0 auto;
                }
                .item {
                    font-size: 15px;
                    color: black;
                    width: 22%;
                    float: left;
                    border: solid 1px black;
                    padding: 1%;
                }
                .gap {
                    width: 100%;
                    height: 10px;
                }
            '''

            hti = Html2Image(output_path=db_path)
            hti.screenshot(html_str=tableHtml,
                           css_str=tableCss,
                           save_as=table,
                           size=(1100, 350))

            #num needs to be changed accordingly

            # We reference the image in the IMG SRC attribute by the ID we give it below
            #msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>Nifty!', 'html')
            html = '''
                <head>
                    <style>
                        header {
                            top: 0;
                            left: 0;
                            width: 100%;
                            border: solid 1px #CCC;
                            padding: 10px;
                            color: white;
                            background-color: #4e2a84;
                        }
                        h1 {
                            font-size: 40px;
                        }
                        h3 {
                            font-size: 20px;
                        }
                        header * {
                            padding: 0px;
                            margin: 0px;
                        }
                    </style>
                </head>
                <body>
                    <header>
                        <h1>AutoAquaponics</h1>
                        <h3>Weekly Report</h3>
                    </header>
                    <img src="cid:image2">
                    <section class="container"></section>
                    <div class="gap"></div>
                    <img src="cid:image1">
                </body>
                
            '''
            msgText = MIMEText(html, 'html')
            msgAlternative.attach(msgText)

            # This example assumes the image is in the current directory

            fp = open(title, 'rb')
            msgImage = MIMEImage(fp.read())
            fp.close()

            # Define the image's ID as referenced above
            msgImage.add_header('Content-ID', '<image1>')
            msgRoot.attach(msgImage)

            fp = open(table, 'rb')
            tableImage = MIMEImage(fp.read())
            fp.close()
            tableImage.add_header('Content-ID', '<image2>')
            msgRoot.attach(tableImage)

            #otherwise it goes here

            # Send the email (this example assumes SMTP authentication is required)

            email = strFrom
            smtp = 'smtp.gmail.com'
            port = 587
            password = '******'
            server = smtplib.SMTP(smtp, port)

            server.starttls()

            server.login(email, password)
            server.sendmail(email, strTo, msgRoot.as_string())

        os.remove(title)
        os.remove(table)
예제 #17
0
def test_screenshot_url():
    hti = Html2Image(output_path=OUTPUT_PATH)

    paths = hti.screenshot(url='https://www.python.org', save_as="pyorg.png")
    img = Image.open(paths[0])
    assert (1920, 1080) == img.size  # default size
예제 #18
0
def main():
    def size_type(string):
        try:
            x, y = map(int, string.split(','))
            return x, y
        except Exception:
            raise argparse.ArgumentTypeError(
                f"size should be int,int, instead got {string}")

    try:
        hti = Html2Image()
    except Exception as e:
        print('Could not instanciate html2image.')
        print(e)
        exit(1)

    parser = argparse.ArgumentParser()

    parser.add_argument('-U', '--url', nargs='*', required=False, default=[])
    parser.add_argument('-H', '--html', nargs='*', required=False, default=[])
    parser.add_argument('-C', '--css', nargs='*', required=False, default=[])
    parser.add_argument('-O', '--other', nargs='*', required=False, default=[])

    parser.add_argument('-S',
                        '--save-as',
                        nargs='*',
                        required=False,
                        default="screenshot.png")
    parser.add_argument('-s',
                        '--size',
                        nargs='*',
                        required=False,
                        default=[],
                        type=size_type)

    parser.add_argument('-o', '--output_path', required=False)

    parser.add_argument('-q', '--quiet', required=False, action="store_true")
    parser.add_argument('-v', '--verbose', required=False, action="store_true")

    parser.add_argument('--browser', required=False)
    parser.add_argument('--chrome_path', required=False)
    # parser.add_argument('--firefox_path', required=False)
    parser.add_argument('--temp_path', required=False)

    args = parser.parse_args()

    if args.verbose:
        print(f'args = {args}')

    if args.output_path:
        hti.output_path = args.output_path

    if args.chrome_path:
        hti.chrome_path = args.chrome_path

    if args.temp_path:
        hti.temp_path = args.temp_path

    paths = hti.screenshot(html_file=args.html,
                           css_file=args.css,
                           other_file=args.other,
                           url=args.url,
                           save_as=args.save_as,
                           size=args.size)

    if not args.quiet:
        print(f'Created {len(paths)} file(s):')
        for path in paths:
            print(f'\t{path}')
예제 #19
0
# load_str, load_file, screenshot_url and screenshot_loaded_file methods.

# minimal html : quite unconventional but browsers can read it anyway
my_html_string = """\
<link rel="stylesheet" href="red_background.css">
<h1> An interesting title </h1>
This page will be red
"""

my_css_string = """\
body {
    background: red;
}
"""

hti = Html2Image()

# image from an url
hti.screenshot_url('https://www.python.org', 'python_org.png', size=(800, 400))

# change output image size for the next images
hti.size = (500, 200)

# image from html & css string
hti.load_str(my_html_string, as_filename='red_page.html')
hti.load_str(my_css_string, as_filename='red_background.css')

hti.screenshot_loaded_file('red_page.html', 'red.png')

# image from html & css files
hti.load_file('blue_page.html')
예제 #20
0
    def get_problem_by_id(self,
                          subject, id,
                          img=None, path_to_img=None, path_to_tmp_html=''):
        """
        Получение информации о задаче по ее идентификатору

        :param subject: Наименование предмета
        :type subject: str

        :param id: Идентификатор задачи
        :type subject: str

        :param img: Принимает одно из двух значений: pyppeteer или grabzit;
                    В результате будет использована одна из библиотек для генерации изображения с задачей.
                    Если не передавать этот аргумент, изображение генерироваться не будет
        :type img: str

        :param path_to_img: Путь до изображения, куда сохранить сохранить задание.
        :type path_to_img: str

        :param path_to_html: Можно указать директорию, куда будут сохраняться временные html-файлы заданий при использовании pyppeteer
        :type path_to_html: str

        :param grabzit_auth: При использовании GrabzIT укажите данные для аутентификации: {"AppKey":"...", "AppSecret":"..."}
        :type grabzit_auth: dict
        """

        doujin_page = requests.get(
            f'{self._SUBJECT_BASE_URL[subject]}/problem?id={id}')
        soup = BeautifulSoup(doujin_page.content, 'html.parser')

        probBlock = soup.find('div', {'class': 'prob_maindiv'})
        if probBlock is None:
            return None

        for i in probBlock.find_all('img'):
            if not 'sdamgia.ru' in i['src']:
                i['src'] = self._SUBJECT_BASE_URL[subject] + i['src']

        URL = f'{self._SUBJECT_BASE_URL[subject]}/problem?id={id}'

        TOPIC_ID = ' '.join(probBlock.find(
            'span', {'class': 'prob_nums'}).text.split()[1:][:-2])
        ID = id

        CONDITION, SOLUTION, ANSWER, ANALOGS = {}, {}, '', []

        try:
            CONDITION = {'text': probBlock.find_all('div', {'class': 'pbody'})[0].text,
                         'images': [i['src'] for i in probBlock.find_all('div', {'class': 'pbody'})[0].find_all('img')]
                         }
        except IndexError:
            pass

        try:
            SOLUTION = {'text': probBlock.find_all('div', {'class': 'pbody'})[1].text,
                        'images': [i['src'] for i in probBlock.find_all('div', {'class': 'pbody'})[1].find_all('img')]
                        }
        except IndexError:
            pass
        except AttributeError:
            pass

        try:
            ANSWER = probBlock.find(
                'div', {'class': 'answer'}).text.replace('Ответ: ', '')
        except IndexError:
            pass
        except AttributeError:
            pass

        try:
            ANALOGS = [i.text for i in probBlock.find(
                'div', {'class': 'minor'}).find_all('a')]
            if 'Все' in ANALOGS:
                ANALOGS.remove('Все')
        except IndexError:
            pass
        except AttributeError:
            pass

        if not img is None:

            for i in probBlock.find_all('div', {'class': 'minor'}):  # delete the information parts of problem
                i.decompose()
            probBlock.find_all('div')[-1].decompose()

            # Pyppeteer
            if img == 'pyppeteer':
                import asyncio
                from pyppeteer import launch
                open(f'{path_to_tmp_html}{id}.html', 'w', encoding='utf-8').write(str(probBlock))
                async def main():
                    browser = await launch()
                    page = await browser.newPage()
                    await page.goto('file:' + path.abspath(f'{path_to_tmp_html}{id}.html'))
                    await page.screenshot({'path': path_to_img, 'fullPage': 'true'})
                    await browser.close()
                asyncio.get_event_loop().run_until_complete(main())
                remove(path.abspath(f'{path_to_tmp_html}{id}.html'))

            # Grabz.it
            elif img == 'grabzit':
                from GrabzIt import GrabzItClient, GrabzItImageOptions
                grabzIt = GrabzItClient.GrabzItClient(self.grabzit_auth['AppKey'], self.grabzit_auth['AppSecret'])
                options = GrabzItImageOptions.GrabzItImageOptions()
                options.browserWidth = 800
                options.browserHeight = -1
                grabzIt.HTMLToImage(str(probBlock), options=options)
                grabzIt.SaveTo(path_to_img)

            # HTML2Image
            elif img == 'html2img':
                from html2image import Html2Image
                if self.html2img_chrome_path == 'chrome': hti = Html2Image()
                else: hti = Html2Image(chrome_path=self.html2img_chrome_path, custom_flags=['--no-sandbox'])
                hti.screenshot(html_str=str(probBlock), save_as=path_to_img)

        return {'id': ID, 'topic': TOPIC_ID, 'condition': CONDITION, 'solution': SOLUTION, 'answer': ANSWER,
                'analogs': ANALOGS, 'url': URL}
예제 #21
0
파일: main.py 프로젝트: PtspluS/card_maker
def create_page_img(html, path, id=0):
    hti = Html2Image(output_path=path + "/img")
    hti.screenshot(html_file=html,
                   css_file="./template/template.css",
                   save_as="page_" + str(id) + ".png")