Beispiel #1
0
def preview():
	form = v.PreviewForm(request.values)
	if form.validate():
		filename = os.path.join(os.getcwd(), os.getenv("ARCHIVE3_SCREENSHOT_DIR"), "preview-" + str(form.data["id"]) + ".jpg")
		if os.path.exists(filename):
			return send_file(filename)
		else:
			filename = ss.generate_screenshot({"url": form.data["url"], "width": 1280, "height": 720, "delay": 1})
			return send_file(filename)
	return render_template("error.html", page_title=misc.page_title("500"), data={"header": "500", "error": f"""Unable to generate preview."""}), 500
Beispiel #2
0
def process_preview(connection, payload):
    # Check if URL already exists.
    if db.check_url_exists(connection, payload["url"]):
        db.delete_submission_record(connection, payload["id"])
        connection.commit()
        misc.log_message(f"""URL already exists: {payload["url"]}""")
        return

    # Validate URL
    misc.log_message(f"""Validating URL: {payload["url"]}""")
    try:
        headers = {"User-Agent": os.getenv("ARCHIVE3_USER_AGENT")}
        response = requests.get(payload["url"],
                                headers=headers,
                                timeout=int(os.getenv("ARCHIVE3_URL_TIMEOUT")))
    except:
        raise Exception(f"""Failure while loading URL: {payload["url"]}""")
    response.raise_for_status()

    # Generate screenshot.
    file_path_temp = ss.generate_screenshot({
        "url": payload["url"],
        "width": 1280,
        "height": 720,
        "delay": 2
    })
    misc.log_message(f"Generated screenshot: {file_path_temp}")

    # Compress screenshot.
    file_path_screenshot = os.path.join(
        os.getcwd(), os.getenv("ARCHIVE3_SCREENSHOT_DIR"),
        "preview-" + str(payload["id"]) + ".jpg")
    ss.compress_preview(file_path_temp, file_path_screenshot)
    misc.log_message(
        f"""Compressed screenshot created: {file_path_screenshot}""")

    # Remove temp file.
    os.remove(file_path_temp)
    misc.log_message(f"""Temp file deleted: {file_path_temp}""")

    # Update submission record.
    data = {"ready": "true"}
    db.update_submission_record(connection, payload["id"], data)
    connection.commit()
    misc.log_message(f"""Updated submission record: {payload["id"]}""")
Beispiel #3
0
def main():
    parser = argparse.ArgumentParser(prog='python3 yqttool.py',
                                     description='疫情通填报、截图生成、截图上传工具')

    parser.add_argument(
        'action',
        type=str,
        metavar='ACTION',
        choices=('submit_to_yqt', 'upload_screenshot', 'generate_screenshot'),
        help=('执行的操作。 '
              'submit_to_yqt ,根据 conf/settings.py 的配置填报疫情通; '
              'upload_screenshot ,根据 conf/settings.py 的配置上传疫情通截图; '
              'generate_screenshot ,生成疫情通截图。'))

    parser.add_argument('-n',
                        '--name',
                        type=str,
                        metavar='NAME',
                        help='姓名,生成疫情通截图时必填')
    parser.add_argument('-i',
                        '--stu-id',
                        type=str,
                        metavar='STU_ID',
                        help='学号,生成疫情通截图时必填')
    parser.add_argument('-d',
                        '--date',
                        type=str,
                        metavar='DATE',
                        help='填报疫情通时的日期(格式: yyyy-MM-dd ),生成疫情通截图时选填,缺省值为今天')
    parser.add_argument('-t',
                        '--time',
                        type=str,
                        metavar='TIME',
                        dest='shot_time',
                        help='填报疫情通时的时间(格式: HH:mm ),生成疫情通截图时选填,缺省值为现在')
    parser.add_argument('-b',
                        '--battery',
                        type=str,
                        metavar='BATTERY',
                        help='填报疫情通时手机剩余电量(两位数 10-99 ),生成疫情通截图时选填,缺省值随机')
    parser.add_argument('-o',
                        '--output',
                        type=str,
                        metavar='PATH',
                        help='疫情通截图保存路径,生成疫情通截图时选填,不填则直接显示不保存')

    args = parser.parse_args()

    # 填报疫情通或上传截图
    if args.action in ('submit_to_yqt', 'upload_screenshot'):
        logger.info('模拟运行 SCF')
        ret = main_handler({'Type': 'Timer', 'TriggerName': args.action}, None)
        logger.info(f'运行结果:{ret}')

    # 生成疫情通截图
    elif args.action == 'generate_screenshot':
        name = args.name
        stu_id = args.stu_id
        if not name or not stu_id:
            logger.error('生成疫情通截图时,参数 --name 和 --stu-id 必填')
            return
        name = name[:6]
        stu_id = stu_id[:11]

        date = args.date[:10] if args.date else None
        shot_time = args.shot_time[:5] if args.shot_time else None

        battery = args.battery
        try:
            battery = int(battery) if 10 <= int(battery) <= 99 else None
        except (TypeError, ValueError):
            battery = None

        img = generate_screenshot(name, stu_id, date, shot_time, battery)

        if args.output:  # 保存图片
            img.save(args.output, 'png')
        else:  # 显示图片
            img.show()

        img.close()

    # 理论上不可能出现的情况
    else:
        logger.error('难以理解的要求')
Beispiel #4
0
def process_screenshot(connection, payload):
    # Validate URL
    misc.log_message(f"""Validating URL: {payload["url"]}""")
    try:
        headers = {"User-Agent": os.getenv("ARCHIVE3_USER_AGENT")}
        response = requests.get(payload["url"],
                                headers=headers,
                                timeout=int(os.getenv("ARCHIVE3_URL_TIMEOUT")))
        if len(response.content) < 1:
            raise Exception("No content received.")
    except:
        raise Exception(f"""Failure while loading URL: {payload["url"]}""")
    response.raise_for_status()

    # Generate Screenshot
    file_path_temp = ss.generate_screenshot({"url": payload["url"]})
    misc.log_message(f"Generated screenshot: {file_path_temp}")

    # Generate Block
    block = b.generate_screenshot_block(file_path_temp)
    hash = block["data_hash"]
    misc.log_message(f"Block created: {block['id']}")

    # Rename screenshot.
    file_path_screenshot = os.path.join(misc.hash_to_path(hash), hash + ".png")
    os.makedirs(os.path.dirname(file_path_screenshot), exist_ok=True)
    os.rename(file_path_temp, file_path_screenshot)
    misc.log_message(
        f"""Screenshot moved to destination: {file_path_screenshot}""")

    # Generate thumbnail.
    file_path_thumb = os.path.join(misc.hash_to_path(hash),
                                   hash + "_thumb.jpg")
    ss.generate_thumbnail(file_path_screenshot, file_path_thumb)
    misc.log_message(f"""Thumbnail created: {file_path_thumb}""")

    # Upload to S3.
    if os.getenv("AWS_S3_UPLOAD_ENABLED") == "true":
        s3 = boto3.resource(
            "s3",
            aws_access_key_id=os.getenv("AWS_S3_ACCESS_KEY_ID"),
            aws_secret_access_key=os.getenv("AWS_S3_SECRET_ACCESS_KEY"))
        bucket = s3.Bucket(os.getenv("AWS_S3_BUCKET_ID"))
        bucket.put_object(
            Key=os.path.basename(file_path_screenshot),
            Body=open(file_path_screenshot, "rb"),
            StorageClass=os.getenv("AWS_S3_STORAGE_CLASS"),
            ACL="public-read",
            ContentType=mimetypes.guess_type(file_path_screenshot)[0])
        misc.log_message(
            f"""Uploaded to S3: {os.getenv("AWS_S3_CDN_BASE_URL")}{os.path.basename(file_path_screenshot)}"""
        )
        bucket.put_object(
            Key=os.path.basename(file_path_thumb),
            Body=open(file_path_thumb, "rb"),
            StorageClass=os.getenv("AWS_S3_STORAGE_CLASS"),
            ACL="public-read",
            ContentType=mimetypes.guess_type(file_path_screenshot)[0])
        misc.log_message(
            f"""Uploaded to S3: {os.getenv("AWS_S3_CDN_BASE_URL")}{os.path.basename(file_path_thumb)}"""
        )

    # Update url record.
    data = {"timestamp_updated": "NOW()"}
    db.update_url_record(connection, payload["id"], data)
    connection.commit()
    misc.log_message(f"""Updated url record: {payload["id"]}""")

    # Create data record.
    data = {"url_id": payload["id"], "hash": hash, "block_id": block["id"]}
    data_record_id = db.create_data_record(connection, data)
    connection.commit()
    misc.log_message(f"""Created data record: {data_record_id}""")

    # Delete screenshots.
    if os.getenv("ARCHIVE3_DELETE_SCREENSHOTS") == "true":
        os.remove(file_path_screenshot)
        misc.log_message(
            f"""Deleted local screenshot: {file_path_screenshot}""")
        os.remove(file_path_thumb)
        misc.log_message(f"""Deleted local screenshot: {file_path_thumb}""")
