All pastes #2128659 Raw Edit

Someone

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

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

class issBot:

    def __init__(self):
        self.updating_comment_in_lang = {
        'en': 'Robot: updating',
        'ru': u'Robot: updating'
        }
        self.title_in_lang = {
        'en': {
            'ISS': u'Template:ISSIB',
            'Tiangong 1': u'User:PALZ9000/Tiangong 1'
            },
        'ru': {
            'ISS':  u'Template:ISSIB',
            'Tiangong 1': u'User:PALZ9000/Tiangong 1'
            }
        }
        self.sat_urls = {
        'ISS': 'http://www.heavens-above.com/orbit.aspx?satid=25544',
        'Tiangong 1': 'http://www.heavens-above.com/orbit.aspx?satid=37820'
        }
        self.langs = self.title_in_lang.keys()
        self.sats = self.sat_urls.keys()

    def run(self):
        while True:
            for sat in self.sats:
                self.update(sat)
            time.sleep(3600*12)

    def update(self, sat):
        webpage = urllib2.urlopen(self.sat_urls[sat])
        soup = BeautifulSoup(webpage)
        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 self.langs:
            page = wikipedia.Page(wikipedia.getSite(lang), self.title_in_lang[lang][sat])
            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())

    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__':
    main()