Information icon.svg At All things in moderation, we are currently voting on restrictions on the use of LLMs (chatbots) in RationalWiki. Reminder: you need to have at least 75 edits and to have been registered for at least three months to be eligible to vote.


From RationalWiki
Jump to navigation Jump to search
#!/usr/bin/env python3
# encoding: utf8
# Copyright © 2016 Martin Tournoij <>
# See below for full copyright
# Make sure that xvfb is running, or run this script with xvfb-run:
#   $ xvfb-run --server-args '-screen 0 1280x1024x16' python3.4
# The resolution is important, the default of 640x480 will result in ugly fonts.

import sys, os, re, subprocess, tempfile, time, urllib.parse, urllib.request, datetime

import pywikibot
from pywikibot.pagegenerators import RecentChangesPageGenerator
from bs4 import BeautifulSoup

import upload

_site = pywikibot.Site()

def recent_changes():
	''' Get all pages changed in the last 70 min '''
	gen = RecentChangesPageGenerator(
		start=(datetime.datetime.utcnow() - datetime.timedelta(minutes=70)),
		reverse=True,         # Start with oldest
		showBot=False,        # Don't need bot changes
		showRedirects=False)  # Or redirects

	return gen

def find_uncaptured(page):
	""" Find uncaptured <capture> links on +page+ """

	soup = BeautifulSoup(_site.getUrl(_site.get_address(page.title())), 'html.parser')
	for link in'span.wigocapture > sup >'):
			filename = re.sub('^File:', '', re.sub(r' \(page does not exist\)$', '', link.get('title')))
			url = link.find_parent('span', class_='wigocapture').find_previous_sibling('a').get('href')

			with tempfile.TemporaryDirectory() as tmp:
				tmp += '/f.png'
				print('    capturing {} -> {}'.format(url, tmp))
					capture_page(url, tmp)
					print('ERROR', sys.exc_info()[1])

				print('    Uploading to {}'.format(filename))
				upload_file(tmp, filename, make_desc(url))
		# Make sure we never fail in the loop; updating pages is asynchronous,
		# and exiting unexpectedly will fuck things up.
			print('ERROR', sys.exc_info()[1])

def make_desc(url):
	""" Make a description for the upload """
	purl =  urllib.parse.urlparse(url)

	if '' in purl.netloc:
		cat = 'Category:{} Conservapedia screencaps'.format('%B %Y'))
		desc = '{{CP screenshot}}\n[[' + cat + ']]'

		# Check for the existence of monthly category and create it
		catpage = pywikibot.Page(_site, cat)
		if not catpage.exists():
			catpage.put('[[Category:Conservapedia screencaps]]', comment='Creating monthly category', minorEdit=False)
	elif '' in purl.netloc:
		desc = '{{CZ screenshot}}\n[[Category:Citizendium screencaps]]'
		desc = '{{subst:Nolicense/subst}}\n[[Category:Screencaps]]'

	ar = archive_is(url)
	return 'Capture of {}\n\n{}{}'.format(url, ar, desc)

def upload_file(path, dest, desc=''):
	""" Upload the file at +path+ to +title+ with the description +desc+ """

		ignoreWarning=True,       # Just upload
		verifyDescription=False,  # Don't ask for confirmation
		keepFilename=True,        # Don't ask for confirmation
		url=[path],               # filename must be list
		useFilename=dest,         # Destination page name

def capture_page(url, out):
	""" Run wkhtmltoimage on +url+, saving the output to +page+ """

		# The --quality parameter is weird:
		# Running with a quality of 90 or higher gives whopping file sizes; well
		# above 50M for some pages (instead of about 1M). After running optipng
		# all the file sizes are the same (?!) I can't see any difference eiter
		# For fonts:
		ret = subprocess.check_output(['wkhtmltoimage', '--quiet', '--quality', '50', url, out])
		size = os.stat(out).st_size
		if size < 1014:
			raise Exception('File is very small: {}'.format(size))
		if size > 5242880: # 5M
			raise Exception('File is very large: {}'.format(size))
	except subprocess.CalledProcessError:

def archive_is(url):
	""" Send +url+ off to for archival, and return the access URL. We
	won't request a re-archive if the page already exists (which is usually
	okay). """

	data = urllib.parse.urlencode([('url', url)])
	with urllib.request.urlopen('', data=data.encode()) as fp:
		archive ='"(http://archive\.is/[a-zA-Z0-9]{3,6})"',
		if archive:
			return archive.groups()[0]
		# Fetch existing
			archive = urllib.request.urlopen('{}'.format(url))
			soup = BeautifulSoup(archive, 'html.parser')
			return'#CONTENT .THUMBS-BLOCK a')[0].get('href')

_linkdb = None
def notify_archives(url):
	''' Notice, but only if we've never done this before '''

	global _linkdb
	if _linkdb is None:
		_linkdb = open('/data/code/rw/linkdb', 'r').readlines()

	if url + '\n' in _linkdb:

	print('  Archive', url)
	print('   ', archive_is(url))
	with open('/data/code/rw/linkdb', 'a') as fp:
		fp.write(url + '\n')

def archive_templates(page):
	''' Find all {{a}} templates on +page+ 
	 Converting refs in Vim
	   :%s/<ref>\[\(.\{-}\) \(.\{-}\)]\(.\{-}\)<\/ref>/<ref>{{a|\1|\2\3}}<\/ref>/

	for t in page.templatesWithParams():
		#print(' '*7, 'Found template:', t[0].title())
		if t[0].title() not in ['Template:A', 'User:Carpetsmoker/a']: continue
		if len(t[1]) > 1:

def main():
	# We want to make sure there's only one instance of this running.
	lock = '/data/code/rw/.capturebot_running'
	if os.path.exists(lock):
		print('Already have a running instance; doing nothing')
	open(lock, 'w+').close()

		done = []
		for p in recent_changes():
			if p.title() in done: continue
			print('=> {}'.format(p.title()))

		always_check = ['User:Capturebot4/sandbox']
		for p in always_check:
			if p.title() in done: continue
			p = pywikibot.Page(_site, p)
			print('=> {}'.format(p.title()))

if __name__ == '__main__':

# The MIT License (MIT)
# Copyright © 2016 Martin Tournoij
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# The software is provided "as is", without warranty of any kind, express or
# implied, including but not limited to the warranties of merchantability,
# fitness for a particular purpose and noninfringement. In no event shall the
# authors or copyright holders be liable for any claim, damages or other
# liability, whether in an action of contract, tort or otherwise, arising
# from, out of or in connection with the software or the use or other dealings
# in the software.