Commit a595749a authored by Jim Tittsler's avatar Jim Tittsler

Remove lint.

* search for .wikieducator.rc in current directory and home directory
* reduce logging output
* warn that template is script generated
parent 8dbd5715
The MIT License (MIT)
Copyright (c) 2014 Open Education Resource Foundation
Copyright (c) 2014-2018 Open Education Resource Foundation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......
import os
#!/usr/bin/env python2
"""oerupartners.
Scrape OERu.org partner list to keep WikiEducator descriptions
and logos up to date.
"""
import os.path
import re
import time
import math
import ConfigParser
from lxml import html, etree
import requests
import hashlib
import requests
import logging
import mwclient
try:
from cStringIO import StringIO
except:
except ImportError:
from StringIO import StringIO
from lxml import html, etree
from PIL import Image
import logging
import mwclient
OERu = 'http://OERu.org'
OERu_partner_page = OERu + '/oeru-partners/'
config = ConfigParser.ConfigParser()
config.read('.wikieducator.rc')
config.read(['.wikieducator.rc', os.path.expanduser('~/.wikieducator.rc')])
we_user = config.get('login', 'user')
we_pass = config.get('login', 'password')
we = mwclient.Site('wikieducator.org', path='/')
we.login(username = we_user, password = we_pass)
we.login(username = we_user, password=we_pass)
logging.basicConfig(level=logging.DEBUG, filename='/tmp/oerupartners.log')
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s',
filename='/tmp/oerupartners.log')
def get_partners(u):
page = requests.get(u)
......@@ -44,25 +53,26 @@ def get_partners(u):
def get_image_files(we, partners):
for partner in partners:
# fetch the image
print partner['img']
logging.info(partner['img'])
img = requests.get(partner['img'])
sha1 = hashlib.sha1(img.content).hexdigest()
# see if we already have this image
print ' ', sha1
# see if WE already has this image
logging.info(" SHA1: %s", sha1)
images = we.allimages(sha1=sha1, generator=True)
count = 0
for i in images:
partner['file'] = i.page_title
print " Exists:", i.page_title.encode('utf-8')
logging.info(" Exists: %s", i.page_title.encode('utf-8'))
count += 1
if count == 0:
# upload the image
pname = re.sub(r'[^- _a-z0-9]', '', partner['name'], flags=re.IGNORECASE)
filename = pname + partner['img'][-4:]
print "upload:", filename
logging.info(" Upload: %s", filename)
try:
we.upload(file=StringIO(img.content), filename=filename,
we.upload(file=StringIO(img.content),
filename=filename,
description='OERu partner %s logo' % (partner['name']))
except mwclient.errors.FileExists:
print "FILE %s EXISTS!" % (filename)
......@@ -82,7 +92,7 @@ def get_image_files(we, partners):
== Licensing ==
{{Fair-use}}""" % (partner['name'], partner['name'], time.strftime('%Y-%m-%d'))
page.save(text = description, summary=u'update description/license information',
minor=True)
minor=True)
im = Image.open(StringIO(img.content))
(partner['width'], partner['height']) = im.size
del im
......@@ -90,7 +100,7 @@ def get_image_files(we, partners):
def make_partner_pages(we, partners):
for partner in partners:
print partner['name']
logging.info(partner['name'])
# fetch the page
partner_page = requests.get(partner['url'])
tree = html.fromstring(partner_page.text)
......@@ -140,7 +150,9 @@ def featured_partner(we, partners):
numparts = len(partners)
partners = sorted(partners, key=lambda k: k['name'])
body = []
body.append('{{Lozenge|style=background:#C3A8C9;|text=Featured OERu Partner}}')
body.append('<noinclude>This template is created by the oerupartners.py script, DO NOT HAND EDIT.')
body.append('')
body.append('</noinclude>{{Lozenge|style=background:#C3A8C9;|text=Featured OERu Partner}}')
body.append('')
body.append('')
body.append('{{#switch:{{#expr: floor(({{#time:U}} mod 345600)/%d)}}' % (
......@@ -148,8 +160,8 @@ def featured_partner(we, partners):
i = 0
for partner in partners:
body.append(' | %d = [[File:%s|left|%dpx|link=%s]] {{OERu Partner Feature|%s|%s}}' %
(i, partner['file'], min(200, partner['width']),
partner['url'], partner['name'], partner['url']))
(i, partner['file'], min(200, partner['width']),
partner['url'], partner['name'], partner['url']))
i += 1
body.append('}}')
page = we.Pages['Template:OERu Featured Partner']
......@@ -161,13 +173,11 @@ def featured_partner(we, partners):
# make a featured partner template
partners = get_partners(OERu_partner_page)
partners = get_image_files(we, partners)
for partner in partners:
print partner['name']
print ' ',partner['url']
print ' ',partner['file']
print ' %s (%d x %d)' % (partner['img'], partner['width'], partner['height'])
print len(partners)
#for partner in partners:
# print partner['name']
# print ' ', partner['url']
# print ' ', partner['file']
# print ' %s (%d x %d)' % (partner['img'], partner['width'], partner['height'])
make_partner_pages(we, partners)
featured_partner(we, partners)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment