Jump to content

User:AllyUnion/VFD bot code

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by AllyUnion (talk | contribs) at 23:06, 7 April 2005. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

These are all the files that are run for User:VFD Bot. This page and its contents are copyrighted under the Creative Commons Attribution ShareAlike 1.0 License: [1]. The information here is not licensed by GNU Free Documentation License. The purpose of hosting this code is to allow the public viewing of the code being run on the Wikipedia.

en-wp-vfd-list-fix.py

#!/usr/bin/python2.3
# Author: Jason Y. Lee (AllyUnion)
# Purpose: Automatically update a VFD List on User:AllyUnion/VFD List
#	   every hour.  Keeps 7 days, presumes for exactly 7 sections
#	   on specified page.  (Seven days, seven sections)
# 	   To be run by a cron job.
#	   Also removes the top section once the next UTC day comes

# Revision 3.07

import wikipedia, config
import os
import commands
import sys
import datetime

if __name__ == "__main__":
	utc = datetime.datetime.utcnow()
	# Get the dates
#	yyyy = int(datetime.datetime.utcnow().strftime('%Y'))
#	mm = int(datetime.datetime.utcnow().strftime('%m'))
#	dd = int(datetime.datetime.utcnow().strftime('%d'))

	# Today's date, exactly at 0000 hours
#	today = datetime.datetime(yyyy, mm, dd, 0, 0, 0, 0)
	today = utc.replace(hour=0,minute=0,second=0,microsecond=0)

	# Today's date, exactly at 1000 hours
#	onehour = datetime.datetime(yyyy, mm, dd, 1, 0, 0, 0)
	onehour = utc.replace(hour=1,minute=0,second=0,microsecond=0)

	# Tomorrow's date, exactly at 0000 hours
	tomorrow = today + datetime.timedelta(1)

	# Yesterday's date, exactly at 0000 hours
	yesterday = today - datetime.timedelta(1)

	# Seven days prior to today's date at 0000 hours
	sevendaysago = today - datetime.timedelta(7)

	# Check the time now
	utctime = datetime.datetime.utcnow()# - datetime.timedelta(0, 14400)

	# Wikipedia Variable Setup
	# VFD Page log name
	vfdlog = "Wikipedia:Votes_for_deletion/Log/"
	# Which site, as specified in user-config.py
	mysite = wikipedia.getSite()

	# Page: User:AllyUnion/VFD List and sections
	oldvpage = wikipedia.getPage(mysite, 'User:AllyUnion/VFD List', True, True, False)
	section1 = wikipedia.getPage(mysite, 'User:AllyUnion/VFD List&section=1', True, True, False)
	section2 = wikipedia.getPage(mysite, 'User:AllyUnion/VFD List&section=2', True, True, False)
	section3 = wikipedia.getPage(mysite, 'User:AllyUnion/VFD List&section=3', True, True, False)
	section4 = wikipedia.getPage(mysite, 'User:AllyUnion/VFD List&section=4', True, True, False)
	section5 = wikipedia.getPage(mysite, 'User:AllyUnion/VFD List&section=5', True, True, False)
	section6 = wikipedia.getPage(mysite, 'User:AllyUnion/VFD List&section=6', True, True, False)
	section7 = wikipedia.getPage(mysite, 'User:AllyUnion/VFD List&section=7', True, True, False)

	# Top heading
	notice = str('This is a list of [[Wikipedia:Votes for deletion|VFD discussions]], updated hourly.<br/>\n<!-- Please do not add your own VFD sections to this page.  It is not necessary. -->\n').encode('iso-8859-1')

	comment = 'Hourly Automatic Update of VFD List: '

	# Newline
	newline = '\n'

	# Temporary Log File
	logfile = '/tmp/vfd.log'

	# Temporary Old Log File
	difffile = '/tmp/diff-vfd.log'

	# Temporary Parse File
	parsefile = '/tmp/vfd-parse.log'

	# Temporary Yesterday Parse File
	yparsefile = '/tmp/vfd-yparse.log'

	# Grep command
	grepcmd = ' | grep -v \'<!-- New votes to the bottom, please. -->\''

	# Perl command to parse file
	perlcmd = ' | perl -pi -e \'s/{{/\* [[/g\' | perl -pi -e \'s/}}/]]/g\''

	# Diff command
	diffcmd = 'diff -u ' + difffile + ' ' + logfile + ' | grep ^+ | grep \'* \[\[\' | perl -pi -e \'s/\*.\[\[Wikipedia:Votes.for.deletion\// /g\' | perl -pi -e \'s/\]\]//g\''

	diffcmd2 = 'diff -u ' + difffile + ' ' + logfile + ' | grep ^- | grep \'* \[\[\'| perl -pi -e \'s/\*.\[\[Wikipedia:Votes.for.deletion\// /g\' | perl -pi -e \'s/\]\]//g\''

	# Login file, full path and filename
