Anmelden Register

Themabewertung:
  • 0 Bewertung(en) - 0 im Durchschnitt
  • 1
  • 2
  • 3
  • 4
  • 5
Charon: IRC-RSS-Bot
#1
Ich hab einen IRC-Bot geschrieben, welcher alle 10min den Minecraft-Feed vom Forum ausliest und neue Threads im Chat (#minecraft) postet. Den Source werd ich demnächst hier veröffentlichen.

Kritik/Anregungen?
#2
Code:
#!/usr/bin/python2
#
# Licensed under the GNU General Public License v3
#

# Abhängigkeiten:
import irclib
import feedparser

import os
import threading
import time
import urllib2

### Einstellungen

network = 'irc.example.com'
port = 6667
channel = '#chan'
nick = 'Botname'
name = 'OwnernamesBot'

FEEDLINK = 'http://uhuc.de/syndication.php?type=atom1.0&fid=25'
old_entries_file = os.getcwd() + "/old-feed-entries"

PARSETIMER = 60 # 1 = 10sek

###

keepRunning = True

msgqueue = []


def read_feed():
    print ("readfeed")
    FILE = open( old_entries_file, "r" )
    filetext = FILE.read()
    FILE.close()    

    d = feedparser.parse(FEEDLINK)

    for entry in d.entries:
        ident = entry.link.encode('utf-8')+entry.title.encode('utf-8')
        if ident in filetext:
            pass
        else:
            print("")
            FILE = open ( old_entries_file, "a")
            FILE.write(ident+"\n")
            FILE.close()
        
            surl = shorturl( entry.link.encode('utf-8') )
            msgqueue.append( "%s ( %s )" % (entry.title.encode('utf-8'), surl ) )



#irclib.DEBUG = True


def shorturl(url):
    api = "http://s.uhuc.de/yourls-api.php?action=shorturl&format=simple&url="+url
    response = urllib2.urlopen(api)
    html = response.read()
    return html




def on_connect(connection, event):
    if irclib.is_channel(channel):
        connection.join(channel)
    else:
        while 1:
            line = sys.stdin.readline()
            if not line:
                break
            connection.privmsg(channel, line)
        connection.quit("Charon out...")


def handleEcho ( connection, event ):
    pass
    #print ' '.join ( event.arguments() )


irc = irclib.IRC()

# Register handlers
irc.add_global_handler ( 'privnotice', handleEcho ) # Private notice
#irc.add_global_handler ( 'welcome', handleEcho ) # Welcome message
irc.add_global_handler ( 'yourhost', handleEcho ) # Host message
irc.add_global_handler ( 'created', handleEcho ) # Server creation message
irc.add_global_handler ( 'myinfo', handleEcho ) # "My info" message
irc.add_global_handler ( 'featurelist', handleEcho ) # Server feature list
irc.add_global_handler ( 'luserclient', handleEcho ) # User count
irc.add_global_handler ( 'luserop', handleEcho ) # Operator count
irc.add_global_handler ( 'luserchannels', handleEcho ) # Channel count
irc.add_global_handler ( 'luserme', handleEcho ) # Server client count
irc.add_global_handler ( 'n_local', handleEcho ) # Server client count/maximum
irc.add_global_handler ( 'n_global', handleEcho ) # Network client count/maximum
irc.add_global_handler ( 'luserconns', handleEcho ) # Record client count
irc.add_global_handler ( 'luserunknown', handleEcho ) # Unknown connections
irc.add_global_handler ( 'motdstart', handleEcho ) # Message of the day ( start )
irc.add_global_handler ( 'motd', handleEcho ) # Message of the day
irc.add_global_handler ( 'edofmotd', handleEcho ) # Message of the day ( end )
irc.add_global_handler ( 'join', handleEcho ) # Channel join
irc.add_global_handler ( 'namreply', handleEcho ) # Channel name list
irc.add_global_handler ( 'endofnames', handleEcho ) # Channel name list ( end )
irc.add_global_handler ("welcome", on_connect)


server = irc.server()
server.connect ( network, port, nick, ircname = name )


counter = 0
while keepRunning:
    try:
        while len(msgqueue) > 0:
            msg = msgqueue.pop()
            server.privmsg( channel, msg )
        print("still running...")
        irc.process_once()
        time.sleep(10) # Damit die CPU nicht überlastet wird

        counter += 1
        if counter >= PARSETIMER:
            counter = 0
            read_feed()

    except KeyboardInterrupt:
        print ("Shutting Down")
        keepRunning = False

Ich hab noch vor zu implementieren, das er in mehreren Chans gleichzeitig ist aber dort auch unterschiedliche Feeds postet. Zudem wollt ich noch implementieren, das er bei jedem Forumspost eine Nachricht absetzten kann. Aber das wüsste ich noch nicht umzusetzten.
#3
wäre es möglich sowas auch für minecraft zu machen ? über die konsole
#4
Es ist eine Bridge zwischen #minecraft und in-game-chat geplant. Diese würde dann alles gesagte vom IRC in den GameChat übertragen und vice versa.
#5
das wäre cool
#6
Ich hab Charon komplett überarbeitet. Für die Kommunikation mit dem IRC-Server wird jetzt http://python-irclib.sourceforge.net/ verwendet. Charon2 prüft per cron alle 10 mins nach neuen Threads. Um den neuen zu posten, verbindet er sich für den Post in den Channel und verlässt in danach gleich wieder.

Code:
#!/usr/bin/python2
# -*- coding: utf-8 -*-

import os
import sys
import urllib2 # python2 only
import time
import traceback
import thread

import irc.client # http://python-irclib.sourceforge.net/
import feedparser # https://pypi.python.org/pypi/feedparser/

### CONFIG ###

HOST = 'irc.jdqirc.net'
PORT = 6667
CHANNEL = '#test'
NICK = 'charon2'
NAME = 'CyDsBot'
FEED_LIST = [ "http://uhuc.de/syndication.php?type=atom1.0&fid=36&limit=4","http://uhuc.de/syndication.php?type=atom1.0&fid=25&limit=4","http://uhuc.de/syndication.php?type=atom1.0&fid=37&limit=4" ]
ENTRIE_FILE = "/home/cyd/bin/charon2/old_feed_entries"

### CODE ###

class IRCCat(irc.client.SimpleIRCClient):
    def __init__(self, target, msgs):
        irc.client.SimpleIRCClient.__init__(self)
        self.target = target
        self.msgs = msgs

    def on_welcome(self, connection, event):
        if irc.client.is_channel(self.target):
            connection.join(self.target)
        else:
            self.send_it()

    def on_join(self, connection, event):
        self.send_it()

    def on_disconnect(self, connection, event):
        sys.exit(0)

    def send_it(self):
        time.sleep(2)
        for i in self.msgs:
            self.connection.privmsg(self.target, i)
            time.sleep(1)
        time.sleep(2)
        self.connection.quit("Goodbye and thanks for all the fish!")

def main():
    msgq = load_feeds()
    if msgq:
        c = IRCCat(CHANNEL, msgq)
        try:
            c.connect(HOST, PORT, NICK)
        except irc.client.ServerConnectionError as x:
            print (x)
            sys.exit(2)
        c.start()

def load_feeds():
    FILE = open( ENTRIE_FILE, "r" )
    filetext = FILE.read()
    FILE.close()    
    msgqueue = []
    for f in FEED_LIST:
        d = feedparser.parse(f)
        for entry in d.entries:
            ident = entry.link.encode('utf-8')+entry.title.encode('utf-8')
            if ident in filetext:
                pass
            else:
                FILE = open ( ENTRIE_FILE, "a")
                FILE.write(ident+"\n")
                FILE.close()
                
                surl = shorturl( entry.link.encode('utf-8') )
                msgqueue.append( "%s ( %s )" % (entry.title.encode('utf-8'), surl ) )
                time.sleep(1)
    return msgqueue

def shorturl(url):
    api = "http://s.uhuc.de/yourls-api.php?action=shorturl&format=simple&url="+url
    html = url
    try:
        response = urllib2.urlopen(api)
        html = response.read()
    except urllib2.HTTPError:
        print("ERROR: urlerror")
        traceback.print_exc(file=sys.stdout)
    except:
        print("ERROR: Unable to short URL")
        traceback.print_exc(file=sys.stdout)    
    return html

if __name__ == "__main__":
    main()

Evtl hat ja jmd Lust ihn für die restlichen (nicht Minecraft) Foren in #uhuc zu verwenden.




Benutzer, die gerade dieses Thema anschauen: 1 Gast/Gäste