mirror of
https://github.com/house-of-vanity/tracker_watcher.git
synced 2025-07-07 09:54:08 +00:00
Implemented logger with different log sources.
Lines may be tenanted with user and/or topic.
This commit is contained in:
32
schema.sql
32
schema.sql
@ -1,8 +1,8 @@
|
||||
-- MySQL dump 10.13 Distrib 5.7.20, for Linux (x86_64)
|
||||
-- MySQL dump 10.13 Distrib 5.7.21, for Linux (x86_64)
|
||||
--
|
||||
-- Host: 127.0.0.1 Database: test
|
||||
-- Host: localhost Database: rutracker_bot
|
||||
-- ------------------------------------------------------
|
||||
-- Server version 5.5.5-10.1.29-MariaDB
|
||||
-- Server version 5.7.21-0ubuntu0.16.04.1-log
|
||||
|
||||
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
|
||||
@ -28,7 +28,25 @@ CREATE TABLE `contact` (
|
||||
`user_id` int(20) NOT NULL COMMENT 'Telegram user id',
|
||||
`reg_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'First /start message',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=latin1;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Table structure for table `log`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `log`;
|
||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!40101 SET character_set_client = utf8 */;
|
||||
CREATE TABLE `log` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`logger` varchar(45) DEFAULT 'default',
|
||||
`line` varchar(400) DEFAULT NULL,
|
||||
`time` datetime DEFAULT CURRENT_TIMESTAMP,
|
||||
`link` int(11) DEFAULT '0',
|
||||
`user_id` int(11) DEFAULT '0',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=584 DEFAULT CHARSET=utf8;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
@ -44,7 +62,7 @@ CREATE TABLE `notification` (
|
||||
`user_id` int(20) NOT NULL COMMENT 'user id on telegram',
|
||||
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation date',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=latin1;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
@ -61,7 +79,7 @@ CREATE TABLE `url` (
|
||||
`u_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Last update on rutracker',
|
||||
`last_check` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=latin1;
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=46 DEFAULT CHARSET=latin1;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
|
||||
|
||||
@ -73,4 +91,4 @@ CREATE TABLE `url` (
|
||||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
||||
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
|
||||
|
||||
-- Dump completed on 2018-01-17 15:40:03
|
||||
-- Dump completed on 2018-01-29 17:35:08
|
||||
|
27
updater.py
27
updater.py
@ -20,6 +20,8 @@ interval = '20 MINUTE'
|
||||
connection = pymysql.connect(host=mysql_host,
|
||||
user=mysql_user,
|
||||
db=mysql_db,
|
||||
use_unicode=True,
|
||||
charset="utf8",
|
||||
passwd=mysql_pass,
|
||||
cursorclass=pymysql.cursors.DictCursor)
|
||||
|
||||
@ -34,18 +36,28 @@ def check_updates(id, u_date, cursor):
|
||||
new_u_date = dt.datetime.fromtimestamp(int(data['result'][id]['reg_time']), tz=pytz.utc)
|
||||
u_date = pytz.utc.localize(u_date)
|
||||
if new_u_date > u_date:
|
||||
log(logger='Updater',
|
||||
line='Update found',
|
||||
link=id)
|
||||
sql = "SELECT c.username, c.user_id " \
|
||||
"FROM notification n LEFT JOIN contact c " \
|
||||
"ON n.user_id = c.user_id WHERE n.topic_id = '%s'" % id
|
||||
cursor.execute(sql)
|
||||
result = cursor.fetchall()
|
||||
for contact in result:
|
||||
print(contact)
|
||||
log(logger='Updater',
|
||||
line='Notifying user',
|
||||
user_id=contact['user_id'],
|
||||
link=id)
|
||||
msg = "%s has been updated.\n[Open on RuTracker.org](%s)\n`magnet:?xt=urn:%s`" % (
|
||||
data['result'][id]['topic_title'],
|
||||
'https://rutracker.org/forum/viewtopic.php?t='+id,
|
||||
data['result'][id]['info_hash'])
|
||||
send(contact['user_id'], msg)
|
||||
else:
|
||||
log(logger='Updater',
|
||||
line='There is not update',
|
||||
link=id)
|
||||
|
||||
|
||||
with connection.cursor() as cursor:
|
||||
@ -57,7 +69,17 @@ def check_updates(id, u_date, cursor):
|
||||
id))
|
||||
connection.commit()
|
||||
|
||||
def log(line, logger='Updater', user_id=0, link=0):
|
||||
global connection
|
||||
with connection.cursor() as cursor:
|
||||
sql = "INSERT INTO log (logger, line, link, user_id) VALUES ('%s', '%s', '%s', '%s')" % (logger, line, link, user_id)
|
||||
cursor.execute(sql)
|
||||
|
||||
|
||||
def send(id, msg):
|
||||
log(logger='Updater',
|
||||
line='Senging message for %s. Body: %s' % (id, msg),
|
||||
user_id=id)
|
||||
url = parser.get('bot', 'telegram_api') + 'bot'+ parser.get('bot', 'telegram_key') + '/sendMessage'
|
||||
post_fields = {
|
||||
'text': msg,
|
||||
@ -68,6 +90,8 @@ def send(id, msg):
|
||||
request = urllib.request.Request(url, urlencode(post_fields).encode())
|
||||
json = urllib.request.urlopen(request).read().decode()
|
||||
|
||||
log(logger='Updater',
|
||||
line='Updater started bt CRON.')
|
||||
try:
|
||||
with connection.cursor() as cursor:
|
||||
# Read a single record
|
||||
@ -80,3 +104,4 @@ try:
|
||||
check_updates(line['link'], line['u_date'], cursor)
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
|
@ -4,7 +4,8 @@ $config = parse_ini_file("../settings.ini");
|
||||
$dbh = new PDO(
|
||||
'mysql:host='.$config['mysql_host'].';dbname='.$config['mysql_db'],
|
||||
$config['mysql_user'],
|
||||
$config['mysql_pass']
|
||||
$config['mysql_pass'],
|
||||
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
|
||||
);
|
||||
|
||||
$request = file_get_contents('php://input');
|
||||
@ -12,9 +13,9 @@ $request = json_decode( $request, TRUE );
|
||||
|
||||
function register(){
|
||||
global $dbh;
|
||||
global $config;
|
||||
global $user_id;
|
||||
global $username;
|
||||
global $message;
|
||||
$stmt = $dbh->query(
|
||||
'SELECT id FROM `contact`
|
||||
WHERE user_id = "'.$user_id.'"'
|
||||
@ -26,6 +27,7 @@ function register(){
|
||||
VALUES ("'.$username.'","'.$user_id.'")'
|
||||
);
|
||||
send('Hello! Send me an URL to rutracker.org topic and i will notify you when topic will be updated.');
|
||||
logger('User registered', 'Webhook');
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -34,9 +36,20 @@ function register(){
|
||||
}
|
||||
}
|
||||
|
||||
function logger($line, $logger = 'Webhook'){
|
||||
global $dbh;
|
||||
global $user_id;
|
||||
global $username;
|
||||
$stmt = $dbh->query(
|
||||
'INSERT INTO log (logger, line, user_id)
|
||||
VALUES ("'.$logger.'","'.$line.'", "'.$user_id.'")'
|
||||
);
|
||||
}
|
||||
|
||||
function send($msg, $die = FALSE){
|
||||
global $config;
|
||||
global $user_id;
|
||||
logger('Message sent. Body: '.$msg, 'Webhook');
|
||||
$url = $config['telegram_api'].'bot'.$config['telegram_key'].'/sendMessage';
|
||||
$data = array(
|
||||
'chat_id' => $user_id,
|
||||
@ -197,6 +210,7 @@ else
|
||||
#$user_id = '124317807';
|
||||
#$message = 'https://rutracker.org/forum/viewtopic.php?t=5505520';
|
||||
$username = $request['message']['from']['username'];
|
||||
logger('Message from user. Body: '.$message, 'Webhook');
|
||||
|
||||
|
||||
if(!(filter_var($message, FILTER_VALIDATE_URL) === FALSE)) {
|
||||
|
Reference in New Issue
Block a user