#	loginfile = '/pywikipediabot/login.py'

	log = file(difffile, 'w')
	log.write(oldvpage.encode('iso-8859-1'))
	log.close()

	# today <= utctime <= onehour
	if (today <= utctime <= onehour):
		print 'Operation: Remove top, add new day'
		# Perform top removal procedure
		# Get yesterday's VFD and convert
		# Get today's VFD and convert
		# Replace sections 6 and 7

		# Open log for writing
		log = file(logfile, 'w')

		# Write notice
		log.write(notice.encode('iso-8859-1'))

		# Plus newline
		log.write(newline.encode('iso-8859-1'))

		# Write sections 2, 3, 4, 5, 6 with a newline between each one
		# Since, we removed section 1, sections 2-6 become our new sections 1-5
	        log.write(section2.encode('iso-8859-1'))
	        log.write(newline.encode('iso-8859-1'))
	        log.write(section3.encode('iso-8859-1'))
	        log.write(newline.encode('iso-8859-1'))
	        log.write(section4.encode('iso-8859-1'))
	        log.write(newline.encode('iso-8859-1'))
	        log.write(section5.encode('iso-8859-1'))
	        log.write(newline.encode('iso-8859-1'))
	        log.write(section6.encode('iso-8859-1'))
	        log.write(newline.encode('iso-8859-1'))

		# Get the VFD page from yesterday
		vfdpage = vfdlog + str(yesterday.strftime('%Y_%B_')) + str(int(yesterday.strftime('%d')))
		toparse = wikipedia.getPage(mysite, vfdpage, True, True, False)

		# Write the VFD yesterday to a temporary parse log
		parselog = file(yparsefile, 'w')
		parselog.write(toparse.encode('iso-8859-1'))
		parselog.close()

		# Yesterday's VFD, parsed into a list
		yparsecmd = 'cat ' + yparsefile + grepcmd + perlcmd
		yparsed = commands.getoutput(yparsecmd)
		yparsed = yparsed.decode('iso-8859-1')

		# Link to VFD page
		# Long Date: example: 2005_January_1
		ydate1 = yesterday.strftime('%Y_%B_') + str(int(yesterday.strftime('%d')))

		# Short Date: example: January 1
		ydate2 = yesterday.strftime('%B') + ' ' + str(int(yesterday.strftime('%d')))

		# Give the page name
		yfind1 = '[[Wikipedia:Votes for deletion/Log/' + ydate1 + '|' + ydate2 + ']]'
		yfind2 = '[[Wikipedia:Votes for deletion/Log/' + ydate1 + '|[[Wikipedia:Votes for deletion/Log/' + ydate1 + '|' + ydate2 + ']]]]'

		# Section space remove
		yparsed = yparsed.replace('== ', '==')
		yparsed = yparsed.replace(' ==', '==')

		# First, replace it once, so a link is established
		yparsed = yparsed.replace('==' + ydate2 + '==', '==' + yfind1 + '==', 1)

		# Second, if it has been done before, this will fix (hopefully), the internal link to the proper form
		yparsed = yparsed.replace(yfind2, yfind1, 1)

		yplines = yparsed.splitlines();
		ypnum = yplines.index('==' + yfind1 + '==')

		log.write(yplines[ypnum].encode('iso-8859-1'))
		log.write(newline.encode('iso-8859-1'))

		for x in range(ypnum-1):
			log.write(yplines[x].encode('iso-8859-1'))
			log.write(newline.encode('iso-8859-1'))

		for x in range(len(yplines) - ypnum - 1):
			x = x + ypnum + 1
			log.write(yplines[x].encode('iso-8859-1'))
			log.write(newline.encode('iso-8859-1'))

		# Write yesterday's stuff to the log
