python 一个半生不熟的想法,可以更容易地进行可重复的截屏过程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 一个半生不熟的想法,可以更容易地进行可重复的截屏过程相关的知识,希望对你有一定的参考价值。

# Copyright 2016 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import signal
import subprocess
from threading import Thread
import time

from selenium import webdriver
from selenium.webdriver.common.keys import Keys


class Camera(object):

    def __init__(self, output_file, xPos=None, yPos=None, height=None,
                 width=None):
        self.output_file = output_file
        self.recording = False
        self.stderr = None
        self.stdout = None

        recording_args = ['recordmydesktop']
        if xPos:
            recording_args.append("-x")
            recording_args.append(str(xPos))
        if yPos:
            recording_args.append("-y")
            recording_args.append(str(yPos))
        if height:
            recording_args.append("-height")
            recording_args.append(str(height))
        if width:
            recording_args.append("-width")
            recording_args.append(str(width))

        recording_args.append("-o")
        recording_args.append(self.output_file)

        self.process = subprocess.Popen(
            recording_args,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE
        )

    def start_recording(self):
        self.stdout, self.stderr = self.process.communicate()
        if self.stderr == '':
            self.recording = True

    def toggle_pause(self):
        self.process.send_signal(signal.SIGUSR1)
        return self.process.communicate()

    def pause_recording(self):
        stdout, stderr = self.toggle_pause()

    def unpause_recording(self):
        stdout, stderr = self.toggle_pause()

    def stop_recording(self):
        self.process.send_signal(signal.SIGINT)
        return self.process.communicate()


class Narrator(object):

    def __init__(self, options):
        self.options = options

    def say(self, text):
        initial_pause = self.options.get("initial_pause", 0)
        time.sleep(initial_pause)
        process = subprocess.Popen(['espeak', '"%s"' % text],
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
        process.communicate()


class Movie(object):

    def __init__(self, camera):
        self.camera = camera

    def __enter__(self):
        cameraman = Thread(target=self.camera.start_recording, args=())
        cameraman.start()

    def __exit__(self, type, value, traceback):
        self.camera.stop_recording()
        pass


class Scene(object):

    def __init__(self, script, narrator):
        self.script = script
        self.narrator = narrator

    def __enter__(self):
        narration = Thread(target=self.narrator.say, args=(self.script,))
        narration.start()

    def __exit__(self, type, value, traceback):
        pass


def main():

    # set the stage
    browser = webdriver.Firefox()
    window_pos = browser.get_window_position()
    window_size = browser.get_window_size()
    browser.get("http://www.google.com/")
    time.sleep(2)

    camera = Camera(
        'demo.ogv',
        xPos=window_pos.get('x'),
        yPos=window_pos.get('y'),
        height=window_size.get('height'),
        width=window_size.get('width')
    )

    narrator = Narrator({})

    with Movie(camera) as movie:

        script = ("This is an example demonstration of how to search google to"
                  " find more information about Triple O.")
        with Scene(script, narrator) as scene:
            search = browser.find_element_by_name('q')
            search.send_keys("openstack on openstack")
            search.send_keys(Keys.RETURN) # hit return after you enter search text
            time.sleep(10) # sleep for 5 seconds so you can see the results
            browser.quit()

if __name__ == "__main__":
    main()

以上是关于python 一个半生不熟的想法,可以更容易地进行可重复的截屏过程的主要内容,如果未能解决你的问题,请参考以下文章

用Python编写web爬虫的5个方法

什么是 python 代码来转置面板数据以使其更容易进行回归和进一步分析

用Python构建一个电子表格应用程序的,以使数据科学变得更容易

shell学习-常用语句

python的优缺点是啥?

Beta阶段个人总结