Skip to content

Commit

Permalink
wrote getGermanName page name translator
Browse files Browse the repository at this point in the history
  • Loading branch information
notconfusing committed Aug 8, 2012
1 parent 1397a89 commit 6fa0e5f
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions viafbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,51 +32,69 @@ def pageValidate(nameOfPage):
except NoPage:
raise NoPage

def determineAuthorityControlTemplate(nameOfPage, site):
def determineAuthorityControlTemplate(pageObject):
"""returns 'noACtemplate' if no Authority Control Template, 'templateNoVIAF' if AC template but no VIAF number,
and returns the viaf number if it exists"""
namepage = Page(site,nameOfPage)
templates = namepage.templatesWithParams()
if site == enwp:
targetTem = 'AuthorityControl'
else:
targetTem = 'Normdaten'
templates = pageObject.templatesWithParams()
for template in templates:
if template[0] == targetTem:
if template[0] == 'Authority control':
for param in template[1]:
if param[:4] == 'VIAF':
return param[5:]
return 'templateNoVIAF'
return 'noACtemplate'

def getGermanName(nameOfPage):
"""returns a strng which is the equivalent German Wikipedia page to argument
raises NoPage if there is no German equivalent."""
namepage = Page(enwp,nameOfPage)
interWikis = namepage.get().getLanguageLinks # is this second call to namepage.get() too much io?

def determineNormdatenTemplate(pageObject):
"""returns 'noNormdatenTemplate' if no Normdaten Template, 'templateNoVIAF' if Normdaten template but no VIAF number,
and returns the viaf number if it exists"""
templates = pageObject.templatesWithParams()
for template in templates:
if template[0] == 'Normdaten':
for param in template[1]:
if param[:4] == 'VIAF':
return param[5:]
return 'templateNoVIAF'
return 'noNormdatenTemplate'

def getGermanName(pageObject):
"""returns a Page object which is the equivalent German Wikipedia page to argument
raises NoPage if there is no German equivalent."""
pageText = pageObject.get()
interWikis = getLanguageLinks(pageText)
try:
return interWikis[dewp]
except KeyError:
raise NoPage

print pageValidate('Mayakovsky')
print pageValidate('User:VIAFbot/redir2')
print determineNormdatenTemplate(getGermanName(Page(enwp,'Oscar Wilde')))
print determineAuthorityControlTemplate(Page(enwp,'Oscar Wilde'))

#the main loop
for wikilink in wikilinks:
wikilink = wikilink.split() #to get the line into a list of (name, viafnum)
unvalidatedPageName = wikilink[0]

try:
validatedPage = pageValidate(unvalidatedPageName) #It's possible that the page doesn't exist
except NoPage:
viafbotrun.write(unvalidatedPageName.title() + "did not exist, or redirected more than 20 times")
continue #If the page doesn't exist, then we don't need to write anything to the Wiki.

#get statuses of Authority Control and Normdaten templates
ACstatus = determineAuthorityControlTemplate(validatedPage, enwp)
germanName = getGermanName(validatedPage)
germanACstatus = determineAuthorityControlTemplate(validatedPage, dewp)
acStatus = determineAuthorityControlTemplate(validatedPage)
try:
germanPageName = getGermanName(validatedPage)
except NoPage: #There was no German equivalent page
germanPageName = None
viafbotrun.write('No German equivalent')
if germanPageName: #Only need to get NormdatenStaus if a German equivalent page exists.
normdatenStatus = determineNormdatenTemplate(germanPageName)
else:
normdatenStatus = 'noACtemplate' #if there's no page there's also noACtemplate either



writeToWiki(ACstatus, germanACstatus)
writeToWiki(acStatus, normdatenStatus)
writeToLog()

origNameOfPage = wikilink[0]
Expand Down

0 comments on commit 6fa0e5f

Please sign in to comment.