Advertisement
Guest User

Untitled

a guest
Feb 12th, 2012
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.15 KB | None | 0 0
  1. #!/usr/bin/python -O
  2.  
  3. # MTLBot - Magnet Torrent List Bot
  4. # An IRC bot to search magnet bittorrent links.
  5. #
  6. # Instructions:
  7. #   If you run the program without any parameters it will connect to IRC and
  8. #   start working as a bot. You first need to build a database of magnet links.
  9. #   You can do this by passing a text file containing hash|name pairs.
  10. #   The format of the files is important, it must be hash|name where hash is
  11. #   the magnet hash and name is the name of the torrent. It must use '|' as a
  12. #   separator character. Each entry needs to be on a new line.
  13. #
  14. #   ex: `python mtlbot.py complete_fixed.txt`
  15. #   This will add the magnet links from complete_fixed.txt to the database.
  16. #
  17. # Magnet link resources:
  18. #   18m torrents: magnet:?xt=urn:btih:F5615DFB80AC995787C1B2219A75DF7805278DEA
  19. #       Comes in the correct format, can import with the text files easily.
  20. #   TPB Mirror: magnet:?xt=urn:btih:938802790a385c49307f34cca4c30f80b03df59c
  21. #       A complete mirror of The Pirate Bay. You need to run awk on the file to
  22. #       get it into the correct format. The command below will format it for
  23. #       you.
  24. #
  25. #       `awk -F\| '{print $6 "|" $2}' complete > complete_fixed.txt`
  26. #
  27. # To change the bot name/irc server it joins, just change the __NICK__ and
  28. # __IRC_SERVER__ variables. You can change the number of results returned near
  29. # the end of the file. There is a comment that says 'Change limit here'. Default
  30. # is 5. You can load multiple text files with the following command:
  31. #
  32. #   `for f in folder_that_has_text_files/*.txt; do python mtlbot.py "$f"; done`
  33. #
  34. # Written by: Jubbs sha1(8af93afca276eb89e7ebc03cd87b0c9ca15fa1c6)
  35.  
  36. import sys
  37. import socket
  38. import string
  39. import time
  40. from sqlite3 import dbapi2 as sqlite
  41.  
  42. __IRC_SERVER__ = "irc.freenode.net"
  43. __NICK__ = __IDENT__ = __REALNAME__ = "MTLBot"
  44. conn = sqlite.connect("torrentlist.db")
  45. cur = conn.cursor()
  46.  
  47. if len(sys.argv) > 1:
  48.    print 'Loading torrent magnet data from: ', sys.argv[1]
  49.    print 'Note: Must be in form HASH|NAME\n'
  50.    try:
  51.       cur.execute('create table magnet_list (name varchar, hash varchar);')
  52.       cur.execute('create index search_index on magnet_list (name);')
  53.       print 'Database has not been created, creating database.'
  54.       conn.commit()
  55.       print 'Database created, adding new data.'
  56.    except:
  57.       print 'Database has already been created, adding new data.'
  58.    num_lines = sum(1 for line in open(sys.argv[1]))
  59.    f = open(sys.argv[1],'r')
  60.    count = 0
  61.    for l in f.readlines():
  62.       name = buffer(l[l.find('|') + 1:].rstrip())
  63.       hash = buffer(l[:l.find('|')])
  64.       cur.execute('insert into magnet_list values (?,?)',[name, hash])
  65.       count += 1
  66.       if count % 10000 == 0:
  67.          percent = float(count)/num_lines*100
  68.          print '%.2f%% Added %d records so far.' % (percent, count)
  69.    conn.commit()
  70.    cur.close()
  71.    exit()
  72.  
  73. print 'Counting number of torrents in database.'
  74. cur.execute("select count(1) from magnet_list;")
  75. torrent_count = cur.fetchone()
  76. print '%d torrents in the database.' % (torrent_count)
  77.  
  78. s = socket.socket( )
  79. s.connect((__IRC_SERVER__, 6667))
  80. s.send("NICK %s\r\n" % __NICK__)
  81. s.send("USER %s %s bla :%s\r\n" % (__IDENT__, __IRC_SERVER__, __REALNAME__))
  82. readbuffer = ''
  83.  
  84. while 1:
  85.    readbuffer = readbuffer+s.recv(1024)
  86.    temp = string.split(readbuffer, "\n")
  87.    readbuffer = temp.pop( )
  88.  
  89.    for line in temp:
  90.       line=string.rstrip(line)
  91.       line=string.split(line)
  92.  
  93.       if(line[1]=="PRIVMSG"):
  94.          sender = line[0][1:line[0].find('!')]
  95.          query = []
  96.          query.append(line[3][1:])
  97.  
  98.          for i in range(1,len(line)-3):
  99.             query.append(line[3+i])
  100.  
  101.          if query[0] != '.find' or len(query) == 1:
  102.             s.send("PRIVMSG %s :MTLBot - Magnet Torrent List Bot\r\n"
  103.                   % (sender))
  104.             s.send("PRIVMSG %s :An IRC bot to search magnet torrent links.\
  105.                  \r\n" % (sender))
  106.             s.send("PRIVMSG %s : Number of torrents in database: %d\
  107.                  \r\n" % (sender, torrent_count[0]))
  108.             s.send("PRIVMSG %s : Usage: .find KEY WORDS\r\n" % (sender))
  109.             s.send("PRIVMSG %s : ex: .find Ubuntu 11.04\r\n" % (sender))
  110.          else:
  111.             bindings = '%'.join(query[1:]) + '%'
  112.             print 'Received request from %s for %s' % (sender, bindings)
  113.             t0 = time.time()
  114.             # Change limit below
  115.             cur.execute("select name, hash from magnet_list\
  116.                     where name like (?) limit 5;",
  117.                      [bindings])
  118.             print '\t %.2f seconds' % (time.time() - t0)
  119.             print '\tFinished execution.'
  120.             results = cur.fetchall()
  121.  
  122.             if len(results) == 0:
  123.                s.send("PRIVMSG %s :No results found, sorry.\r\n"
  124.                      % (sender))
  125.             else:
  126.                s.send("PRIVMSG %s :Result(s) found: %d\r\n"
  127.                      % (sender, len(results)))
  128.                for row in results:
  129.                   s.send("PRIVMSG %s :%s = magnet:?xt=urn:btih:%s\r\n"
  130.                         % (sender, row[0], row[1]))
  131.  
  132.       if(line[0]=="PING"):
  133.          s.send("PONG %s\r\n" % line[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement