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