#		log.write(yparsed.encode('iso-8859-1'))
		log.write(newline.encode('iso-8859-1'))

		# Get the VFD page for today
		vfdpage = vfdlog + str(today.strftime('%Y_%B_')) + str(int(today.strftime('%d')))
		toparse = wikipedia.getPage(mysite, vfdpage, True, True, False)

		# Write the VFD page for today to a temporary parse log
		parselog = file(parsefile, 'w')
		parselog.write(toparse.encode('iso-8859-1'))
		parselog.close()

		# Today's VFD, parsed into a list
		parsecmd = 'cat ' + parsefile + grepcmd + perlcmd
		parsed = commands.getoutput(parsecmd)
		parsed = parsed.decode('iso-8859-1')

		# Link to VFD page

		# Long Date: example: 2005_January_1
		date1 = today.strftime('%Y_%B_') + str(int(today.strftime('%d')))

		# Short Date: example: January 1
		date2 = today.strftime('%B') + ' ' + str(int(today.strftime('%d')))

		# Give the page name
		find1 = '[[Wikipedia:Votes for deletion/Log/' + date1 + '|' + date2 + ']]'
		find2 = '[[Wikipedia:Votes for deletion/Log/' + date1 + '|[[Wikipedia:Votes for deletion/Log/' + date1 + '|' + date2 + ']]]]'

		# Section space remove
		parsed = parsed.replace('== ', '==')
		parsed = parsed.replace(' ==', '==')

		# First, replace it once, so a link is established
		parsed = parsed.replace('==' + date2 + '==', '==' + find1 + '==', 1)

		# Second, if it has been done before, this will fix (hopefully), the internal link to the proper form
		parsed = parsed.replace(find2, find1, 1)

		plines = parsed.splitlines();
		pnum = plines.index('==' + find1 + '==')

		log.write(plines[pnum].encode('iso-8859-1'))
		log.write(newline.encode('iso-8859-1'))

		for x in range(pnum-1):
			log.write(plines[x].encode('iso-8859-1'))
			log.write(newline.encode('iso-8859-1'))

		for x in range(len(plines) - pnum - 1):
			x = x + pnum + 1
			log.write(plines[x].encode('iso-8859-1'))
			log.write(newline.encode('iso-8859-1'))

		# Write today's stuff to the log
#		log.write(parsed.encode('iso-8859-1'))
		log.write(newline.encode('iso-8859-1'))

		# Close the file, making sure all the contents are written to the log
		log.close()

		# User:Mozzerati Feature request
		diffcomment = commands.getoutput(diffcmd)
		diffcomment = diffcomment.decode('iso-8859-1')
		difflist = diffcomment.splitlines()

		diffcomment2 = commands.getoutput(diffcmd2)
		diffcomment2 = diffcomment2.decode('iso-8859-1')
		difflist2 = diffcomment2.splitlines()

		for check in difflist:
			for checking in difflist2:
				if (checking[1:] == check[1:]):
					difflist.remove(check)
		for x in range(len(difflist) - 1):
			comment += difflist[x] + ', '
		comment += difflist[x+1] + '.'
#		for check in difflist:
#			comment += check + ', '
#		comment[-2:] = '.'

		# Reopen the log file		
		log = file(logfile, 'r')

		# Read the whole log into a variable
		post = log.read()

		# Close log file
		log.close()

		# Log in to Wikipedia
