2021-06-14 08:52:58 +03:00
# utils
2021-06-24 19:00:59 +03:00
Small tools needed to solve immediate tasks independently or as part of a project
* [`camsutil` ](https://git.hmp.today/pavel.muhortov/utils#camsutil )
* [`cronutil` ](https://git.hmp.today/pavel.muhortov/utils#cronutil )
* [`confutil`.py ](https://git.hmp.today/pavel.muhortov/utils#confutil-py )
* [`ffmpeger`.py ](https://git.hmp.today/pavel.muhortov/utils#ffmpeger-py )
* [`procutil`.py ](https://git.hmp.today/pavel.muhortov/utils#procutil-py )
* [`sendmail`.py ](https://git.hmp.today/pavel.muhortov/utils#sendmail-py )
2021-07-10 11:06:54 +03:00
* [`simplewc`.py ](https://git.hmp.today/pavel.muhortov/utils#simplewc-py )
2021-06-23 01:10:06 +03:00
____
2021-06-24 19:00:59 +03:00
## `camsutil`
2021-06-23 01:10:06 +03:00
**Description:** Creation of a request to the camera API based on the prepared template
**Dependencies:** Python 3 (tested version 3.9.5)
| PARAMETERS | DESCRIPTION | DEFAULT|
|-------------|-------------|--------|
|**--host**|hostname or ip address|**REQUIRED**|
|**--user**|valid user|**REQUIRED**|
|**--password**|valid password|**REQUIRED**|
|**--template**|the name of an existing template|**REQUIRED**|
|**[-h]**|print help and exit||
|**[--protocol]**|http, https, etc.|http|
|**[--port]**|port number|80|
|**[--channel]**|ptz channel number|101|
|**[--vid]**|video channel id|1|
|**[--x]**|horizontal positioning: azimuth, pan|0|
|**[--y]**|vertical positioning: elevation, tilt|0|
|**[--z]**|zoom direction, absolute zoom|0|
|**[--speed]**|positioning speed: from 1 to 7|1|
|**[--time]**|momentary duration, max 100000ms|100000|
|**[--text]**|overlay text content|`None`|
|**[--enabled]**|enabled (true) or disabled (false) overlay text|true|
Example usage in terminal with Python:
```shell
python3 ./ptz.py --host HOST --user USER --password PASS --template Hikvision_GetCapabilities.html
```
Example usage in terminal with make the script executable:
```shell
chmod u+x ./ptz.py
ptz.py --host HOST --user USER --password PASS --template Hikvision_PtzPreset.html --vid 2 --speed 5
```
Example usage in Python:
```Python
from camsutil import ptz
data = ptz.API(host='HOST', user='USER', password='PASS', template='Hikvision_GetJPEG.html', x=1920, y=1080).call()
with open('img.jpg', 'wb') as output:
output.write(data)
```
2021-06-24 18:30:44 +03:00
____
2021-06-24 19:00:59 +03:00
## `cronutil`
2021-06-24 18:30:44 +03:00
**Description:** Control wrapper for the [schedule ](https://github.com/dbader/schedule ) package
**Dependencies:** Python 3 (tested version 3.9.5)
Scheduler works only on a weekly scale. This is due to the use of the [schedule ](https://github.com/dbader/schedule ) package .
Target format: wD:HH:MM:SS, where:
wD - day of week unit: `'1'` , `'2'` , ... , `'6'` , `'7'` , `'*'`
HH - hour unit in 24-hours format: `'00'` , `'01'` , ... , `'22'` , `'23'` , `'**'`
MM - minute unit: `'00'` , `'01'` , ... , `'58'` , `'59'` , `'**'`
SS - second unit: `'00'` , `'01'` , ... , `'58'` , `'59'` , `'**'`
Units can be listed separated by commas.
Examples:
`'*:**:**:**'` - every second of every minute of every hour of every day
`'*:**:**:*5'` - every 05,15,25,35,45,55 seconds of every minute of every hour of every day
`'*:2*:**:**'` - every second of every minute of every 20,21,22,23 hours of every day
`'*:2*:**:*5'` - every 05,15,25,35,45,55 seconds of every minute of every 20,21,22,23 hours of every day
`'3,5:2*:**:*5'` - every 05,15,25,35,45,55 seconds every minute every 20,21,22,23 hours of Wednesday, Friday
`'1,7:12:00:**'` - every second 00 minutes 12 hours of Monday, Sunday
`'1:07:00:00'` - every 00 seconds 00 minutes 07 hours Monday
Example usage in Python:
```Python
from time import strftime
from cronutil import Scheduler
def now():
print(strftime('%Y.%m.%d %H:%M:%S'))
cron = Scheduler()
cron.add('2,4:**:*0:00,15,30,45', now)
cron.start()
2021-06-25 09:21:25 +03:00
cron.add('2,4:**:59:59', cron.stop)
2021-06-24 18:30:44 +03:00
```
2021-06-15 15:34:01 +03:00
____
2021-06-24 19:00:59 +03:00
## `confutil`.py
**Description:** Parser of configs, arguments, parameters
**Dependencies:** Python 3 (tested version 3.9.5)
Example config to parse:
```text
[main]
# This block contains basic parameters
[httpd]
# This block contains parameters for the http server
# Address to which to bind listening
#address=0.0.0.0;
# Port to which to bind listening. Port below 1024 requires root privileges.
port=8800;
# Working directory (available to everyone)
directory=www;
```
Example usage in Python:
```Python
from os import path
from confutil import Parse
conf = path.splitext(__file__)[0] + '.conf'
if path.exists(conf):
print(Parse(parameters=conf, block='httpd'))
```
____
## `ffmpeger`.py
2021-06-20 15:56:25 +03:00
**Description:** FFmpeg management from Python
2021-07-10 11:06:54 +03:00
**Dependencies:** Python 3 (tested version 3.9.5), installed or downloaded [ffmpeg ](https://ffmpeg.org/download.html ), [procutil.py ](https://git.hmp.today/pavel.muhortov/utils#procutil-py ) in the same directory
2021-06-20 15:56:25 +03:00
| PARAMETERS | DESCRIPTION | DEFAULT|
|-------------|-------------|--------|
|**-s**, ** --src**|sources urls|**REQUIRED**|
|**[-h]**|print help and exit||
|**[--preset]**|240p, 360p, 480p, 720p, 1080p, 1440p, 2160p|`None`|
|**[--fps]**|frame per second encoding output|`None`|
|**[--dst]**|destination url|`None`|
|**[--ffpath]**|alternative path to bin|`None`|
|**[--watchdog]**|detect ffmpeg freeze and terminate||
|**[--sec]**|seconds to wait before the watchdog terminates|15|
|**[--mono]**|detect ffmpeg running copy and terminate||
Example usage in cron with Python:
```shell
2021-07-10 11:06:54 +03:00
# at every minute
2021-06-20 15:56:25 +03:00
* * * * * /usr/bin/python3 ~/ffmpeger.py -s rtsp://user:pass@host:554/Streaming/Channels/101 --dst rtmp://a.rtmp.youtube.com/live2/YOUKEY --mono --watchdog --sec 30 >> /dev/null 2>& 1
```
Example usage in terminal with make the script executable:
```shell
chmod u+x ./ffmpeger.py
ffmpeger.py -s rtsp://user:pass@host:554/Streaming/Channels/101 --dst rtp://239.0.0.1:5554
```
Example usage in Python:
```Python
from ffmpeger import FFmpeg
FFmpeg.run(src='null, anull', preset='240p', fps=10)
```
2021-06-20 19:23:02 +03:00
2021-06-20 15:56:25 +03:00
____
2021-06-24 19:00:59 +03:00
## `procutil`.py
2021-06-21 00:05:04 +03:00
**Description:** Find a running process from Python
**Dependencies:** Python 3 (tested version 3.9.5)
| PARAMETERS | DESCRIPTION | DEFAULT|
|-------------|-------------|--------|
|**[-h]**|print help and exit||
|**[--find]**|find process pid, name or arguments||
|**[--exclude]**|exclude process pid, name or arguments|`None`|
|**[--self]**|find a clones of self|`True`|
|**[--kill]**|kill the process with pid||
Example usage in terminal with Python for find all running processes:
```shell
python3 ./procutil.py
```
Example usage in terminal with make the script executable for find all specified processes:
```shell
chmod u+x ./procutil.py
./procutil.py --find ssh --exclude sftp
```
Example usage in Python for find a clones of self:
```Python
from os import getpid
from sys import argv
from procutil import Proc
processes = Proc.search(' '.join(argv), str(getpid()))
if processes:
for process in processes:
print(process)
```
2021-06-24 19:00:59 +03:00
## `sendmail`.py
2021-06-20 15:56:25 +03:00
**Description:** Sending email from Python
**Dependencies:** Python 3 (tested version 3.9.5)
2021-06-15 15:34:01 +03:00
| PARAMETERS | DESCRIPTION | DEFAULT|
|-------------|-------------|--------|
|**-u**, ** --user**|smtp valid user|**REQUIRED**|
|**-p**, ** --pass**|smtp valid password|**REQUIRED**|
|**-d**, ** --dest**|destination addresses|**REQUIRED**|
|**[-h]**|print help and exit||
|**[--smtp]**|smtp hostname or ip address|smtp.gmail.com|
|**[--port]**|smtp port number|587|
|**[--stls]**|smtp required TLS|`True`|
|**[--from]**|mail from alias|**--user** value|
|**[--subj]**|mail subject|'no subject'|
|**[--text]**|mail body text|'no text'|
|**[--type]**|mail body type: plain, html|plain|
|**[--file]**|mail attachment files|`None`|
2021-06-16 20:20:35 +03:00
Example usage in terminal with Python:
2021-06-15 15:34:01 +03:00
```shell
python3 ./sendmail.py -u user@gmail.com -p pass -d addr1@gmail.com,addr2@gmail.com
```
Example usage in terminal with make the script executable:
```shell
chmod u+x ./sendmail.py
./sendmail.py -u user@gmail.com -p pass -d addr1@gmail.com,addr2@gmail.com
```
Example usage in Python:
```Python
from sendmail import Mail
msg = Mail(smtp_user='user@gmail.com', smtp_pass='pass', mail_dest='addr1@gmail.com,addr2@gmail.com')
log = msg.send()
print(log)
2021-06-16 20:20:35 +03:00
```
2021-06-20 19:23:02 +03:00
2021-06-16 20:20:35 +03:00
____
2021-07-10 11:06:54 +03:00
## `simplewc`.py
**Description:** Update Let's Encrypt wildcard certificate with DNS-01 challenge
**Dependencies:** Python 3 (tested version 3.9.5),
installed or downloaded [acme.sh ](https://github.com/Neilpang/acme.sh ),
installed [dnspython ](https://github.com/rthalley/dnspython ) package,
dns is supported to [dynamic update ](https://en.wikipedia.org/wiki/Dynamic_DNS )
| PARAMETERS | DESCRIPTION | DEFAULT|
|-------------|-------------|--------|
|**--domain**|domain for which the wildcard certificate is issued|**REQUIRED**|
|**--server**|master server containing the domain zone|**REQUIRED**|
|**--keyname**|name of the key to update the zone|**REQUIRED**|
|**--keydata**|content of the key to update the zone|**REQUIRED**|
|**[-h]**|print help and exit||
|**[--acmepath]**|alternative path to bin (example: ~/.acme.sh/acme.sh)|`None`|
|**[--force]**|"force" argument for the acme.sh|`False`|
|**[--test]**|"test" argument for the acme.sh|`False`|
Example usage in cron with Python:
```shell
# at 00:00 on Monday
0 0 * * 1 /usr/bin/python3 ~/simplewc.py --domain EXAMPLE.COM --server 8.8.8.8 --keyname KEY --keydata YOU_KEY_CONTENT > /dev/null
```
Example usage in terminal with make the script executable:
```shell
chmod u+x ./simplewc.py
./simplewc.py --domain EXAMPLE.COM --server 8.8.8.8 --keyname KEY --keydata YOU_KEY_CONTENT --test --force
```
____