All pastes #2128532 Raw Edit

Someone

public text v1 · immutable
#2128532 ·published 2012-03-16 02:02 UTC
rendered paste body
#!/usr/bin/python
# -*- coding: utf-8 -*-

__version__ = '$Id: ISS-infobox.py$'

import wikipedia, urllib2
from BeautifulSoup import BeautifulSoup
import re, codecs, time

# SETTING
title_in_lang = {
'en': u'Template:ISSIB',
'ru': u'Template:ISSIB'
}
updating_comment_in_lang = {
'en': 'Robot: updating',
'ru': u'Robot: updating'
}
ha_url = 'http://www.heavens-above.com/orbit.aspx?satid=25544&lat=-33.1069'
langs = title_in_lang.keys()

class issBot:

  def run(self):
    while True:
      ha = urllib2.urlopen(ha_url)
      soup = BeautifulSoup(ha)
      epoch = soup.find(attrs={'id':'ctl00_ContentPlaceHolder1_lblEpoch'}).renderContents()
      eccentricity = soup.find(attrs={'id':'ctl00_ContentPlaceHolder1_lblEccentricity'}).renderContents()
      inclination = soup.find(attrs={'id':'ctl00_ContentPlaceHolder1_lblInclination'}).renderContents()
      perigee_height = soup.find(attrs={'id':'ctl00_ContentPlaceHolder1_lblPerigee'}).renderContents()
      perigee_height = re.search('([\d\.]+) *km', perigee_height).group(1)
      apogee_height = soup.find(attrs={'id':'ctl00_ContentPlaceHolder1_lblApogee'}).renderContents()
      apogee_height = re.search('([\d\.]+) *km', apogee_height).group(1)
      right_ascension_of_ascending_node = soup.find(attrs={'id':'ctl00_ContentPlaceHolder1_lblNode'}).renderContents()
      argument_of_perigee = soup.find(attrs={'id':'ctl00_ContentPlaceHolder1_lblArgP'}).renderContents()
      revolution_per_day = soup.find(attrs={'id':'ctl00_ContentPlaceHolder1_lblRevsPerDay'}).renderContents()
      mean_anomaly_at_epoch = soup.find(attrs={'id':'ctl00_ContentPlaceHolder1_lblMA'}).renderContents()
      orbit_number_at_epoch = soup.find(attrs={'id':'ctl00_ContentPlaceHolder1_lblOrbitNum'}).renderContents()
      text = '''\
{{#switch:{{{1}}}
|epoch=%s
|eccentricity=%s
|inclination=%s
|perigee_height=%s
|apogee_height=%s
|right_ascension_of_ascending_node=%s
|argument_of_perigee=%s
|revolution_per_day=%s
|mean_anomaly_at_epoch=%s
|orbit_number_at_epoch=%s
}}''' % (epoch, eccentricity, inclination, perigee_height, apogee_height, right_ascension_of_ascending_node, argument_of_perigee, revolution_per_day, mean_anomaly_at_epoch, orbit_number_at_epoch)
      for lang in langs:
        page = wikipedia.Page(wikipedia.getSite(lang), title_in_lang[lang])
        if not self.save(text, page, comment=updating_comment_in_lang[lang]):
          self.error_report(error_text=u"I can't save the page [[%s|here]], something is wrong (it can only be attributed to human error obviously)." % page.title())
      time.sleep(3600*12) # sleep for 12 hours

  def load(self, page):
    try:
      text = page.get()
    except wikipedia.NoPage:
      wikipedia.output(u'ERROR: Page %s does not exist.' % page.title(asLink=True))
      wikipedia.stopme()
    except wikipedia.IsRedirectPage:
      wikipedia.output(u'ERROR: Page %s is a redirect.' % page.title(asLink=True))
    else:
      return text
    return None

  def save(self, text, page, comment=''):
    try:
      page.put(text, comment=comment, minorEdit=True, botflag=True)
    except wikipedia.LockedPage:
      wikipedia.output(u'ERROR: Page %s is locked.' % page.title(asLink=True))
    except wikipedia.EditConflict:
      wikipedia.output(u'ERROR: Skipping %s because of edit conflict' % page.title())
    except wikipedia.SpamfilterError, error:
      wikipedia.output(u'ERROR: Cannot change %s because of spam blacklist entry %s' % (page.title(), error.url))
    else:
      return True
    return False

  def error_report(self, error_title='Robot: error', error_text='something went wrong', comment='Robot: reporting error', user='ZxxZxxZ', site='en', user2='Penyulap', site2='en'):
    wikipedia.output(u'Reporting error, title: %s, text: %s' % (error_title, error_text))
    talk_page = wikipedia.Page(wikipedia.getSite(site), user)
    text = self.load(talk_page)
    text += u'''
== %s ==
Hi %s! %s. --~~~~
''' % (error_title, user, error_text)
    if not self.save(text, talk_page, comment=comment):
      talk_page2 = wikipedia.Page(wikipedia.getSite(site2), user2)
      text2 = self.load(talk_page2)
      text2 += u'''
== %s ==
Hi %s! %s --~~~~
''' % (error_title, user2, error_text)
      if not self.save(text2, talk_page2, comment=comment):
        wikipedia.output('ERROR: Error reporting failed; pages not saved.')
        wikipedia.stopme()

def main():
  bot = issBot()
  bot.run()

if __name__ == '__main__':
  try:
    main()
  finally:
    wikipedia.stopme()