ویکیپدیا:درخواستهای ربات/ربات مترجم/ویرایش ۱
ظاهر
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Reza(User:reza1615), 2011
#
# Distributed under the terms of the CC-BY-SA 3.0 .
import catlib
import pagegenerators
import wikipedia,add_text2
from xgoogle.translate import Translator
self=''
def main():
def __init__( self,generator,always,summary = None ):
self.generator = generator
self.always = always
self.dry = False
self.summary = summary
site = wikipedia.getSite()
cat = catlib.Category(site,'Category:Articles containing non-English language text')
gen = pagegenerators.CategorizedPageGenerator(cat)
for pageout in gen:
#Do something with the page object, for example:
text = pageout.get()
# Define the main function
pagename = u'ویکیپدیا:صفحه تمرین' # the storage page=============================
pageinput = wikipedia.Page(site, pagename)
wikipedia.output(u"Loading %s..." % pagename) # Please, see the "u" before the text
try:
text = pageout.get(force = False, get_redirect=False, throttle = True, sysop = False,change_edit_time = True) # text = page.get() <-- is the same
except wikipedia.NoPage: # First except, prevent empty pages
text = ''
except wikipedia.IsRedirectPage: # second except, prevent redirect
wikipedia.output(u'%s is a redirect!' % pagename)
exit()# wikipedia.stopme() is in the finally, we don't need to use it twice, exit() will only close the script
except wikipedia.Error: # third exception, take the problem and print
wikipedia.output(u"Some Error, skipping..")
exit()
#text = pageout.title()
comment=u'ربات' # توضيحات ربات
reza=1
assert type(text) == type(u''), "Expects input to be unicode."
#==============================================translator=======
text=text.encode('utf-8')
retText=''
for text in getSplits(text):
text3=text
translate = Translator().translate
text2=translate(text3, lang_to="en")
print text2
add_text2.add_text( pageinput,text2,None,None) # the storage page=================only change text2=======
def getSplits(text,splitLength=700):
''' Translate Api has a limit on length of text(4500 characters)
that can be translated at once, '''
return (text[index:index+splitLength]
for index in xrange(0,len(text),splitLength))
if __name__ == '__main__':
try:
main()
finally:
wikipedia.stopme()