Beispiel #5
0
def main_handler(event, _):

    # 定时触发,填报疫情通
    if event.get('Type') == 'Timer' and event.get('TriggerName') == 'submit_to_yqt':
        # [总数, 成功数]
        count = [0, 0]

        for item in WORKPIECES:
            if 'submit_to_yqt' in item['actions']:
                try:
                    count[0] += 1
                    submit_to_yqt(item['stu_id'], item['passwd'])
                    count[1] += 1
                except ValueError as e:
                    if str(e) == '今天已经填报疫情通,不能重复填报':
                        logger.warning(f'{item["name"]} 今天已经填报疫情通,已忽略')
                        count[1] += 1
                    else:
                        raise e
                except Exception as e:
                    logger.warning(f'{item["name"]} 填报疫情通失败,已忽略,请手动填报。错误信息:{e}')

        summary_msg = f'填报疫情通完成({count[1]}/{count[0]})'
        logger.info(summary_msg)
        return summary_msg

    # 定时触发,上传截图
    elif event.get('Type') == 'Timer' and event.get('TriggerName') == 'upload_screenshot':
        # [总数, 成功数]
        count = [0, 0]

        for item in WORKPIECES:
            if 'upload_screenshot' in item['actions']:
                try:
                    count[0] += 1
                    upload_yqt_screenshot(name=item['name'], stu_id=item['stu_id'], dorm=item['dorm'])
                    count[1] += 1
                except Exception as e:
                    logger.warning(f'{item["name"]} 截图上传失败,已忽略,请手动上传,错误信息:{e}')

        summary_msg = f'上传截图完成({count[1]}/{count[0]})'
        logger.info(summary_msg)
        return summary_msg

    # API 网关触发,发送定位页
    elif 'queryString' in event and 'gps' in event['queryString']:
        with open('get_geo_api_info.html', 'rt', encoding='utf-8') as fp:
            gps_page = fp.read()
        return {
            'body': gps_page,
            'statusCode': 200,
            'headers': {'Content-Type': 'text/html'},
            'isBase64Encoded': False
        }

    # API 网关触发,生成疫情通截图
    elif 'queryString' in event:

        name = event['queryString'].get('name')
        stu_id = event['queryString'].get('stu_id')
        if not name or not stu_id:
            return {
                'body': '请求参数 `name` 和 `stu_id` 必须提供',
                'statusCode': 400,
                'headers': {'Content-Type': 'text/plain'},
                'isBase64Encoded': False
            }

        date = event['queryString'].get('date')
        shot_time = event['queryString'].get('time')
        battery = event['queryString'].get('battery')

        name = name[:6]
        stu_id = stu_id[:11]
        date = date[:10] if date else None
        shot_time = shot_time[:5] if shot_time else None
        try:
            battery = int(battery) if 10 <= int(battery) <= 99 else None
        except (TypeError, ValueError):
            battery = None

        img = generate_screenshot(name, stu_id, date, shot_time, battery)
        fp = BytesIO()
        img.save(fp, format='png')
        img.close()

        return {
            'body': base64.standard_b64encode(fp.getvalue()).decode(),
            'statusCode': 200,
            'headers': {'Content-Type': 'image/png'},
            'isBase64Encoded': True
        }

    else:
        raise RuntimeError('不能理解的触发方式')
Beispiel #6
0
from burger import get_coupon_image, get_coupon_qr
from screenshot import generate_screenshot
from pathlib import Path

CODES = ["5bd1c98203df2a0261635822"]

for c in CODES:
    code = get_coupon_qr(c)

    file = Path("{}.png".format(code))
    if not file.is_file():
        qr = get_coupon_image(c)

        print("[+] Generating screenshot for {}".format(code))
        generate_screenshot(code, qr)
    else:
        print("[] Screenshot already generated for {}".format(code))