2

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.

Hillburn
  • 29
  • 9

2 Answers2

1

You shouldn't remove things from an open source project you are trying to use.

Instead, install the missing packages,

pip install requests if you have a problem with that maybe you don't have pip, so just install it.

Or use this python.exe -m pip install requests.


This error Output directory specified is not valid. is due to this line:

if not os.path.isdir(args.output_dir):
    sys.exit('Output directory specified is not valid.')

It generally means that the directory doesn't exist.


As for the last error, it cannot execute the command convert:

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

It just means that you don't have Imagemagick installed. You can install it by downloading the right installer for your architecture here: Link

Then install it with these options ticked: enter image description here

Then try and make sure that the convert command executes from your terminal. If not, follow this instruction:

Lastly you have to set MAGICK_HOME environment variable to the path of ImageMagick (e.g. C:\Program Files\ImageMagick-6.7.7-Q16). You can set it in Computer ‣ Properties ‣ Advanced system settings ‣ Advanced ‣ Environment Variables....

source

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
majidarif
  • 18,694
  • 16
  • 88
  • 133
  • Installed requests, now I don't have to erase it from the script anymore, but I'm still getting the same error messages like I did before – Hillburn Jun 14 '16 at 15:10
  • @Hillburn have you created the folder in question? Does it currently exist? – majidarif Jun 14 '16 at 15:11
  • @Hillburn Try `python3 ttvsnap.py verycoolstreamer ./screenshots/`, but make sure the directory `screenshots` exist on the current directory where you are running the script. – majidarif Jun 14 '16 at 15:12
  • I wrote 'Python ttvsnap.py SaltyBet ./screens/' with a folder called screens inside the ttvsnap folder, but I still get the last error I posted in my question. – Hillburn Jun 14 '16 at 15:24
  • @Hillburn I updated my answer. Its because you don't have the `ImageMagick` installed. – majidarif Jun 14 '16 at 15:25
  • Followed your instructions, still getting the same error. Does ImageMagick support Windows 10? – Hillburn Jun 14 '16 at 15:57
  • @Hillburn did you try to run `convert` command via your terminal if it works? (Just to make sure that `convert` is available). The script should then work if the `convert` command works manually if executed through the terminal. – majidarif Jun 14 '16 at 15:59
  • typing convert in my terminal results in "A file system must be specified." – Hillburn Jun 14 '16 at 16:07
  • @Hillburn see this http://stackoverflow.com/questions/4481573/how-to-override-windows-convert-command-by-imagemagicks-one – majidarif Jun 14 '16 at 16:08
  • According to the link, I have to put ImageMagick before System32 in the PATH environment variable to override "convert". But "C:\Program Files\ImageMagick-7.0.2-Q16" is already at the top of the list. – Hillburn Jun 14 '16 at 16:25
  • @Hillburn sorry man, that is all I can help. You'll have to find out how to install ImageMagick properly for windows. I myself don't use windows for development. – majidarif Jun 14 '16 at 16:28
1

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()
Ryan Townshend
  • 2,404
  • 21
  • 36
  • for my purposes, are there any requisites to selenium (like ImageMagick) or am I good to go once I installed the package? – Hillburn Jun 14 '16 at 16:43
  • Updated the post, with an example for you. – Ryan Townshend Jun 14 '16 at 19:57
  • thanks, the example works with PhantomJS for me. However, I'm new to Python so I don't fully understand how you set the destination of the screenshot. Is the "__ file __" variable referring to the file that contains your script itself? – Hillburn Jun 15 '16 at 08:56
  • Exactly that. Then from there you can use the os.path libraries to build a valid local path. – Ryan Townshend Jun 15 '16 at 12:57