Search code examples
pythonscreenshotpython-3.5twitch

Grabbing a screenshot from Twitch with Python


Right now, I'm starting a Python project which is supposed to take a screenshot of selected twitch channels, modify those screenshots and put them on a GUI. The GUI shouldn't be a problem, but I'm having problems with the screenshots.
I've found 2 resources to deal with the twitch communication: the python-twitch package and a script called ttvsnap (https://github.com/chfoo/ttvsnap).
The package was no help to me, because I didn't find anything related to screenshots. The script looked promising, but I encountered some problems:

According to the creator, ttvsnap periodically takes screenshots of a twitch stream and puts them in a selected directory.
If I try to start the script, I'm getting this error:

Traceback (most recent call last):  
    File "ttvsnap.py", line 13, in <module>
        import requests  
ImportError: No module named 'requests'

Erasing "import requests" from the script allows me to run it, but the script then has a problem with selecting a directory. To run the script, I'm supposed to write:

Python ttvsnap.py 'streamname here' 'directory here'

The example directory from the creator was './screenshot/', but with that input, I'm getting the following error (maybe because I'm on Windows?):

Output directory specified is not valid.

Trying a directory like C:\DevFiles\Screenshots give me the following error:

Invalid drive specification. ###Translated this line since I'm using a German OS
Traceback (most recent call last):
  File "ttvsnap.py", line 176, in <module>
    main()
  File "ttvsnap.py", line 46, in main
    subprocess.check_call(['convert', '-version'])
  File "C:\Program Files (x86)\Python35-32\lib\subprocess.py", line 584, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['convert', '-version']' returned non-zero exit status 4

Any idea on how to get it to run or a different resource to use would be much appreciated.


Solution

  • Selenium can be handy for navigating a site and taking screenshots.

    http://selenium-python.readthedocs.io/

    Fleshed out an example that should do what you need. Gist link as well : https://gist.github.com/ryantownshend/6449c4d78793f015f3adda22a46f1a19

    """
    basic example.
    
    Dirt simple example of using selenium to screenshot a site.
    
    Defaults to using local Firefox install.
    Can be setup to use PhantomJS
    
    http://phantomjs.org/download.html
    
    This example will run in both python 2 and 3
    """
    import os
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import WebDriverWait
    
    
    def main():
        """the main function."""
        driver = webdriver.Firefox()
        # driver = webdriver.PhantomJS()
        driver.get("http://google.com")
        # assert "Python" in driver.title
        elem = driver.find_element_by_name("q")
        elem.clear()
        elem.send_keys("cats")
        elem.send_keys(Keys.RETURN)
    
        # give the query result time to load
        WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.ID, "resultStats"))
        )
    
        # take the screenshot
        pwd = os.path.dirname(os.path.realpath(__file__))
        driver.save_screenshot(os.path.join(pwd, 'cats.png'))
        driver.close()
    
    if __name__ == '__main__':
        main()