#		cmd = 'python2.3 ' + loginfile
#		os.system(cmd)

		page = wikipedia.PageLink(mysite, 'User:AllyUnion/VFD List')
		# Post to the Wikipedia
		page.put(post, comment)

		cmd = 'rm -f ' + logfile + ' ' + parsefile + ' ' + yparsefile + ' ' + difffile
		os.system(cmd)

	# onehour < utctime <= tomorrow
	elif (onehour < utctime <= tomorrow):
		print 'Operation: Normal - Update last section'
		# Get today's VFD and convert
		# Replace section 7

		# Open log for writing
		log = file(logfile, 'w')

		# Write notice
		log.write(notice.encode('iso-8859-1'))

		# Plus newline
		log.write(newline.encode('iso-8859-1'))

		# Write sections 1, 2, 3, 4, 5, 6 with a newline between each one
	        log.write(section1.encode('iso-8859-1'))
	        log.write(newline.encode('iso-8859-1'))
	        log.write(section2.encode('iso-8859-1'))
	        log.write(newline.encode('iso-8859-1'))
	        log.write(section3.encode('iso-8859-1'))
	        log.write(newline.encode('iso-8859-1'))
	        log.write(section4.encode('iso-8859-1'))
	        log.write(newline.encode('iso-8859-1'))
	        log.write(section5.encode('iso-8859-1'))
	        log.write(newline.encode('iso-8859-1'))
	        log.write(section6.encode('iso-8859-1'))
	        log.write(newline.encode('iso-8859-1'))

		# Get the VFD page for today
		vfdpage = vfdlog + str(today.strftime('%Y_%B_')) + str(int(today.strftime('%d')))
		toparse = wikipedia.getPage(mysite, vfdpage, True, True, False)

		# Write the VFD page for today to a temporary parse log
		parselog = file(parsefile, 'w')
		parselog.write(toparse.encode('iso-8859-1'))
		parselog.close()

		# Today's VFD, parsed into a list
		parsecmd = 'cat ' + parsefile + grepcmd + perlcmd
		parsed = commands.getoutput(parsecmd)
		parsed = parsed.decode('iso-8859-1')

		# Link to VFD page

		# Long Date: example: 2005_January_1
		date1 = today.strftime('%Y_%B_') + str(int(today.strftime('%d')))

		# Short Date: example: January 1
		date2 = today.strftime('%B') + ' ' + str(int(today.strftime('%d')))

		# Give the page name
		find1 = '[[Wikipedia:Votes for deletion/Log/' + date1 + '|' + date2 + ']]'
		find2 = '[[Wikipedia:Votes for deletion/Log/' + date1 + '|[[Wikipedia:Votes for deletion/Log/' + date1 + '|' + date2 + ']]]]'

		# Section space remove
		parsed = parsed.replace('== ', '==')
		parsed = parsed.replace(' ==', '==')
		
		# First, replace it once, so a link is established
		parsed = parsed.replace('==' + date2 + '==', '==' + find1 + '==', 1)

		# Second, if it has been done before, this will fix (hopefully), the internal link to the proper form
		parsed = parsed.replace(find2, find1, 1)

		plines = parsed.splitlines();
		pnum = plines.index('==' + find1 + '==')

		log.write(plines[pnum].encode('iso-8859-1'))
		log.write(newline.encode('iso-8859-1'))

		for x in range(pnum-1):
			log.write(plines[x].encode('iso-8859-1'))
			log.write(newline.encode('iso-8859-1'))

		for x in range(len(plines) - pnum - 1):
			x = x + pnum + 1
			log.write(plines[x].encode('iso-8859-1'))
			log.write(newline.encode('iso-8859-1'))

		# Write today's stuff to the log
#		log.write(parsed.encode('iso-8859-1'))
		log.write(newline.encode('iso-8859-1'))

		# Close the file, making sure all the contents are written to the log
		log.close()

		# User:Mozzerati Feature request
		diffcomment = commands.getoutput(diffcmd)
		diffcomment = diffcomment.decode('iso-8859-1')
		difflist = diffcomment.splitlines()

		diffcomment2 = commands.getoutput(diffcmd2)
		diffcomment2 = diffcomment2.decode('iso-8859-1')
		difflist2 = diffcomment2.splitlines()

		for check in difflist:
			for checking in difflist2:
				if (checking[1:] == check[1:]):
					difflist.remove(check)
		for x in range(len(difflist) - 1):
			comment += difflist[x] + ', '
		comment += difflist[x+1] + '.'
#		comment[-2:] = '.'

		# Reopen the log file		
		log = file(logfile, 'r')

		# Read the whole log into a variable
		post = log.read()

		# Close log file
		log.close()

		# Log in to Wikipedia
#		cmd = 'python2.3 ' + loginfile
#		os.system(cmd)

		page = wikipedia.PageLink(mysite, 'User:AllyUnion/VFD List')
		# Post to the Wikipedia
		page.put(post, comment)

		cmd = 'rm -f ' + logfile + ' ' + parsefile + ' ' + difffile
		os.system(cmd)


	# Possibility is that utctime == tomorrow, but should never happen
	else:
		sys.exit(1)

sys.exit(0)