Deletion implemented.

This commit is contained in:
UltraDesu
2018-01-17 21:21:46 +03:00
parent f1fbeb710e
commit 4f1ea4de2d
2 changed files with 94 additions and 21 deletions

View File

@ -16,7 +16,7 @@ mysql_db = parser.get('mysql', 'mysql_db')
mysql_pass = parser.get('mysql', 'mysql_pass') mysql_pass = parser.get('mysql', 'mysql_pass')
interval = '1 HOUR' interval = '1 HOUR'
interval = '1 MINUTE' #interval = '1 MINUTE'
# Connect to the database # Connect to the database
connection = pymysql.connect(host=mysql_host, connection = pymysql.connect(host=mysql_host,
user=mysql_user, user=mysql_user,
@ -60,7 +60,8 @@ def send(id, msg):
url = parser.get('bot', 'telegram_api') + 'bot'+ parser.get('bot', 'telegram_key') + '/sendMessage' url = parser.get('bot', 'telegram_api') + 'bot'+ parser.get('bot', 'telegram_key') + '/sendMessage'
post_fields = { post_fields = {
'text': msg, 'text': msg,
'chat_id': id 'chat_id': id,
'parse_mode': 'Markdown'
} }
request = urllib.request.Request(url, urlencode(post_fields).encode()) request = urllib.request.Request(url, urlencode(post_fields).encode())
json = urllib.request.urlopen(request).read().decode() json = urllib.request.urlopen(request).read().decode()
@ -69,7 +70,7 @@ try:
with connection.cursor() as cursor: with connection.cursor() as cursor:
# Read a single record # Read a single record
sql = "SELECT * FROM url WHERE last_check < DATE_SUB(NOW(), INTERVAL %s)" % interval sql = "SELECT * FROM url WHERE last_check < DATE_SUB(NOW(), INTERVAL %s)" % interval
sql = "SELECT * FROM url" #sql = "SELECT * FROM url"
cursor.execute(sql) cursor.execute(sql)
result = cursor.fetchall() result = cursor.fetchall()
for line in result: for line in result:

View File

@ -25,19 +25,21 @@ function register(){
} }
else else
{ {
send('Send me an URL to rutracker.org topic.'); send('Send me an URL to rutracker.org topic and i will notify you when it will be updated.
die(); /stop to remove notification.
/list to show all notifications.', $die = TRUE);
} }
} }
function send($msg, $die = FALSE){
function send($msg, $die = FALSE) {
global $config; global $config;
global $user_id; global $user_id;
$url = $config['telegram_api'].'bot'.$config['telegram_key'].'/sendMessage'; $url = $config['telegram_api'].'bot'.$config['telegram_key'].'/sendMessage';
$data = array( $data = array(
'chat_id' => $user_id, 'chat_id' => $user_id,
'text' => $msg 'text' => $msg,
'parse_mode' => 'Markdown',
disable_web_page_preview => '1'
); );
$options = array( $options = array(
'http' => array( 'http' => array(
@ -66,14 +68,10 @@ function notify($url){
if (!isset($url['t'])) if (!isset($url['t']))
{ {
send('URL is invalid.', $die = TRUE); send('URL is invalid.', $die = TRUE);
die();
} }
$url = $url['t']; $url = $url['t'];
$json = file_get_contents( $obj = f_info($url);
'http://api.rutracker.org/v1/get_tor_topic_data?by=topic_id&val='.$url
);
$obj = json_decode($json);
$stmt = $dbh->query( $stmt = $dbh->query(
'SELECT * FROM notification n 'SELECT * FROM notification n
LEFT JOIN contact c ON n.user_id = c.user_id LEFT JOIN contact c ON n.user_id = c.user_id
@ -81,9 +79,11 @@ function notify($url){
); );
if($stmt->rowCount() > 0) 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{ }else{
send('You will be subscribed to '.$obj->result->{$url}->topic_title); send('You will be subscribed to
'.$obj->topic_title);
$stmt = $dbh->query( $stmt = $dbh->query(
'SELECT * FROM `url` 'SELECT * FROM `url`
WHERE link = "'.$url.'"' WHERE link = "'.$url.'"'
@ -102,7 +102,7 @@ function notify($url){
$stmt = $dbh->query( $stmt = $dbh->query(
'INSERT into url (link,u_date) '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( $stmt = $dbh->query(
'INSERT into notification (user_id, topic_id) 'INSERT into notification (user_id, topic_id)
@ -111,11 +111,71 @@ function notify($url){
} }
} }
}else{ }else{
send('URL is invalid. Only rutracker supported right now.', $die = TRUE); 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 ) if( !$request )
@ -131,13 +191,25 @@ else
$chatId = $request['message']['chat']['id']; $chatId = $request['message']['chat']['id'];
$message = $request['message']['text']; $message = $request['message']['text'];
$user_id = $request['message']['from']['id']; $user_id = $request['message']['from']['id'];
//$user_id = '124317807'; #$user_id = '124317807';
//$message = 'https://rutracker.org/forum/viewtopic.php?t=5505520'; #$message = 'https://rutracker.org/forum/viewtopic.php?t=5505520';
$username = $request['message']['from']['username']; $username = $request['message']['from']['username'];
if(!(filter_var($message, FILTER_VALIDATE_URL) === FALSE)) { if(!(filter_var($message, FILTER_VALIDATE_URL) === FALSE)) {
#send('Recognized as an URL'); #send('Recognized as an URL');
notify($message); 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{ }else{
register(); register();
} }