Skip to content

Commit

Permalink
Reconnect the wiki repo; Wikipedia-based automation
Browse files Browse the repository at this point in the history
  • Loading branch information
grammarware committed Jan 30, 2013
1 parent 6a92ade commit 5d97fd7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "wiki"]
path = wiki
url = git@github.com:grammarware/glossary.wiki.git
url = git@github.com:grammarware/sleg.wiki.git
48 changes: 48 additions & 0 deletions updWikipedia.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/local/bin/python
# -*- coding: utf-8 -*-

import os
import urllib, urllib2, httplib, socket

languages = ('en','ru','nl','de')

def safelyLoadURL(url):
errors = 0
while errors<3:
try:
return urllib.urlopen(url).read()
except IOError,e:
print 'Warning: failed to load URL, retrying...'
errors += 1
except socket.error,e:
print 'Warning: connection reset by peer, retrying...'
errors += 1
print 'Error fetching URL:',url
return ''

for root, dirs, filenames in os.walk('wiki/'):
for f in filter(lambda x:x.endswith('.md'),filenames):
if f == 'Home.md':
continue
print f
md = open('wiki/%s' % f, 'r')
topic = ''
links = []
for line in md.readlines():
if not line:
continue
for chunk in line.split('[')[1:]:
name = chunk.split(']')[0]
link = chunk.split('](')[1].split('))')[0]
if name == 'Wikipedia':
links.append(link)
print 'Checking up',topic
for link in links:
print '----->',link
rawlink = link.replace('.wikipedia.org/wiki/', '.wikipedia.org/w/index.php?title=')+'&action=raw'
for iw in filter(lambda x:x.startswith('[[') and x.strip().endswith(']]') and x[2:4] in languages and x[4]==':', safelyLoadURL(rawlink).split('\n')):
newlink = 'http://%s.wikipedia.org/wiki/%s' % (iw[2:4],iw.split(':')[1].split(']]')[0])
newlink = newlink.replace('(','%28').replace(')','%29')
if newlink not in links:
print ' ?-->',newlink
md.close()
2 changes: 1 addition & 1 deletion wiki
Submodule wiki updated from 622c64 to d5f926

0 comments on commit 5d97fd7

Please sign in to comment.