Wikipedia:Dugnadskontor/Legge til Sportslenker/rc-listener.py

Fra Wikipedia, den frie encyklopedi

Dette er et script som lytter på recent changes fra wikidata og ser etter endringer på visse properties tilknyttet sportslenker.

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
rc-listener.py - a small script listening for recent changes on wikidata and react to some changes

"""
#
#    based on https://wikitech.wikimedia.org/w/index.php?title=RCStream&oldid=318396
#    Copyright (C) 2016  Stig Meireles Johansen (stigmjATgmail.com)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import socketIO_client

host = 'stream.wikimedia.org'
port = 80
stream = 'www.wikidata.org'

class RCListener(socketIO_client.BaseNamespace):
	def on_change(self, change):
		properties = { 'P1285', 'P1447', 'P2323', 'P2593', 'P536', 'P597', 'P599', 'P2641', 'P2642', 'P1146', 'P2090', 'P2162', 'P1409', 'P1541', 'P1664', 'P1663', 'P2350', 'P2481', 'P2602', 'P2601', 'P2459', 'P858', 'P861', 'P1825', 'P1826', 'P2482', 'P1967', 'P2091', 'P2423', 'P2640', 'P1364', 'P1469', 'P2276', 'P2020', 'P2193', 'P2195', 'P2369', 'P2446', 'P2447', 'P2574', 'P2398', 'P1238', 'P2448', 'P2449', 'P2458' }
		for prop in properties:
			if prop in change['comment']:
				print('%(user)s edited %(title)s in ns %(namespace)d with comment %(comment)s' % change)

	def on_connect(self):
		self.emit('subscribe', stream)

print ("Connecting to %s:%d" % (host, port))
socketIO = socketIO_client.SocketIO(host, port)
socketIO.define(RCListener, '/rc')
print ("Waiting")
socketIO.wait()