Advertisement
Guest User

en category

a guest
Oct 11th, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.67 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Reza(User:reza1615), 2011
  5. #
  6. # Distributed under the terms of the CC-BY-SA 3.0 .
  7. # -*- coding: utf-8 -*-
  8. """
  9. -file - Work on all pages given in a local text file.
  10. Will read any [[wiki link]] and use these articles.
  11. Argument can also be given as "-file:filename".
  12. -cat - Work on all pages which are in a specific category.
  13. Argument can also be given as "-cat:categoryname".
  14. -encat - Finding the pages that uses the category in en.wikipedia and add categories to pages that they have interwiki in en wiki to home wiki
  15. -recentcat - Useful for automatic bots and it should add with -namespace:14 -recenchanges:N (N=Number of categories that you want to work on)
  16. -newcat - Adding article to new categories -newcat:N (N=Number of categories that you want to work on)
  17. -page - Only edit a specific page.
  18. Argument can also be given as "-page:pagetitle". You can give this
  19. parameter multiple times to edit multiple pages.
  20. -ref - Work on all pages that link to a certain page.
  21. Argument can also be given as "-ref:referredpagetitle".
  22. -filelinks - Works on all pages that link to a certain image.
  23. Argument can also be given as "-filelinks:ImageName".
  24. -links - Work on all pages that are linked to from a certain page.
  25. Argument can also be given as "-links:linkingpagetitle".
  26. -start - Work on all pages in the wiki, starting at a given page. Choose
  27. "-start:!" to start at the beginning.
  28. NOTE: You are advised to use -xml instead of this option; this is
  29. meant for cases where there is no recent XML dump.
  30. -except:XYZ - Ignore pages which contain XYZ. If the -regex argument is given,
  31. XYZ will be regarded as a regular expression.
  32. -summary:XYZ - Set the summary message text for the edit to XYZ, bypassing the
  33. predefined message texts with original and replacements inserted.
  34. -template:XYZ-
  35. -blog: -checking for blog sources. if it is in page it will sent page link to defined address
  36. -source - checking the articles sources . if it doesn't have . it will send page link to defined address
  37. -namespace:n - Number of namespace to process. The parameter can be used
  38. multiple times. It works in combination with all other
  39. parameters, except for the -start parameter. If you e.g. want to
  40. iterate over all user pages starting at User:M, use
  41. -start:User:M.
  42. -always - Don't prompt you for each replacement
  43. other: -
  44.  
  45. """
  46.  
  47. import catlib,query
  48. import pagegenerators,re,sys#,fa_cosmetic_changes
  49. import wikipedia,codecs,string
  50. from xml.dom import minidom
  51. wikipedia.config.put_throttle = 0
  52. wikipedia.put_throttle.setDelay()
  53. #-----------------------------------------------version-----------------------------------------
  54. try:
  55. import MySQLdb
  56. except:
  57. wikipedia.output(u'\03{lightred}you should use this code only on toolserver\03{default}')
  58. wikipedia.stopme()
  59. sys.exit()
  60. versionpage = wikipedia.Page( wikipedia.getSite('ar'),u'مستخدم:Elph/التصانیف المعادلة/version' )
  61. lastversion=versionpage.get().strip()
  62. version=u'22'
  63. if lastversion!=version:
  64. wikipedia.output(u"\03{lightred}Your bot dosen't use the last verion please update me!\03{default}")
  65. wikipedia.stopme()
  66. sys.exit()
  67. #-----------------------------------------------------------------------------------------------
  68. langs = u'انگلیسی|آلمانی|اسپانیایی|فرانسوی|روسی|ایتالیایی|لاتین|اسکاتلندی|هلندی|هندی|اردو|پشتو|مصری|تاجیکستانی|قرقیزستانی|ازبکستانی|ترکی|یونانی|چینی|ژاپنی|کره‌ای|تایلندی|[فپ]ارسی'
  69.  
  70. def namespacefinder( enlink ,firstsite):
  71. try:
  72. enlink=unicode(str(enlink),'UTF-8').replace(u'[[',u'').replace(u']]',u'').replace(u'en:',u'').replace(u'ur:',u'')
  73. except:
  74. enlink=enlink.replace(u'[[',u'').replace(u']]',u'').replace(u'en:',u'').replace(u'ur:',u'')
  75. enlink=enlink.replace(u' ',u'_')
  76. site = wikipedia.getSite(firstsite)
  77. params = {
  78. 'action': 'query',
  79. 'prop': 'langlinks',
  80. 'titles': enlink,
  81. 'redirects': 1,
  82. 'lllimit':500,
  83. }
  84. a=1
  85. if a:
  86. categoryname = query.GetData(params,site, encodeTitle = True)
  87. for item in categoryname[u'query'][u'pages']:
  88. fanamespace=categoryname[u'query'][u'pages'][item]['ns']
  89. return fanamespace
  90. else:
  91. return False
  92.  
  93. def englishdictionry( enlink ,firstsite,secondsite):
  94. try:
  95. enlink=unicode(str(enlink),'UTF-8').replace(u'[[',u'').replace(u']]',u'').replace(u'en:',u'').replace(u'ur:',u'')
  96. except:
  97. enlink=enlink.replace(u'[[',u'').replace(u']]',u'').replace(u'en:',u'').replace(u'ur:',u'')
  98. if enlink.find('#')!=-1:
  99. return False
  100. if enlink==u'':
  101. return False
  102. enlink=enlink.replace(u' ',u'_')
  103. site = wikipedia.getSite(firstsite)
  104. sitesecond= wikipedia.getSite(secondsite)
  105. params = {
  106. 'action': 'query',
  107. 'prop': 'langlinks',
  108. 'titles': enlink,
  109. 'redirects': 1,
  110. 'lllimit':500,
  111. }
  112. try:
  113. categoryname = query.GetData(params,site, encodeTitle = True)
  114. for item in categoryname[u'query'][u'pages']:
  115. case=categoryname[u'query'][u'pages'][item][u'langlinks']
  116. for item in case:
  117. if item[u'lang']==secondsite:
  118. intersec=item[u'*']
  119. break
  120. result=intersec
  121. if result.find('#')!=-1:
  122. return False
  123. return result
  124. except:
  125. return False
  126. def catquery(enlink,firstsite,hidden):
  127. cats=[]
  128. try:
  129. enlink=unicode(str(enlink),'UTF-8').replace(u'[[',u'').replace(u']]',u'').replace(u'en:',u'').replace(u'ur:',u'')
  130. except:
  131. enlink=enlink.replace(u'[[',u'').replace(u']]',u'').replace(u'en:',u'').replace(u'ur:',u'')
  132. enlink=enlink.split(u'#')[0].strip()
  133. if enlink==u'':
  134. return False
  135. enlink=enlink.replace(u' ',u'_')
  136. site = wikipedia.getSite(firstsite)
  137. if hidden:
  138. params = {
  139. 'action': 'query',
  140. 'prop': 'categories',
  141. 'titles': enlink,
  142. 'redirects': 1,
  143. 'cllimit':500,
  144. }
  145. else:
  146. params = {
  147. 'action': 'query',
  148. 'prop': 'categories',
  149. 'titles': enlink,
  150. 'redirects': 1,
  151. 'clshow':'!hidden',
  152. 'cllimit':500,
  153. }
  154. try:
  155. categoryname = query.GetData(params,site, encodeTitle = True)
  156. for item in categoryname[u'query'][u'pages']:
  157. categoryha=categoryname[u'query'][u'pages'][item][u'categories']
  158. break
  159. for cat in categoryha:
  160. cats.append(cat[u'title'])
  161. return cats
  162. except:
  163. return False
  164. def templatequery(enlink,firstsite):
  165. temps=[]
  166. try:
  167. enlink=unicode(str(enlink),'UTF-8').replace(u'[[',u'').replace(u']]',u'').replace(u'en:',u'').replace(u'ur:',u'')
  168. except:
  169. enlink=enlink.replace(u'[[',u'').replace(u']]',u'').replace(u'en:',u'').replace(u'ur:',u'')
  170. enlink=enlink.split(u'#')[0].strip()
  171. if enlink==u'':
  172. return False
  173. enlink=enlink.replace(u' ',u'_')
  174. site = wikipedia.getSite(firstsite)
  175. params = {
  176. 'action': 'query',
  177. 'prop':'templates',
  178. 'titles': enlink,
  179. 'redirects': 1,
  180. 'tllimit':500,
  181. }
  182.  
  183. try:
  184. categoryname = query.GetData(params,site, encodeTitle = True)
  185. for item in categoryname[u'query'][u'pages']:
  186. templateha=categoryname[u'query'][u'pages'][item][u'templates']
  187. break
  188. for temp in templateha:
  189. temps.append(temp[u'title'].replace(u'_',u' '))
  190. return temps
  191. except:
  192. return False
  193. def subcatquery(enlink,firstsite):
  194. cats=[]
  195. try:
  196. enlink=unicode(str(enlink),'UTF-8').replace(u'[[',u'').replace(u']]',u'').replace(u'en:',u'').replace(u'ur:',u'')
  197. except:
  198. enlink=enlink.replace(u'[[',u'').replace(u']]',u'').replace(u'en:',u'').replace(u'ur:',u'')
  199. enlink=enlink.split(u'#')[0].strip()
  200. if enlink==u'':
  201. return False
  202. enlink=enlink.replace(u' ',u'_')
  203. site = wikipedia.getSite(firstsite)
  204. params = {
  205. 'action': 'query',
  206. 'list': 'categorymembers',
  207. 'cmtitle': enlink,
  208. 'cmtype':'subcat',
  209. 'cmlimit': 500,
  210. }
  211. try:
  212. categoryname = query.GetData(params,site, encodeTitle = True)
  213. for item in categoryname[u'query'][u'categorymembers']:
  214. categoryha=item[u'title']
  215. wikipedia.output(categoryha)
  216. cats.append(categoryha)
  217. if cats!=[]:
  218. return cats
  219. except:
  220. return False
  221. def sitop(link,wiki):
  222. link=link.replace( u'[[',u'' ).replace( u']]',u'' ).strip()
  223. wikipedia.config.put_throttle = 0
  224. wikipedia.put_throttle.setDelay()
  225. site = wikipedia.getSite(wiki)
  226. try:
  227. page = wikipedia.Page( site,link )
  228. if wiki=='ur':
  229. cats=page.categories(api=True)
  230. else:
  231. cats=page.categories()
  232. return cats
  233. except wikipedia.IsRedirectPage:
  234. page = page.getRedirectTarget()
  235. try:
  236. cats=page.categories()
  237. return cats
  238. except:
  239. return False
  240. except:
  241. return False
  242. def category(PageTitle,koltuple):
  243. counters=0
  244. PageTitle=PageTitle.replace( u'[[',u'' ).replace( u']]',u'' ).strip()
  245. listacategory=[PageTitle]
  246. listacategory2=[]
  247. for catname in listacategory:
  248. counters+=1
  249. if counters>150:
  250. break
  251. gencat=subcatquery(catname,'ur')#---function
  252.  
  253. if gencat:
  254. if len (gencat)>100:
  255. continue
  256. for subcat in gencat:
  257. subcat2=u'[[ur:'+subcat+u']]'
  258. if subcat in listacategory:
  259. continue
  260. else:
  261. listacategory.append(subcat)
  262. if subcat2 in koltuple:
  263. listacategory2.append(subcat2)
  264. listacategory2.append(' ')
  265. return listacategory2
  266. if listacategory==[]:
  267. return False
  268. else:
  269. return listacategory
  270. def pagefafinder(encatTitle):
  271.  
  272. cats=[]
  273. try:
  274. item=unicode(str(encatTitle),'Ascii').replace('[[en:','').replace(']]','').replace(' ','_').replace('Category:','')
  275. except:
  276. item=str(encatTitle).replace('[[en:','').replace(']]','').replace(' ','_').replace('Category:','')
  277. for raw in results:
  278. cats.append(raw)
  279. if cats!=[]:
  280. return cats
  281. else:
  282. return False
  283. def duplic(catfa,radeh):
  284. catfa=catfa.replace(u'ur:',u'')
  285. radeht=u' '
  286. if len(radeh.strip())<1 :
  287. return False
  288. if len(catfa.replace(u',',u'').strip())<1 :
  289. return radeh
  290. for x in catfa.split(','):
  291. catfax=x.split('|')[0].split(']]')[0].replace('[[','').strip()
  292. for y in radeh.split(','):
  293. radehy=y.split('|')[0].split(']]')[0].replace('[[','').strip()
  294. if catfax==radehy:
  295. radeh= radeh.replace(y,'')
  296. break
  297. for rad in radeh.split(','):
  298. radeht+=rad+'\n'
  299. return radeht
  300. def pedar(catfa,radehi,link):
  301. link=link.replace( u'[[',u'' ).replace( u']]',u'' ).strip()
  302. hazflist=catfa
  303. if englishdictionry( link ,'ur','en')==False :
  304. return hazflist
  305. radehi=radehi.replace('\n\n','\n').replace('\n\n','\n').replace('\n\n','\n').replace('\n\n','\n').replace('\n\n','\n').strip()
  306. kol=catfa.strip()+radehi.replace('\n',',').strip()
  307. kol=kol.replace(',,',',').strip()
  308. radehtest=radehi.replace('\n',',').replace(',,',',').strip().split(',')
  309. koltuple=kol.split(',')
  310. for x in range(0,len(radehtest)):
  311. if radehtest[x].find(u'مقالات')!=-1:
  312. continue
  313. catslistx=category(radehtest[x],koltuple)#----------category function
  314. if catslistx==False :
  315. continue
  316. for y in range(0,len(koltuple)):
  317. if radehi.find(radehtest[x])==-1:
  318. break
  319. for catlis in catslistx:
  320. try:
  321. catlis=unicode(str(catlis),'UTF-8').strip()
  322. except:
  323. catlis=catlis.strip()
  324. if koltuple[y].strip() == catlis:
  325. if radehi.find(radehtest[x])!=-1:
  326. hazfi=radehtest[x].replace(u'[[',u'').replace(u']]',u'').replace(u'زمرہ:',u'').replace(u'Category:',u'').strip()
  327. try:
  328. hazfi = re.search(u'\[\[ *(?:[Cc]ategory|زمرہ) *:*%s*(?:\|.*?|)\]\]'% hazfi, radehi).group(0)
  329. radehi=radehi.replace(hazfi,'')
  330. except:
  331. radehi=radehi.replace(u'(',u' اااا ').replace(u')',u' بببب ')
  332. hazfi=hazfi.replace(u'(',u' اااا ').replace(u')',u' بببب ')
  333. hazfi = re.search(u'\[\[ *(?:[Cc]ategory|زمرہ) *:*%s*(?:\|.*?|)\]\]'% hazfi, radehi).group(0)
  334. radehi=radehi.replace(hazfi,'')
  335. radehi=radehi.replace(u' اااا ',u'(').replace(u' بببب ',u')')
  336. radehi=radehi.replace('\n\n','\n').strip()
  337. break
  338. radehi=radehi.replace(',','\n').strip()
  339. return radehi
  340.  
  341. def run(gen):
  342.  
  343. for pagework in gen:
  344. radehf,catsfas,maghalehen,radeh,finallRadeh=' ',' ',' ',' ',' '
  345. shod=False
  346. try:
  347. pagework=unicode(str(pagework),'UTF-8')
  348. except:
  349. pagework=pagework
  350. wikipedia.output(u'-----------------------------------------------')
  351. wikipedia.output(u'opening....'+pagework)
  352. catsfa=sitop(pagework,'ur')
  353. if catsfa==False :
  354. continue
  355. for tem in catsfa:
  356. if unicode(str(tem),'UTF-8').find(u'تصنيف:مقالات أنشئت بواسطة معالج إنشاء المقالات‏')!=-1:
  357. continue
  358. catsfas+=unicode(str(tem),'UTF-8')+','
  359. maghalehen=englishdictionry( pagework ,'ur','en')
  360. if not maghalehen:
  361. continue
  362. pageblacklist=[u'Sandbox']
  363. passing=True
  364. for item in pageblacklist:
  365. if maghalehen.find(item.lower())!=-1:
  366. passing=False
  367. break
  368. if not passing:
  369. continue
  370. if namespacefinder(maghalehen ,'en')!=namespacefinder(pagework ,'ur'):
  371. wikipedia.output(u"\03{lightred}Interwikis have'nt the same namespace\03{default}")
  372. continue
  373. catsen=catquery(maghalehen,'en',False)
  374. if not catsen:
  375. continue
  376. templateblacklist=[u'loanword',u'Wikipedia category',u'sockpuppet',u'Empty category',u'tracking category',u'container category',u'hiddencat',u'backlog subcategories',u'Stub category']
  377. nameblcklist=[u'Current events',u'Tracking',u'articles‎',u'Surnames',u'Loanword',u'Words and phrases',u'Given names',u'Human names',u'stubs‎',u'Nicknames',u'Living people']
  378. for cat in catsen:
  379. passport=True
  380. temples=str(templatequery(cat,'en')).replace( u'_',u' ' ).strip()
  381. cat=cat.replace( u'_',u' ' ).strip()
  382. for black in templateblacklist:
  383. if temples.lower().find(black.lower())!=-1:
  384. passport=False
  385. break
  386. for item in nameblcklist:
  387. if cat.lower().find(item.lower())!=-1:
  388. passport=False
  389. break
  390. if not passport:
  391. continue
  392. interwikifarsibase=englishdictionry( cat ,'en','ur')
  393. if interwikifarsibase:
  394. if interwikifarsibase.find(u',')!=-1:
  395. try:
  396. siteerror=wikipedia.getSite('ur')
  397. errorpage=wikipedia.Page(siteerror,u'user:Rezabot/CategoriesWithBadNames')
  398. texterror=errorpage.get()
  399. if texterror.find(interwikifarsibase)==-1:
  400. texterror+=u'\n#[[:'+interwikifarsibase+u']]'
  401. errorpage.put(texterror,u'روبہ: غلط نام کا اعلان')
  402. continue
  403. except:
  404. continue
  405. interwikifarsi=u'[['+interwikifarsibase+u']]'
  406. if cat ==englishdictionry( interwikifarsibase ,'ur','en'):
  407. radeh+=interwikifarsi+u','
  408. radehf=duplic(catsfas,radeh)
  409. if radehf==False:
  410. continue
  411. radehf=radehf.replace('\n\n','\n').replace('\n\n','\n').replace('\n\n','\n').replace('\n\n','\n').strip()
  412. if radehf=="":
  413. continue
  414. if catsfas.strip()!=u'':
  415. finallRadeh=pedar(catsfas,radehf,pagework)
  416. else:
  417. finallRadeh=radehf.replace(',','\n')
  418. if finallRadeh==False:
  419. continue
  420. if finallRadeh.replace('\n','').strip()=='':
  421. continue
  422. if finallRadeh:
  423. site = wikipedia.getSite('ur')
  424. link=pagework.replace( u'[[',u'' ).replace( u']]',u'' ).strip()
  425. page = wikipedia.Page( site,link )
  426.  
  427. try:
  428. text=page.get()
  429. except wikipedia.IsRedirectPage:
  430. page = page.getRedirectTarget()
  431. text=page.get()
  432. except:
  433. wikipedia.output(u'\03{lightred}Could not open page\03{default}')
  434. continue
  435. namespaceblacklist=[1,2,3,5,7,8,9,11,13,15,101,103]
  436. if page.namespace() in namespaceblacklist:
  437. continue
  438. if text.find(u'{{مساوی زمرہ کے لیے نہیں}}')!=-1 or text.find(u'{{سانچہ:مساوی زمرہ کے لیے نہیں}}')!=-1 or text.find(u'{{مساوی زمرہ کے لیے نہیں}}')!=-1 or text.find(u'{{تصنيف بهتر')!=-1:
  439. wikipedia.output(u'\03{lightred}this page had {{مساوی زمرہ کے لیے نہیں}} so it is skipped!\03{default}' )
  440. continue
  441. #---------------------------------------remove repeative category-----------------
  442.  
  443. for item in finallRadeh.split(u'\n'):
  444. item2=item.split(u'|')[0].replace(u'[[',u'').replace(u']]',u'').strip()
  445. radehbehtar=templatequery(item2,'ur')
  446. if radehbehtar:
  447. if (u'قالب:تحويل تصنيف' in radehbehtar) or (u'قالب:delete' in radehbehtar) or (u'قالب:حذف' in radehbehtar):
  448. wikipedia.output(u'\03{lightred}Category %s had {{تحويل تصنيف}} or {{delete}} so it is skipped! please edit en.wiki interwiki\03{default}' % item2)
  449. finallRadeh=finallRadeh.replace(item,u'').replace(u'\n\n',u'\n')
  450. continue
  451. textremove=text.replace(u' |',u'|').replace(u' |',u'|').replace(u' |',u'|').replace(u'| ',u'|').replace(u'| ',u'|')
  452. if textremove.find(u'{{لا للتصنيف الميلادي}}')!=-1 or textremove.find(u'{{لا للتصنيف الميلادي}}')!=-1:
  453. if item.find(u'(الميلادي)')!=-1 or item.find(u'(قبل الميلاد)')!=-1 or item.find(u'(قبل الميلاد)')!=-1:
  454. finallRadeh=finallRadeh.replace(item,u'').replace(u'\n\n',u'\n')
  455. if textremove.find(item2+u']]')!=-1 or textremove.find(item2+u'|')!=-1:
  456. finallRadeh=finallRadeh.replace(item,u'').replace(u'\n\n',u'\n')
  457.  
  458. if finallRadeh.replace(u'\r',u'').replace(u'\n',u'').strip()==u'':
  459. continue
  460. finallRadeh=finallRadeh.replace(u'\r',u'').replace(u'\n\n\n\n',u'\n').replace(u'\n\n\n',u'\n').replace(u'\n\n',u'\n').replace(u'\n\n',u'\n').replace(u'\n\n',u'\n').replace(u'\n\n',u'\n').replace(u'\n\n',u'\n').replace(u'\n\n',u'\n') #---------------------------------------------------------------------------------
  461. if text.find(ur'تصنيف:')!=-1 and page.namespace()!=10:
  462. num=text.find(ur'[[تصنيف:')
  463. text=text[:num]+finallRadeh+'\n'+text[num:]
  464. else:
  465. m = re.search(ur'\[\[([a-z]{2,3}|[a-z]{2,3}\-[a-z\-]{2,}|simple):.*?\]\]', text)
  466. if m:
  467. if m.group(0)==u'[[en:Article]]':
  468. try:
  469. if string.count(text,u'[[en:Article]] --->')==1:
  470. finallRadeh=finallRadeh.replace(u'\r',u'').replace(u'\n\n\n\n',u'\n').replace(u'\n\n\n',u'\n').replace(u'\n\n',u'\n').replace(u'\n\n',u'\n').replace(u'\n\n',u'\n').replace(u'\n\n',u'\n').replace(u'\n\n',u'\n').replace(u'\n\n',u'\n').strip()
  471. text=text.split(u'[[en:Article]] --->')[0]+u'[[en:Article]] --->\n'+finallRadeh+text.split(u'[[en:Article]] --->')[1]
  472. else:
  473. if page.namespace()==10:
  474. continue
  475. text+='\n'+finallRadeh
  476. except:
  477. if page.namespace()==10:
  478. continue
  479. text+='\n'+finallRadeh
  480. else:
  481. num=text.find(m.group(0))
  482. text=text[:num]+finallRadeh+'\n'+text[num:]
  483. else:
  484. if page.namespace()==10:
  485. continue
  486. text+='\n'+finallRadeh
  487. wikipedia.output(u'\03{lightpurple} Added=='+finallRadeh+u"\03{default}")
  488. radehfmsg=finallRadeh.strip().replace(u'\n',u'+')
  489. if len(radehfmsg.split(u'+'))>4:
  490. numadd=str(len(radehfmsg.split(u'+')))#.replace(u'0',u'۰').replace(u'1',u'۱').replace(u'2',u'۲').replace(u'3',u'۳').replace(u'4',u'۴').replace(u'5',u'۵').replace(u'6',u'۶').replace(u'7',u'۷').replace(u'8',u'۸').replace(u'9',u'۹')
  491. radehfmsg=u' %s تصنيف'% numadd
  492. msg=u'روبہ [[صارف:محمد شعيب/زمرہ جات#'+version+u'|مساوي زمرہ جات ('+version+u')]]: + %s'
  493. text_new=text
  494. if page.namespace()==0: #----------------cleaning
  495. #text_new,cleaning_version,msg_clean=fa_cosmetic_changes.fa_cosmetic_changes(text,page)
  496. text_new,msg_clean=(text,page)
  497. else:
  498. msg_clean=u' '
  499. #msg=msg %(msg_clean,radehfmsg)
  500. msg=msg %(radehfmsg)
  501. msg=msg.replace(u' ',u' ').strip()
  502. page.put(text_new.strip(), msg)
  503.  
  504. wikipedia.output(u'\03{lightpurple} Done=' + pagework+u"\03{default}")
  505. #except Exception as e:
  506. #wikipedia.output(u'\03{lightred}Could not open page\03{default}')
  507. #continue
  508. #------------------------------------------------------------encat part-------------------------------
  509. def categorydown(listacategory,one):
  510. listacategory=[listacategory]
  511. count=1
  512. for catname in listacategory:
  513. count+=1
  514. if count==200:
  515. break
  516. gencat = pagegenerators.SubCategoriesPageGenerator(catname, recurse=False)
  517. for subcat in gencat:
  518. try:
  519. wikipedia.output(str(subcat))
  520. except:
  521. wikipedia.output(subcat)
  522. if subcat in listacategory:
  523. continue
  524. else:
  525. listacategory.append(subcat)
  526. if one==True:
  527. break
  528. return listacategory
  529.  
  530. def encatlist(encat,one):
  531. wikipedia.config.put_throttle = 0
  532. wikipedia.put_throttle.setDelay()
  533. count=0
  534. listenpageTitle=[]
  535. encat=encat.replace(u'[[',u'').replace(u']]',u'').replace(u'Category:',u'').replace(u'category:',u'').strip()
  536. language='en'
  537. encat = catlib.Category( wikipedia.getSite(language),encat )
  538. listacategory=categorydown(encat,one)
  539. for enpageTitle in listacategory:
  540. try:
  541. fapages=pagefafinder(enpageTitle)
  542. if fapages!=False:
  543. for pages in fapages:
  544. pages=unicode(pages[0],'UTF-8')
  545. wikipedia.output(u'\03{lightgreen}Adding '+pages+u' to fapage lists\03{default}')
  546. listenpageTitle.append(pages)
  547. except:
  548.  
  549. try:
  550. enpageTitle=unicode(str(enpageTitle),'UTF-8').split(u'|')[0].split(u']]')[0].replace(u'[[',u'').strip()
  551. except:
  552. enpageTitle=enpageTitle.split(u'|')[0].split(u']]')[0].replace(u'[[',u'').strip()
  553. cat = catlib.Category( wikipedia.getSite(language),enpageTitle )
  554. gent = pagegenerators.CategorizedPageGenerator( cat )
  555. for pagework in gent:
  556. count+=1
  557. try:
  558. link=str(pagework).split(u'|')[0].split(u']]')[0].replace(u'[[',u'').strip()
  559. except:
  560. pagework=unicode(str(pagework),'UTF-8')
  561. link=pagework.split(u'|')[0].split(u']]')[0].replace(u'[[',u'').strip()
  562. wikipedia.output(link)
  563. fapagetitle=englishdictionry(link,'en','ur')
  564. if fapagetitle==False:
  565. continue
  566. else:
  567. wikipedia.output(u'\03{lightgreen}Adding '+fapagetitle+u' to fapage lists\03{default}')
  568. listenpageTitle.append(fapagetitle)
  569.  
  570. if listenpageTitle==[]:
  571. return False,False
  572. return listenpageTitle,listacategory
  573. #-------------------------------------------------------------------encat part finish--------------------------
  574. def main():
  575. wikipedia.config.put_throttle = 0
  576. wikipedia.put_throttle.setDelay()
  577. summary_commandline,gen,template = None,None,None
  578. namespaces,PageTitles,exceptions = [],[],[]
  579. encat,newcatfile='',''
  580. autoText,autoTitle,down = False,False,False
  581. recentcat,newcat=False,False
  582. genFactory = pagegenerators.GeneratorFactory()
  583. for arg in wikipedia.handleArgs():
  584. if arg == '-autotitle':
  585. autoTitle = True
  586. elif arg == '-autotext':
  587. autoText = True
  588. elif arg.startswith( '-page' ):
  589. if len( arg ) == 5:
  590. PageTitles.append( wikipedia.input( u'Which page do you want to chage?' ) )
  591. else:
  592. PageTitles.append( arg[6:] )
  593. break
  594. elif arg.startswith( '-except:' ):
  595. exceptions.append( arg[8:] )
  596. elif arg.startswith( '-template:' ):
  597. template = arg[10:]
  598. elif arg.startswith( '-encat:' ):
  599. encat = arg[7:].replace(u'Category:',u'').replace(u'category:',u'').replace(u'زمرہ:',u'')
  600. break
  601. elif arg.startswith( '-newcatfile:' ):
  602. newcatfile = arg[12:]
  603. break
  604. elif arg == '-down':
  605. down = True
  606. break
  607. elif arg.startswith('-recentcat'):
  608. arg=arg.replace(':','')
  609. if len(arg) == 10:
  610. genfa = pagegenerators.RecentchangesPageGenerator()
  611. else:
  612. genfa = pagegenerators.RecentchangesPageGenerator(number = int(arg[10:]))
  613. genfa = pagegenerators.DuplicateFilterPageGenerator(genfa)
  614. genfa = pagegenerators.NamespaceFilterPageGenerator( genfa,[14] )
  615. preloadingGen = pagegenerators.PreloadingGenerator( genfa,60)
  616. recentcat=True
  617. break
  618. elif arg.startswith('-newcat'):
  619. arg=arg.replace(':','')
  620. if len(arg) == 7:
  621. genfa = pagegenerators.NewpagesPageGenerator(100, False, None,14)
  622. else:
  623. genfa = pagegenerators.NewpagesPageGenerator(int(arg[7:]), False, None,14)
  624. preloadingGen = pagegenerators.PreloadingGenerator( genfa,60)
  625. newcat=True
  626. break
  627. elif arg.startswith( '-namespace:' ):
  628. namespaces.append( int( arg[11:] ) )
  629. elif arg.startswith( '-summary:' ):
  630. wikipedia.setAction( arg[9:] )
  631. summary_commandline = True
  632. else:
  633. generator = genFactory.handleArg( arg )
  634. if generator:
  635. gen = generator
  636. if encat!='':
  637. if down:
  638. one=False
  639. else:
  640. one=True
  641. encatfalist,encatlists=encatlist(encat,one)
  642. if encatlists:
  643. for encat in encatlists:
  644. encat=englishdictionry(encat,u'en',u'ur')
  645. if encat:
  646. run([encat])
  647. if encatfalist!=False:
  648. run(encatfalist)
  649. if PageTitles:
  650. pages = [wikipedia.Page( wikipedia.getSite(),PageTitle ) for PageTitle in PageTitles]
  651. gen = iter( pages )
  652. if recentcat:
  653. for workpage in preloadingGen:
  654. workpage=workpage.title()
  655. cat = catlib.Category( wikipedia.getSite('ur'),workpage)
  656. gent = pagegenerators.CategorizedPageGenerator( cat )
  657. run( gent)
  658. wikipedia.stopme()
  659. sys.exit()
  660. if newcat:
  661. for workpage in preloadingGen:
  662. workpage=workpage.title()
  663. workpage=englishdictionry(workpage,'ur','en')
  664. if workpage!=False:
  665. one=True
  666. encatfalist,encatlists=encatlist(workpage,one)
  667. if encatlists:
  668. for encat in encatlists:
  669. encat=englishdictionry(encat,u'en',u'ur')
  670. if encat:
  671. run([encat])
  672. if encatfalist!=False:
  673. run(encatfalist)
  674. wikipedia.stopme()
  675. sys.exit()
  676. if newcatfile:
  677. text2 = codecs.open( newcatfile,'r' ,'utf8' )
  678. text = text2.read()
  679. linken = re.findall(ur'\[\[.*?\]\]',text, re.S)
  680. if linken:
  681. for workpage in linken:
  682. workpage=workpage.split(u'|')[0].replace(u'[[',u'').replace(u']]',u'').replace(u'Category:',u'').replace(u'category:',u'').replace(u'زمرہ:',u'').strip()
  683. workpage=englishdictionry(workpage,'ur','en')
  684. if workpage!=False:
  685. one=True
  686. encatfalist,encatlists=encatlist(workpage,one)
  687. if encatlists:
  688. run(encatlists)
  689. if encatfalist!=False:
  690. run(encatfalist)
  691. wikipedia.stopme()
  692. sys.exit()
  693. if not gen:
  694. wikipedia.stopme()
  695. sys.exit()
  696. if namespaces != []:
  697. gen = pagegenerators.NamespaceFilterPageGenerator( gen,namespaces )
  698. preloadingGen = pagegenerators.PreloadingGenerator( gen,pageNumber = 60 )
  699. run( preloadingGen)
  700.  
  701.  
  702. if __name__ == '__main__':
  703. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement