پرش به محتوا

ویکی‌پدیا:درخواست‌های ربات/ربات مختصات جغرافیا/ویرایش ۳

از ویکی‌پدیا، دانشنامهٔ آزاد

این کد بر اساس ترتیب الفبا مقالات را جستجو میکند.

#!/usr/bin/python
# -*- coding: utf-8  -*-
#
# Reza(User:reza1615), 2011
#
# Distributed under the terms of the CC-BY-SA 3.0 .
import catlib ,pprint ,pagegenerators
import wikipedia,add_text
import codecs,time,string,re
from dictionaryV  import coorddar
site = wikipedia.getSite( 'fa' )
enlink,falink,newtext = u' ',u' ',u' '
latd,latm,lats,latNS,longd,longm,longs,longEW=0,0,0,0,0,0,0,0
count = 0
startpage = u'سرخه'
for page in site.allpages( startpage ): # Use a generetor object, this will yield all pages one by one
        farsilink = str( page ).replace( '[','' )
        farsilink = farsilink.replace( ']','' ).strip()
 
        if farsilink in coorddar:
            continue
        pagename = page.title() # Take the title of the page (not "[[page]]" but "page")
        wikipedia.output( u"Loading %s..." % pagename ) # Please, see the "u" before the text
        try:
            atext = page.get() # Taking the text of the page
        except wikipedia.NoPage: # First except, prevent empty pages
            continue
        except wikipedia.IsRedirectPage: # second except, prevent redirect
            wikipedia.output( u'%s is a redirect!' % pagename )
            continue
        except wikipedia.Error: # third exception, take the problem and print
            wikipedia.output( u"Some error, skipping.." )
            continue     
 
        if atext.find( u'[[en:' ) == -1:
            continue
        else:
            enlink = atext.split( '[[en:' )[1]
            enlink = enlink.split( ']]' )[0]
            site = wikipedia.getSite( u'en' )
            page = wikipedia.Page( site,enlink )
            try:
                text = page.get()
            except wikipedia.IsRedirectPage:
                page = page.getRedirectTarget()
                try:
                    text = page.get()
                except:
                    continue
            except wikipedia.NoPage: # First except, prevent empty pages
                continue
            except wikipedia.Error: # third exception, take the problem and print
                continue   
            try:
                textcoord = text.split( '{{coord|' )[1]
                textcoord = textcoord.split( '}}' )[0]
                textcoord = u'{{coord|' + textcoord + u'}}'
                count = count + 1
                falink = str( pagename )
                pprint.pprint( falink )
                textresult = u'AAA\nXXX%sXXX\n%s\nBBB\n' % ( falink,textcoord )
                newtext = newtext + textresult
                with codecs.open( 'resultr2.txt',mode = 'a',encoding = 'utf8' ) as f:
                        f.write( textresult )  
                f.close()
                continue
                
            except:
                    a1=1
            for a in range(0,3):
               text=text.replace(' ','').strip()
           
            if  text.find( '|latd=' )!=-1:
                #--------------------------------------------------------coord -------
                textcoord = text.split( '|latd=' )[1]
                textcoord = textcoord.split( '|' )[0]
                textcoord=textcoord.replace('=','').strip()
                latd=textcoord.replace('\n','').strip()
                if latd=='':
                   latd=0
            else:
                continue
                 #--------------------------------------------------------coord -------
            if  text.find( '|longd=' )!=-1:
                textcoord = text.split( '|longd=' )[1]
                textcoord = textcoord.split( '|' )[0]
                textcoord=textcoord.replace('=','').strip()
                longd=textcoord.replace('\n','').strip()
                if longd=='':
                   longd=0
            else:
                continue
                #--------------------------------------------------------coord -------
            if  text.find( '|latm=' )!=-1:
                textcoord = text.split( '|latm=' )[1]
                textcoord = textcoord.split( '|' )[0]
                textcoord=textcoord.replace('=','').strip()
                latm=textcoord.replace('\n','').strip()
                if latm=='':
                   latm=0
            else:
                 latm=0
                 #--------------------------------------------------------coord -------
            if  text.find( '|lats=' )!=-1:
                textcoord = text.split( '|lats=' )[1]
                textcoord = textcoord.split( '|' )[0]
                textcoord=textcoord.replace('=','').strip()
                lats=textcoord.replace('\n','').strip()
                if lats=='':
                   lats=0
            else:
                 lats=0
                #--------------------------------------------------------coord -------
            if  text.find( '|latNS=' )!=-1:
                textcoord = text.split( '|latNS=' )[1]
                textcoord = textcoord.split( '|' )[0]
                textcoord=textcoord.replace('=','').strip()
                latNS=textcoord.replace('\n','').strip()
                if latNS=='':
                  latNS==' '
            else:
                 latNS==' '
           
                #--------------------------------------------------------coord -------
            if  text.find( '|longm=' )!=-1:
                textcoord = text.split( '|longm=' )[1]
                textcoord = textcoord.split( '|' )[0]
                textcoord=textcoord.replace('=','').strip()
                longm=textcoord.replace('\n','').strip()
                if longm=='':
                   longm=0
            else:
                longm=0
                #--------------------------------------------------------coord -------
            if  text.find( '|longs=' )!=-1:
                textcoord = text.split( '|longs=' )[1]
                textcoord = textcoord.split( '|' )[0]
                textcoord=textcoord.replace('=','').strip()
                longs=textcoord.replace('\n','').strip()
                if longs=='':
                   longs=0
            else:
                longs=0
                #--------------------------------------------------------coord -------
            if  text.find( '|longEW=' )!=-1:
                textcoord = text.split( '|longEW=' )[1]
                textcoord = textcoord.split( '}}' )[0]
                textcoord = textcoord.split( '|' )[0]
                textcoord=textcoord.replace('=','').strip()
                longEW=textcoord.replace('\n','').strip()
                if longEW=='':
                   longEW==' '
            else:
                longEW==' '
                 #--------------------------------------------------------coord -------
            if  text.find( '|longd=' )!=-1:
                textcoord = u'{{coord|%s|%s|%s|%s|%s|%s|%s|%s|display=title}}' % (latd,latm,lats,latNS,longd,longm,longs,longEW)
                count = count + 1
                falink = str( pagename )
                pprint.pprint( falink +'bingo'+textcoord)
                textresult = u'AAA\nXXX%sXXX\n%s\nBBB\n' % ( falink,textcoord )
                newtext = newtext + textresult
                with codecs.open( 'resultr2.txt',mode = 'a',encoding = 'utf8' ) as f:
                        f.write( textresult )  
                f.close()
                continue