Rosetta Code:Village Pump/Sort popular pump pages: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(added source code)
Line 11: Line 11:
It's working perfectly. (I used GugaTagFixer to test it.)
It's working perfectly. (I used GugaTagFixer to test it.)
I will post source code here. --[[User:Guga360|Guga360]] 22:37, 24 April 2009 (UTC)
I will post source code here. --[[User:Guga360|Guga360]] 22:37, 24 April 2009 (UTC)

<code python>import urllib
import urllib2
import time
import re
import xml.dom.minidom

class Page:
def __init__(self, title, ts):
self.title = title
self.time = time.mktime(time.strptime(ts,"%Y-%m-%dT%H:%M:%SZ"))

def login(user, password):
global cookie
query = urllib.urlencode({'lgname':user,'lgpassword':password})
result = urllib.urlopen("http://www.rosettacode.org/w/api.php?action=login&format=xml",query).read()
parse = xml.dom.minidom.parseString(result)
login = parse.getElementsByTagName("login")[0]
cookie = login.getAttribute("cookieprefix")+"_session="+login.getAttribute("sessionid")

def get_token():
global cookie

result = urllib2.Request("http://www.rosettacode.org/w/api.php?action=query&prop=info&intoken=edit&titles=Main%20Page&format=xml",headers={"Cookie":cookie})
result = urllib2.urlopen(result).read()
parse = xml.dom.minidom.parseString(result)
token = parse.getElementsByTagName("page")[0].getAttribute("edittoken")
return token
def GetTimeStamp(x):
return x.getElementsByTagName("rev")[0].getAttribute("timestamp")
def getsortedvp():
edits = []

get1 = urllib.urlopen("http://www.rosettacode.org/wiki/Village_Pump:Home&action=raw").read()
pumps = re.findall("\{\{vp\|(.+?)\}\}",get1)[1:] # ignore first (example)

for i in pumps:
get2 = urllib.urlopen("http://www.rosettacode.org/w/api.php?action=query&prop=revisions&format=xml&titles=Village_Pump:Home/%s" % i.replace(" ","_")).read()
x = xml.dom.minidom.parseString(get2)
edits.append(Page(i,GetTimeStamp(x)))

edits = sorted(edits, key=lambda x: x.time,reverse=True)

final = """This is the new place for Rosetta Code community activity. To start a new "thread", just append a "/" to the end of the URL of this page and give your thread a short title. On the new page, give a more detailed description of the topic as a heading, and write away. Also, link to the new topic on this page. Ex: <nowiki>{{vp|New topic}}</nowiki>.

"""+'\n'.join(["*{{vp|%s}}" % i.title for i in edits])
return final
def edit(article, content, token):
global cookie
query = urllib.urlencode({
"title":article,
"text":content,
"token":token,
"bot":"yes"
})
req = urllib2.Request("http://www.rosettacode.org/w/api.php?action=edit&format=xml",data=query, headers={"Cookie":cookie})
req = urllib2.urlopen(req).read()
return req

login("VPSortBot","password")
token = get_token()

edit("Village_Pump:Home",getsortedvp(), token)</code>

--[[User:Guga360|Guga360]] 22:41, 24 April 2009 (UTC)

Revision as of 22:41, 24 April 2009

I was reading [[1]]. I'm programming the Village Pump sorter right now.

I will just read all {{vp|...}} and get last edit date. And just sort it by edit date. Very simple.

IMO, it's better than rotate new pump pages to the top of list, and old to bottom. --Guga360 21:22, 24 April 2009 (UTC)

My code is 80% done. --Guga360 22:01, 24 April 2009 (UTC)

I already runned it. It's working perfectly. (I used GugaTagFixer to test it.) I will post source code here. --Guga360 22:37, 24 April 2009 (UTC)

import urllib import urllib2 import time import re import xml.dom.minidom

class Page: def __init__(self, title, ts): self.title = title self.time = time.mktime(time.strptime(ts,"%Y-%m-%dT%H:%M:%SZ"))

def login(user, password): global cookie

query = urllib.urlencode({'lgname':user,'lgpassword':password}) result = urllib.urlopen("http://www.rosettacode.org/w/api.php?action=login&format=xml",query).read() parse = xml.dom.minidom.parseString(result) login = parse.getElementsByTagName("login")[0] cookie = login.getAttribute("cookieprefix")+"_session="+login.getAttribute("sessionid")

def get_token(): global cookie

result = urllib2.Request("http://www.rosettacode.org/w/api.php?action=query&prop=info&intoken=edit&titles=Main%20Page&format=xml",headers={"Cookie":cookie}) result = urllib2.urlopen(result).read() parse = xml.dom.minidom.parseString(result) token = parse.getElementsByTagName("page")[0].getAttribute("edittoken") return token

def GetTimeStamp(x): return x.getElementsByTagName("rev")[0].getAttribute("timestamp")

def getsortedvp(): edits = []

get1 = urllib.urlopen("http://www.rosettacode.org/wiki/Village_Pump:Home&action=raw").read() pumps = re.findall("\{\{vp\|(.+?)\}\}",get1)[1:] # ignore first (example)

for i in pumps: get2 = urllib.urlopen("http://www.rosettacode.org/w/api.php?action=query&prop=revisions&format=xml&titles=Village_Pump:Home/%s" % i.replace(" ","_")).read() x = xml.dom.minidom.parseString(get2) edits.append(Page(i,GetTimeStamp(x)))

edits = sorted(edits, key=lambda x: x.time,reverse=True)

final = """This is the new place for Rosetta Code community activity. To start a new "thread", just append a "/" to the end of the URL of this page and give your thread a short title. On the new page, give a more detailed description of the topic as a heading, and write away. Also, link to the new topic on this page. Ex: {{vp|New topic}}.

"""+'\n'.join(["*%s" % i.title for i in edits])

return final

def edit(article, content, token):

   global cookie	

   query = urllib.urlencode({
   "title":article,
   "text":content,
   "token":token,
   "bot":"yes"
   })

   req = urllib2.Request("http://www.rosettacode.org/w/api.php?action=edit&format=xml",data=query, headers={"Cookie":cookie})
   req = urllib2.urlopen(req).read()

   return req	

login("VPSortBot","password") token = get_token()

edit("Village_Pump:Home",getsortedvp(), token)

--Guga360 22:41, 24 April 2009 (UTC)