Posts: 5
Threads: 1
Joined: Sep 2024
Hello, I have a windows box running an HDhomerun box. I'm trying to change channels remotely. I have made a very simple little script, and it changes the channels perfectly, however I'm stuck at trying to do this remotely. Here's my little code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import pyautogui
import time
channel1 = '41'
channel2 = '08'
time.sleep( 5 )
pyautogui.click( 'HDHR-live.png' )
time.sleep( 25 )
pyautogui.typewrite( str (channel1))
time.sleep( 2.5 )
pyautogui.press( 'enter' )
time.sleep( 20 )
pyautogui.typewrite( str (channel2))
time.sleep( 2 )
pyautogui.press( 'enter' )
time.sleep( 30 )
pyautogui.press( 'esc' )
|
My thought was to use flask and be able to enter the channel number via an http call so that the python script can change the channel.
This is what I thought to input a channel number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
from flask import Flask
app = Flask(__name__)
@app .route( '/channel/<number>' )
def channel_name(number):
return 'Channel %s!' % number
if __name__ = = '__main__' :
app.run()
|
And again, this one works by itself, but I have no idea on how to actually get the first script to listen for the flask http call for a channel number.
Maybe I'm over thinking this? Maybe there's an easier way to get this done?
Any input is appreciated.
Posts: 7,324
Threads: 123
Joined: Sep 2016
Sep-16-2024, 08:37 PM
(This post was last modified: Sep-16-2024, 08:38 PM by snippsat.)
Something like this put together.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
from flask import Flask
import pyautogui
import time
app = Flask(__name__)
def change_channel(channel_number):
print (channel_number)
pyautogui.click( 'HDHR-live.png' )
time.sleep( 25 )
pyautogui.typewrite(channel_number)
time.sleep( 2 )
pyautogui.press( 'enter' )
@app .route( '/channel/<number>' )
def channel_name(number):
change_channel(number)
return f 'Channel changed to {number}!'
if __name__ = = '__main__' :
app.run()
|
Run.
1 2 3 4 5 6 7 8 9 |
G:\div_code\foobar
λ flask - - app tv run
* Serving Flask app 'tv'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http: / / 127.0 . 0.1 : 5000
Press CTRL + C to quit
99
127.0 . 0.1 - - [ 16 / Sep / 2024 22 : 40 : 47 ] "GET /channel/99 HTTP/1.1" 200 -
|
In browser http://127.0.0.1:5000/channel/99
Output: Channel changed to 99!
Posts: 5
Threads: 1
Joined: Sep 2024
Thank you for the code.
I did try it. Parts of it work. I do see it handing off the channel number, but then I get an internal server error on the browser, and then command line I get the following error:
Quote:E:\Python>flask --app tv run
* Serving Flask app 'tv'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
12
[2024-09-16 19:53:39,005] ERROR in app: Exception on /channel/12 [GET]
Traceback (most recent call last):
File "E:\Python\Lib\site-packages\pyautogui\__init__.py", line 172, in wrapper
return wrappedFunction(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\Python\Lib\site-packages\pyautogui\__init__.py", line 210, in locateOnScreen
return pyscreeze.locateOnScreen(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\Python\Lib\site-packages\pyscreeze\__init__.py", line 405, in locateOnScreen
retVal = locate(image, screenshotIm, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\Python\Lib\site-packages\pyscreeze\__init__.py", line 383, in locate
points = tuple(locateAll(needleImage, haystackImage, **kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\Python\Lib\site-packages\pyscreeze\__init__.py", line 371, in _locateAll_pillow
raise ImageNotFoundException('Could not locate the image.')
pyscreeze.ImageNotFoundException: Could not locate the image.
During handling of the above exception, another exception occurred:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:\Python\Lib\site-packages\flask\app.py", line 1473, in wsgi_app
response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\Python\Lib\site-packages\flask\app.py", line 882, in full_dispatch_request
rv = self.handle_user_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\Python\Lib\site-packages\flask\app.py", line 880, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File "E:\Python\Lib\site-packages\flask\app.py", line 865, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\Python\tv.py", line 24, in channel_name
change_channel(number)
File "E:\Python\tv.py", line 13, in change_channel
pyautogui.click('HDHR-live.png')
File "E:\Python\Lib\site-packages\pyautogui\__init__.py", line 594, in wrapper
returnVal = wrappedFunction(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\Python\Lib\site-packages\pyautogui\__init__.py", line 985, in click
x, y = _normalizeXYArgs(x, y)
^^^^^^^^^^^^^^^^^^^^^^
File "E:\Python\Lib\site-packages\pyautogui\__init__.py", line 663, in _normalizeXYArgs
location = locateOnScreen(firstArg)
^^^^^^^^^^^^^^^^^^^^^^^^
pyautogui.ImageNotFoundException
127.0.0.1 - - [16/Sep/2024 19:53:39] "GET /channel/12 HTTP/1.1" 500 -
The image it's looking for it's in the same folder that the script is, and it worked before.
Posts: 5
Threads: 1
Joined: Sep 2024
I take that back. I ran it from a different computer on the network, and it did actually change the channel the first time, but when I went to change the channel a second time, it gave me the internal server error.
I think I may have an idea. I'm going to try a few things, will get back if it works. Thanks.
Posts: 5
Threads: 1
Joined: Sep 2024
So the problem was that the LIVE button image either was not exactly the same all the time, or it was highlighted so the click(HDHR-live.png would fail because it wouldn't find it all the time. I tried a few different ways to fix that, but I couldn't. I tried adding a confidence of .7 but it did not like that coding at all.
What I ended up doing is just moving the mouse over with coordinates and enter a click. That works.
One last question, How can I "end" the tv watching. I would like to put in something like stop, or end instead of the channel number and then with an "if" statement send an esc key to the application, that would take it out of live tv.
Posts: 7,324
Threads: 123
Joined: Sep 2016
Sep-17-2024, 01:34 PM
(This post was last modified: Sep-17-2024, 01:39 PM by snippsat.)
(Sep-17-2024, 06:49 AM)greggk1 Wrote: One last question, How can I "end" the tv watching. I would like to put in something like stop, or end instead of the channel number and then with an "if" statement send an esc key to the application, that would take it out of live tv. Can do it like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
from flask import Flask
import pyautogui
import time, sys
app = Flask(__name__)
def change_channel(channel_number):
if channel_number = = 'stop' :
print ( f 'Command given is <{channel_number}>' )
else :
pyautogui.click( 'HDHR-live.png' )
time.sleep( 25 )
pyautogui.typewrite(channel_number)
time.sleep( 2 )
pyautogui.press( 'enter' )
@app .route( '/channel/<number>' )
def channel_name(number):
change_channel(number)
return f 'Channel changed to {number}!'
if __name__ = = '__main__' :
app.run()
|
1 2 3 4 5 6 7 8 9 |
G:\div_code\foobar
λ flask - - app tv run
* Serving Flask app 'tv'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http: / / 127.0 . 0.1 : 5000
Press CTRL + C to quit
Command given is <stop>
127.0 . 0.1 - - [ 17 / Sep / 2024 15 : 42 : 51 ] "GET /channel/stop HTTP/1.1" 200 -
|
Posts: 5
Threads: 1
Joined: Sep 2024
Wow, that worked nicely. Much appreciated.
|