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

این ربات برای استخراج مختصات از گوگل مپ و افزودن آنها به مقالات یک رده خاص، کاربرد دارد.

کتابخانه pygeocoder را از این آدرس بگیرید و در فولدر pywikipedia قرار دهید و کتابخانهٔ requests را از اینجا بگیرید و فولدر requests را در فولدر pywikipedia کپی کنید.

آرگومان ویرایش

به جز تغییر نام رده در خود متن ربات نیاز به هیچ آرگومان خاصی نیست.

روش کار ویرایش

باید تنها متغیر categoryname=u"روستاهای شهرستان اردبیل" را به نام رده مورد نظرتان تغییر دهید مثلا روستاهای شهرستان آباده تا مکان‌های موجود در رده را در گوگل مپ جستجو کند و در صورت موجود بودن مختصات آنها را در مقاله وارد کند. این ربات نام‌های با حروف عربی و بدون نیم‌فاصله را نیز جستجو می‌کند تا بهترین نتیجه را ارائه دهد.

<syntaxhighlight lang="python">

  1. !/usr/bin/python
  2. -*- coding: utf-8 -*-
  3. Reza(User:reza1615), 2014
  4. Distributed under the terms of the CC-BY-SA 3.0 .

import wikipedia,re import category,catlib,codecs,time wikipedia.config.put_throttle = 0 wikipedia.put_throttle.setDelay() from pygeocoder import Geocoder

  1. ------------------------------------------------category name-----------------------

categoryname=u"روستاهای شهرستان اردبیل" country=u'ایران' region=u'IR'

  1. ------------------------------------------------------------------------------------

def cleaner(citys):

       citys=citys.split(u'|')[0].replace(u'[[',u).strip()
       blacklistname=[u'شهرستان',u'بخش',u'روستای',u'روستا',u'دهستان']
       for item in blacklistname:
           citys=citys.replace(item,u)
       return citys

def googlemap(adress,test):

           if test==u'test':
               test=u'(for testing)'
           else:
               test=u' '
           timeforstop=5
           time.sleep(timeforstop)# sleep to have less errors
           wikipedia.output(u'getting coordination for Farsi name of %s ....%s' % (adress,test))
           #---farsi name--
           try:
               results = Geocoder.geocode(adress)
               lat,lan=results[0].coordinates
               return lat,lan
           except:
               pass
           time.sleep(timeforstop)# sleep to have less errors

           #---arabic name--
           try:
               adress1=adress.replace(u'ی',u'ي').replace(u'ک',u'ك')
               if adress1!=adress:
                   wikipedia.output(u'getting coordination for arabic name of %s ....%s' % (adress,test))
                   results = Geocoder.geocode(adress1)
                   lat,lan=results[0].coordinates
                   return lat,lan              
           except:
               pass
           time.sleep(timeforstop)# sleep to have less errors
           #---without Zwinj farsi name--

           try:
               adress2=adress.replace(u'‌',u' ')
               if adress2!=adress:
                   wikipedia.output(u'getting coordination for without Zwinj farsi name of %s ....%s' % (adress,test))        
                   results = Geocoder.geocode(adress2)
                   lat,lan=results[0].coordinates
                   return lat,lan            
           except:
               pass
           time.sleep(timeforstop)# sleep to have less errors
           #---without Zwinj arabic name--

           try:
               adress3=adress.replace(u'ی',u'ي').replace(u'ک',u'ك').replace(u'‌',u' ')
               if adress3!=adress1: 
                   wikipedia.output(u'getting coordination for without Zwinj arabic name of %s ....%s' % (adress,test))                
                   results = Geocoder.geocode(adress3)
                   lat,lan=results[0].coordinates
                   return lat,lan            
               else:
                   return False,False
           except:
               return False,False

def coordmaker(rpage,lat,lan,region):

       if str(rpage).find(u'ایستگاه')!=-1:
           types='|type:railwaystation'
       elif str(rpage).find(u'دانشگاه')!=-1:
           types='|type:edu'
       elif str(rpage).find(u'دانشکده')!=-1:
           types='|type:edu'
       elif str(rpage).find(u'شهرستان')!=-1:
           types=u'|type:landmark'
       elif str(rpage).find(u'مدرسه')!=-1:
           types='|type:edu'
       elif str(rpage).find(u'دبیرستان')!=-1:
           types='|type:edu'
       elif str(rpage).find(u'دبستان')!=-1:
           types=u'|type:edu'
       else:
           types=u'|type:city'
       coord=u'{{#coordinates:}}: عرض جغرافیایی نامعتبر'
       wikipedia.output(rpage)
       wikipedia.output(coord)
       return coord

def main(listOfArticles,country,region):

   for rpage in listOfArticles:
       wikipedia.output(u'-------------------------')
       wikipedia.output(u'getting '+rpage+u' ....')
       site = wikipedia.getSite('fa') 
       try:
           pagefa = wikipedia.Page( site,rpage )
           texttemple=pagefa.get(expandtemplates=True)
       except wikipedia.IsRedirectPage:
           pagefa = pagefa.getRedirectTarget()
           texttemple=pagefa.get(expandtemplates=True)
           rpage=pagefa.title().strip()
       except Exception,e:
               wikipedia.output(str(e))
               continue
       try:
           if texttemple.find('')!=-1 or texttemple.find('')!=-1 :
               wikipedia.output(u'it had coordination!')
               continue
           textfa=pagefa.get()
           try:
               imfa = re.search(ur'\|\s*(?:استان|بخش|شهرستان|دهستان).*?\=.*?\|', text_fa2)
               mothercity=imfa.group(0).split(u'=')[1].split(u'|')[0].replace(u'[[',u).strip()
               citys=mothercity+u' '+country
           except:
               citys=u' '+country
           rpage=cleaner(rpage)
           adress=rpage.replace(u'(',u' , ').replace(u')',u).replace(u'،',u',').replace(u'  ',u' ').replace(u'  ',u' ').replace(u', ,',u',').strip()
           if adress.find(citys.strip())==-1:
               adress+=u', '+citys
           citys=citys.strip()
           adress=adress.strip()
           lat,lan=googlemap(adress,u'main')
           if lat and lan:
               lat1,lan1=googlemap(citys,u'test')
           else:
               continue
           if lat1==lat and lan1==lan:
               continue
           if lat and lan:
               coord=coordmaker(rpage,lat,lan,region)
               try:
                   textfa=textfa.replace(u'',)
                   if textfa.find(u'{{مختصات-نیاز')!=-1:
                       mokhtasattemplate=u'{{مختصات-نیاز'+textfa.split(u'{{مختصات-نیاز')[1].split(u'}}')[0]+u'}}'
                       textfa=textfa.replace(mokhtasattemplate+u'\n',u)
                       textfa=textfa.replace(mokhtasattemplate,u)
                   textfa=coord+'\n'+textfa+u'\n'
                   pager=pagefa.put(textfa,u'ربات:افزودن مختصات برگرفته از گوگل مپ')      
                   wikipedia.output(rpage+u' done')
               except wikipedia.IsRedirectPage:
                   continue
               except Exception,e:
                   wikipedia.output(str(e))
                   continue
       except Exception,e:
           wikipedia.output(str(e))
           continue

if __name__ == "__main__":

   try:
       categoryname=categoryname.replace(u'رده:',u).strip()
       cat = catlib.Category(wikipedia.getSite('fa'),categoryname)
       listOfArticles = cat.articlesList()
       main(listOfArticles,country,region)
   finally:
       wikipedia.stopme()