Skip to content
Snippets Groups Projects
Commit b067c425 authored by Svetlana Tkachenko's avatar Svetlana Tkachenko
Browse files

plugins/wikinews: create

parent 6abe09ac
No related branches found
No related tags found
No related merge requests found
# guppy Copyright (C) 2010-2011 guppy team members.
#
# This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
# This is free software, and you are welcome to redistribute it
# under certain conditions; type `show c' for details.
import time, threading
import json
import urllib.request
import urllib
import datetime
#import time
@plugin
class wikinews(object):
"""Test looping a message. """
def __init__(self, server):
self.server = server
self.commands = []
self.loop = True
self.channel = "#wikinews-spam"
self.timeListAllFeeds = 5*60 # 5 minutes
self.timeCheckNewEntries = 1*60 # 1 minutes
self.lastEntries = []
self.url='http://en.wikinews.org/w/api.php?action=query&list=categorymembers&format=json&cmtitle=Category%3AReview&cmprop=ids%7Ctitle%7Ctimestamp&cmnamespace=0&cmlimit=10&cmsort=timestamp'
def loop(plugin,server):
timeWaited = 0
while self.loop:
if timeWaited >= self.timeListAllFeeds:
self.listAllFeeds()
timeWaited = 0
time.sleep(10)
timeWaited += 10
def loop2(plugin,server):
timeWaited = 0
while self.loop:
if timeWaited >= self.timeCheckNewEntries:
self.checkNewEntries()
timeWaited = 0
time.sleep(10)
timeWaited += 10
self.t1 = threading.Thread(target=loop, args=(self,server,))
self.t2 = threading.Thread(target=loop2, args=(self,server,))
self.t1.daemon = True
self.t2.daemon = True
self.t1.start()
self.t2.start()
def checkNewEntries(self):
response = urllib.request.urlopen(self.url).read()
j = json.loads(response.decode("utf8"))
lastentries = self.lastEntries
self.lastEntries = []
for entry in j['query']['categorymembers']:
self.lastEntries.append(entry['pageid'])
if entry['pageid'] not in lastentries:
self.listEntry(entry)
def listAllFeeds(self):
#
self.lastEntries = []
# <marienz> gry: sounds like you just get one json object, so something like json.loads(urlopen(...).read().decode('utf-8')) would work. json.load(urlopen(...)) would arguably be better, but I don't know off the top of my head how to get unicode vs bytes right in python 3 with that one.
# {'query': {'categorymembers': [{'timestamp': '2012-10-13T20:54:35Z', 'ns': 0, 'pageid': 558335, 'title': 'Test 34'}]}}
response = urllib.request.urlopen(self.url).read()
j = json.loads(response.decode("utf8"))
for entry in j['query']['categorymembers']:
self.lastEntries.append(entry['pageid'])
self.listEntry(entry)
def listEntry(self,entry):
url2 = "https://en.wikinews.org/w/api.php?action=query&prop=info&format=json&inprop=url&pageids="+str(entry['pageid'])
fullurl = json.loads(urllib.request.urlopen(url2).read().decode("utf8"))['query']['pages'][str(entry['pageid'])]['fullurl']
fullurl = urllib.request.urlopen("http://tinyurl.com/api-create.php?url="+fullurl).readline().decode('utf8')
print(entry['timestamp'])
hoursAgo = datetime.datetime.utcnow() - datetime.datetime.strptime(entry['timestamp'], "%Y-%m-%dT%H:%M:%SZ")
hoursAgo = datetime.timedelta(hoursAgo.days,hoursAgo.seconds)
self.server.doMessage(self.channel, "%s *%s* ago (#%s) - %s" % (fullurl, hoursAgo, entry['pageid'], entry['title']))
def destroy(self):
self.loop = False
self.t1.join()
self.t2.join()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment