· 9 years ago · Dec 10, 2016, 04:13 PM
1import telegram
2import time
3import feedparser
4import threading
5import sqlite3
6from telegram.ext import Updater
7from telegram.ext import CommandHandler
8#from telegram import Updater
9
10token = '146326897:AAH71FGRf37o42PSrk-Zoi1BjMcroOYaDYQ'
11updater = Updater(token)
12dispatcher = updater.dispatcher
13bot = telegram.Bot(token)
14updates = bot.getUpdates()
15
16handler = sqlite3.connect('users.db')
17c = handler.cursor()
18c.execute('''
19 CREATE TABLE IF NOT EXISTS `users` (
20 `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,
21 `chat_id` INTEGER NOT NULL);
22''')
23c.close()
24try:
25 feed = feedparser.parse( 'http://www.nyaa.se/?page=rss&cats=1_11' )
26except IndexError:
27 time.sleep(60)
28#print(feed)
29
30countMessage = 0
31
32def postThread():
33 global countMessage
34 while True:
35 handler = sqlite3.connect('users.db')
36 c = handler.cursor()
37 try:
38 newfeed = feedparser.parse( 'http://www.nyaa.se/?page=rss&cats=1_11' )
39 print('New Anime: '+ str(newfeed['entries'][0]['title']))
40 print('Old Anime: '+ feed['entries'][0]['title'])
41 if(feed['entries'][0]['title'] != newfeed['entries'][0]['title']):
42 #bot.sendMessage(update.message.chat_id, newfeed['entries'][0]['title'])
43 #print('a')
44 for i in range(100):
45 if(newfeed['entries'][i]['title'] == feed['entries'][0]['title']):
46 #print(newfeed['entries'][i]['title'])
47 #print(str(i))
48 for k in range(i,0,-1):
49 k=k-1
50 #print(str(k))
51 #print(countMessage)
52 for row in c.execute('SELECT chat_id FROM users;'):
53 #print('rowit')
54 if row != None:
55 print('fixed?')
56 if countMessage <= 25:
57 print(row[0])
58 bot.sendMessage(row[0], 'Name: '+newfeed['entries'][k]['title']+'\nLink: '+ newfeed['entries'][k]['link'])
59 countMessage = countMessage + 1
60 feed['entries'][0]['title'] = newfeed['entries'][0]['title']
61 #print('count'+str(countMessage))
62 else:
63 bot.sendMessage(row[0], 'Name: '+newfeed['entries'][k]['title']+'\nLink: '+ newfeed['entries'][k]['link'])
64 time.sleep(1)
65 countMessage = 0;
66 feed['entries'][0]['title'] = newfeed['entries'][0]['title']
67 except IndexError:
68 time.sleep(120)
69 #TODO: PARSING ALGHORITM
70 #bot.sendMessage(1618685,"Ok!Test")
71 c.close();
72
73 time.sleep(120)
74t1 = threading.Thread(target=postThread, args=())
75t1.start()
76
77
78##############################
79# THIS IS START COMMAND #
80##############################
81def start(bot, update):
82 #print (update.message.chat_id)
83 #myid = 'Your id'+update.message.chat_id
84 handler = sqlite3.connect('users.db')
85 c = handler.cursor()
86 userid = update.message.chat_id
87 c.execute('SELECT * FROM users WHERE chat_id='+str(userid)+';')
88 if c.fetchone() == None:
89 c.execute('INSERT INTO users VALUES(NULL, '+str(userid)+');')
90 handler.commit()
91 bot.sendMessage(userid,'''Hi! Welcome to my nyaa parser\n
92Now you subscribe on automatic distribution\n
93If you want unsubscribe enter a command /unsubscribe
94 ''')
95 print(userid+" register in my system!")
96 else:
97 bot.sendMessage(userid,'''Now you subscribe on automatic distribution\n
98If you want unsubscribe enter a command /unsubscribe
99 ''')
100 #handler.commit()
101 #print(update.message.chat_id)
102 c.close();
103
104def version(bot, update):
105 bot.sendSticker(update.message.chat_id, open('sticker.webp', 'rb'))
106 bot.sendMessage(update.message.chat_id, 'Version: 0.0.1\nAll reports or questions request to @disinterpreter')
107
108def helpinfo(bot, update):
109 bot.sendMessage(update.message.chat_id, '''
110/start - Start and subscribe\n
111/subscribe - Subscribe on mass mailing\n
112/unsubscribe - Unsubscribe on mass mailing\n
113/help - Show all commands\n
114/version - Show version\n
115 ''')
116
117##############################
118# THIS IS UNSUBSCRIBE COMMAND#
119##############################
120def unsubscribe(bot, update):
121 handler = sqlite3.connect('users.db')
122 c = handler.cursor()
123 userid = update.message.chat_id
124 c.execute('SELECT * FROM users WHERE chat_id='+str(userid)+';')
125 if c.fetchone() == None:
126 bot.sendMessage(userid,'''You hasn't subscribe.
127 ''')
128 else:
129 c.execute('DELETE FROM users WHERE chat_id='+str(userid)+';')
130 handler.commit()
131 bot.sendMessage(userid,'''You have successfully unsubscribed.\n
132If you want subscribe enter a command /start
133 ''')
134 #handler.commit()
135 #print(update.message.chat_id)
136 c.close();
137unsub = CommandHandler('unsubscribe', unsubscribe)
138sub = CommandHandler('subscribe', start)
139start = CommandHandler('start', start)
140ver = CommandHandler('version', version)
141help = CommandHandler('help', helpinfo)
142dispatcher.add_handler(unsub)
143dispatcher.add_handler(sub)
144dispatcher.add_handler(start)
145dispatcher.add_handler(ver)
146dispatcher.add_handler(help)
147updater.start_polling()