From 4f1ea4de2d14bd7ccfc1b06fa7b15f79c244c4ef Mon Sep 17 00:00:00 2001 From: UltraDesu Date: Wed, 17 Jan 2018 21:21:46 +0300 Subject: [PATCH] Deletion implemented. --- updater.py | 7 +-- webhook/index.php | 108 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 94 insertions(+), 21 deletions(-) diff --git a/updater.py b/updater.py index 0c713ef..0f11407 100644 --- a/updater.py +++ b/updater.py @@ -16,7 +16,7 @@ mysql_db = parser.get('mysql', 'mysql_db') mysql_pass = parser.get('mysql', 'mysql_pass') interval = '1 HOUR' -interval = '1 MINUTE' +#interval = '1 MINUTE' # Connect to the database connection = pymysql.connect(host=mysql_host, user=mysql_user, @@ -60,7 +60,8 @@ def send(id, msg): url = parser.get('bot', 'telegram_api') + 'bot'+ parser.get('bot', 'telegram_key') + '/sendMessage' post_fields = { 'text': msg, - 'chat_id': id + 'chat_id': id, + 'parse_mode': 'Markdown' } request = urllib.request.Request(url, urlencode(post_fields).encode()) json = urllib.request.urlopen(request).read().decode() @@ -69,7 +70,7 @@ try: with connection.cursor() as cursor: # Read a single record sql = "SELECT * FROM url WHERE last_check < DATE_SUB(NOW(), INTERVAL %s)" % interval - sql = "SELECT * FROM url" + #sql = "SELECT * FROM url" cursor.execute(sql) result = cursor.fetchall() for line in result: diff --git a/webhook/index.php b/webhook/index.php index 7fac817..884f34e 100644 --- a/webhook/index.php +++ b/webhook/index.php @@ -25,19 +25,21 @@ function register(){ } else { - send('Send me an URL to rutracker.org topic.'); - die(); + send('Send me an URL to rutracker.org topic and i will notify you when it will be updated. +/stop to remove notification. +/list to show all notifications.', $die = TRUE); } } - -function send($msg, $die = FALSE) { +function send($msg, $die = FALSE){ global $config; global $user_id; $url = $config['telegram_api'].'bot'.$config['telegram_key'].'/sendMessage'; $data = array( 'chat_id' => $user_id, - 'text' => $msg + 'text' => $msg, + 'parse_mode' => 'Markdown', + disable_web_page_preview => '1' ); $options = array( 'http' => array( @@ -66,14 +68,10 @@ function notify($url){ if (!isset($url['t'])) { send('URL is invalid.', $die = TRUE); - die(); } $url = $url['t']; - $json = file_get_contents( - 'http://api.rutracker.org/v1/get_tor_topic_data?by=topic_id&val='.$url - ); - $obj = json_decode($json); + $obj = f_info($url); $stmt = $dbh->query( 'SELECT * FROM notification n LEFT JOIN contact c ON n.user_id = c.user_id @@ -81,9 +79,11 @@ function notify($url){ ); if($stmt->rowCount() > 0) { - send('You already subscribed to '.$obj->result->{$url}->topic_title, $die = TRUE); + send('You already subscribed to +'.$obj->topic_title, $die = TRUE); }else{ - send('You will be subscribed to '.$obj->result->{$url}->topic_title); + send('You will be subscribed to +'.$obj->topic_title); $stmt = $dbh->query( 'SELECT * FROM `url` WHERE link = "'.$url.'"' @@ -102,7 +102,7 @@ function notify($url){ $stmt = $dbh->query( 'INSERT into url (link,u_date) - VALUES ("'.$url.'","'.gmdate("Y-m-d H:i:s", $obj->result->{$url}->reg_time).'")' + VALUES ("'.$url.'","'.gmdate("Y-m-d H:i:s", $obj->reg_time).'")' ); $stmt = $dbh->query( 'INSERT into notification (user_id, topic_id) @@ -111,11 +111,71 @@ function notify($url){ } } - }else{ send('URL is invalid. Only rutracker supported right now.', $die = TRUE); } +} +function list_url(){ + global $dbh; + global $config; + global $user_id; + + $stmt = $dbh->query( + 'SELECT c.username, c.user_id, n.topic_id + FROM notification n LEFT JOIN contact c + ON n.user_id=c.user_id where c.user_id="'.$user_id.'"' + ); + $msg = '*Your notifications:* + '; + $i = 1; + while ($row = $stmt->fetch()) + { + $info = f_info($row['topic_id']); + $url = 'https://rutracker.org/forum/viewtopic.php?t='.$row['topic_id']; + $msg = $msg."*".$i.")* ".$info->topic_title." + [Open topic](".$url.') +Stop notification - /stop'.$row['topic_id'].' +`__________________________` + '; + $i++; + } + send($msg); +} + +function stop($topic_id){ + global $dbh; + global $config; + global $user_id; + + $stmt = $dbh->query( + 'SELECT * FROM notification n + LEFT JOIN contact c + ON c.user_id = n.user_id + WHERE n.topic_id = "'.$topic_id.'" + AND c.user_id = "'.$user_id.'"' + ); + if($stmt->rowCount() > 0){ + $stmt = $dbh->query( + 'DELETE FROM notification WHERE + user_id = "'.$user_id.'" + AND topic_id = "'.$topic_id.'"' + ); + send('*Notification stoped for:* +'.f_info($topic_id)->topic_title); + }else{ + send('It is not yours.'); + } + +} + +// fetch info about topic +function f_info($topic_id){ + $json = file_get_contents( + 'http://api.rutracker.org/v1/get_tor_topic_data?by=topic_id&val='.$topic_id + ); + $obj = json_decode($json); + return($obj->result->{$topic_id}); } if( !$request ) @@ -131,15 +191,27 @@ else $chatId = $request['message']['chat']['id']; $message = $request['message']['text']; $user_id = $request['message']['from']['id']; - //$user_id = '124317807'; - //$message = 'https://rutracker.org/forum/viewtopic.php?t=5505520'; + #$user_id = '124317807'; + #$message = 'https://rutracker.org/forum/viewtopic.php?t=5505520'; $username = $request['message']['from']['username']; - + + if(!(filter_var($message, FILTER_VALIDATE_URL) === FALSE)) { #send('Recognized as an URL'); notify($message); + } + elseif($message == '/list'){ + list_url(); + }elseif(substr($message, 0, 5) == '/stop'){ + if (is_numeric(substr($message, 5))) + { + stop(substr($message, 5)); + + }else{ + send('Give me ID'); + } }else{ - register(); + register(); } } ?> \ No newline at end of file