#!/usr/bin/python -O
# MTLBot - Magnet Torrent List Bot
# An IRC bot to search magnet bittorrent links.
#
# Instructions:
# If you run the program without any parameters it will connect to IRC and
# start working as a bot. You first need to build a database of magnet links.
# You can do this by passing a text file containing hash|name pairs.
# The format of the files is important, it must be hash|name where hash is
# the magnet hash and name is the name of the torrent. It must use '|' as a
# separator character. Each entry needs to be on a new line.
#
# ex: `python mtlbot.py complete_fixed.txt`
# This will add the magnet links from complete_fixed.txt to the database.
#
# Magnet link resources:
# 18m torrents: magnet:?xt=urn:btih:F5615DFB80AC995787C1B2219A75DF7805278DEA
# Comes in the correct format, can import with the text files easily.
# TPB Mirror: magnet:?xt=urn:btih:938802790a385c49307f34cca4c30f80b03df59c
# A complete mirror of The Pirate Bay. You need to run awk on the file to
# get it into the correct format. The command below will format it for
# you.
#
# `awk -F\| '{print $6 "|" $2}' complete > complete_fixed.txt`
#
# To change the bot name/irc server it joins, just change the __NICK__ and
# __IRC_SERVER__ variables. You can change the number of results returned near
# the end of the file. There is a comment that says 'Change limit here'. Default
# is 5. You can load multiple text files with the following command:
#
# `for f in folder_that_has_text_files/*.txt; do python mtlbot.py "$f"; done`
#
# Written by: Jubbs sha1(8af93afca276eb89e7ebc03cd87b0c9ca15fa1c6)
import sys
import socket
import string
import time
from sqlite3 import dbapi2 as sqlite
__IRC_SERVER__ = "irc.freenode.net"
__NICK__ = __IDENT__ = __REALNAME__ = "MTLBot"
conn = sqlite.connect("torrentlist.db")
cur = conn.cursor()
if len(sys.argv) > 1:
print 'Loading torrent magnet data from: ', sys.argv[1]
print 'Note: Must be in form HASH|NAME\n'
try:
cur.execute('create table magnet_list (name varchar, hash varchar);')
cur.execute('create index search_index on magnet_list (name);')
print 'Database has not been created, creating database.'
conn.commit()
print 'Database created, adding new data.'
except:
print 'Database has already been created, adding new data.'
num_lines = sum(1 for line in open(sys.argv[1]))
f = open(sys.argv[1],'r')
count = 0
for l in f.readlines():
name = buffer(l[l.find('|') + 1:].rstrip())
hash = buffer(l[:l.find('|')])
cur.execute('insert into magnet_list values (?,?)',[name, hash])
count += 1
if count % 10000 == 0:
percent = float(count)/num_lines*100
print '%.2f%% Added %d records so far.' % (percent, count)
conn.commit()
cur.close()
exit()
print 'Counting number of torrents in database.'
cur.execute("select count(1) from magnet_list;")
torrent_count = cur.fetchone()
print '%d torrents in the database.' % (torrent_count)
s = socket.socket( )
s.connect((__IRC_SERVER__, 6667))
s.send("NICK %s\r\n" % __NICK__)
s.send("USER %s %s bla :%s\r\n" % (__IDENT__, __IRC_SERVER__, __REALNAME__))
readbuffer = ''
while 1:
readbuffer = readbuffer+s.recv(1024)
temp = string.split(readbuffer, "\n")
readbuffer = temp.pop( )
for line in temp:
line=string.rstrip(line)
line=string.split(line)
if(line[1]=="PRIVMSG"):
sender = line[0][1:line[0].find('!')]
query = []
query.append(line[3][1:])
for i in range(1,len(line)-3):
query.append(line[3+i])
if query[0] != '.find' or len(query) == 1:
s.send("PRIVMSG %s :MTLBot - Magnet Torrent List Bot\r\n"
% (sender))
s.send("PRIVMSG %s :An IRC bot to search magnet torrent links.\
\r\n" % (sender))
s.send("PRIVMSG %s : Number of torrents in database: %d\
\r\n" % (sender, torrent_count[0]))
s.send("PRIVMSG %s : Usage: .find KEY WORDS\r\n" % (sender))
s.send("PRIVMSG %s : ex: .find Ubuntu 11.04\r\n" % (sender))
else:
bindings = '%'.join(query[1:]) + '%'
print 'Received request from %s for %s' % (sender, bindings)
t0 = time.time()
# Change limit below
cur.execute("select name, hash from magnet_list\
where name like (?) limit 5;",
[bindings])
print '\t %.2f seconds' % (time.time() - t0)
print '\tFinished execution.'
results = cur.fetchall()
if len(results) == 0:
s.send("PRIVMSG %s :No results found, sorry.\r\n"
% (sender))
else:
s.send("PRIVMSG %s :Result(s) found: %d\r\n"
% (sender, len(results)))
for row in results:
s.send("PRIVMSG %s :%s = magnet:?xt=urn:btih:%s\r\n"
% (sender, row[0], row[1]))
if(line[0]=="PING"):
s.send("PONG %s\r\n" % line[1])