Updater script which should keep base up to date.

This commit is contained in:
UltraDesu
2018-01-17 03:43:23 +03:00
parent 0f7ae242dc
commit 94e0637ae3
5 changed files with 263 additions and 25 deletions

186
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,186 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "${file}",
"cwd": "${workspaceFolder}",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput"
]
},
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"localRoot": "${workspaceFolder}",
"remoteRoot": "${workspaceFolder}",
"port": 3000,
"secret": "my_secret",
"host": "localhost"
},
{
"name": "Python: Terminal (integrated)",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "${file}",
"cwd": "",
"console": "integratedTerminal",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": []
},
{
"name": "Python: Terminal (external)",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "${file}",
"cwd": "",
"console": "externalTerminal",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": []
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "${workspaceFolder}/manage.py",
"cwd": "${workspaceFolder}",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput",
"DjangoDebugging"
]
},
{
"name": "Python: Flask (0.11.x or later)",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config:python.pythonPath}",
"program": "fully qualified path fo 'flask' executable. Generally located along with python interpreter",
"cwd": "${workspaceFolder}",
"env": {
"FLASK_APP": "${workspaceFolder}/quickstart/app.py"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput"
]
},
{
"name": "Python: Flask (0.10.x or earlier)",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config:python.pythonPath}",
"program": "${workspaceFolder}/run.py",
"cwd": "${workspaceFolder}",
"args": [],
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput"
]
},
{
"name": "Python: PySpark",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"osx": {
"pythonPath": "${env:SPARK_HOME}/bin/spark-submit"
},
"windows": {
"pythonPath": "${env:SPARK_HOME}/bin/spark-submit.cmd"
},
"linux": {
"pythonPath": "${env:SPARK_HOME}/bin/spark-submit"
},
"program": "${file}",
"cwd": "${workspaceFolder}",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput"
]
},
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"module": "module.name",
"cwd": "${workspaceFolder}",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput"
]
},
{
"name": "Python: Pyramid",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"cwd": "${workspaceFolder}",
"env": {},
"envFile": "${workspaceFolder}/.env",
"args": [
"${workspaceFolder}/development.ini"
],
"debugOptions": [
"RedirectOutput",
"Pyramid"
]
},
{
"name": "Python: Watson",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "${workspaceFolder}/console.py",
"cwd": "${workspaceFolder}",
"args": [
"dev",
"runserver",
"--noreload=True"
],
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput"
]
}
]
}

4
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,4 @@
{
"python.linting.pylintEnabled": false,
"python.pythonPath": "C:\\Users\\ab\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe"
}

16
add.php
View File

@ -13,17 +13,25 @@ if (
} }
else{ else{
$url = $_POST['url']; $url = $_POST['url'];
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) { // check url is valid
if (
filter_var($url, FILTER_VALIDATE_URL) === FALSE ||
!((parse_url($url, PHP_URL_HOST) == 'rutracker.org')) ||
!((parse_url($url, PHP_URL_PATH) == '/forum/viewtopic.php')) ||
!((parse_url($url, PHP_URL_QUERY)))
) {
die('Not a valid URL'); die('Not a valid URL');
} }
parse_str((parse_url($url, PHP_URL_QUERY)), $url); parse_str((parse_url($url, PHP_URL_QUERY)), $url);
if (!isset($url['t']))
{
die('Not a valid URL');
}
$url = $url['t']; $url = $url['t'];
$username = $_POST['telegram']; $username = $_POST['telegram'];
} }
// check url is valid // check if the same user already reqested notify about the same this topic
// check user already reqested notify about this topic
$stmt = $dbh->query( $stmt = $dbh->query(
'SELECT c.username, u.link FROM `contact` c 'SELECT c.username, u.link FROM `contact` c
LEFT JOIN `url` u ON u.id = c.topic_id LEFT JOIN `url` u ON u.id = c.topic_id

View File

@ -1,22 +1,17 @@
<?php <!DOCTYPE html>
<html>
?> <head>
<meta charset="UTF-8">
<!DOCTYPE html> <title>Let me know</title>
<html> </head>
<head> <body>
<meta charset="UTF-8"> <h1>Rutracker notifyer </h1>
<title>Let me know</title> <h3>I will notify you when interesting for you topic will be updated.</h3>
</head> <form action="add.php" method="post">
<label>URL </label><input name="url" type="text"><br>
<body> <label>Telegram </label><input name="telegram" type="text">
<h1>Rutracker notifyer </h1> <input type="submit">
<h3>I will notify you when interesting for you topic will be updated.</h3> </form>
<form action="add.php" method="post"> </body>
<label>URL </label><input name="url" type="text"><br>
<label>Telegram </label><input name="telegram" type="text">
<input type="submit">
</form>
</body>
</html> </html>

45
updater.py Normal file
View File

@ -0,0 +1,45 @@
# TODO - config file.
import pymysql.cursors
import urllib.request, json
import datetime as dt
interval = '1 HOUR'
# Connect to the database
connection = pymysql.connect(host='localhost',
user='root',
db='test',
cursorclass=pymysql.cursors.DictCursor)
# If u_date which already stored older than fresh u_date
# so going to notify
# Any way update last_check
def check_updates(id, u_date):
with urllib.request.urlopen(
"http://api.rutracker.org/v1/get_tor_topic_data?by=topic_id&val="+id) as url:
data = json.loads(url.read().decode())
last_check = dt.datetime.now()
new_u_date = dt.datetime.fromtimestamp(int(data['result'][id]['reg_time']))
if new_u_date > u_date:
print("There is an update. Going to notify.")
with connection.cursor() as cursor:
# Create a new record
sql = "UPDATE url SET last_check = %s, u_date = %s WHERE link = %s"
cursor.execute(sql, (
last_check.strftime('%Y-%m-%d %H:%M:%S'),
new_u_date.strftime('%Y-%m-%d %H:%M:%S'),
id))
connection.commit()
try:
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT * FROM url WHERE last_check < DATE_SUB(NOW(), INTERVAL %s)" % interval
cursor.execute(sql)
result = cursor.fetchall()
for line in result:
print('Going to check %s. Last check was at %s' % (line['link'], line['last_check']))
check_updates(line['link'], line['u_date'])
finally:
connection.close()