ব্যবহারকারী:MdsShakil/উৎসকোড/KanikBot

উইকিপিডিয়া, মুক্ত বিশ্বকোষ থেকে
Source
https://tools-static.wmflabs.org/kanikbot-bn/bot-task-5.html (archive)

Copyright © 2023 Kanik, Ahmad

Permission is hereby granted, free of charge, to any person obtaining a copy of this software, to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

This Software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this Software.

import pywikibot
from datetime import date
import re
import mwparserfromhell
from time import sleep

site = pywikibot.Site('wikipedia:bn')
acat = pywikibot.Category(site, 'স্বয়ংক্রিয়ভাবে সংগ্রহশালায় স্থানান্তরিত হওয়া আলোচনা পাতা')

months = ['জানুয়ারি', 'ফেব্রুয়ারি', 'মার্চ',
'এপ্রিল', 'মে', 'জুন', 'জুলাই', 'আগস্ট',
'সেপ্টেম্বর', 'অক্টোবর', 'নভেম্বর', 'ডিসেম্বর']
ts_ptn = re.compile(r'([০-৯]{1,2}) (\S+) ([০-৯]{4}) \(ইউটিসি\)')
sc_ptn = re.compile(r'(?s:.+?)(?=\Z|\n==[^=\n].*==)')
sgi = site.siteinfo.get('general')
ARSZ_X = round(sgi['maxarticlesize']*0.7)


def en2bn(num):
	if not isinstance(num, int) or num < 1:
		raise ValueError ('Invalid Parameter',)
	bn_d, cnum = '০১২৩৪৫৬৭৮৯', list()
	while num > 0:
		cnum.append(bn_d[num%10])
		num //= 10
	return ''.join(reversed(cnum))


def get_spage(tpage, pref, add=0):
	idx = en2bn(pref['cidx'] + add)
	spbp = pref['ptrn'].replace('$', idx)
	spfp = tpage.title() + spbp
	try:
		pywikibot.Page(site, spfp).title()
	except pywikibot.exceptions.InvalidTitleError:
		return None
	return pywikibot.Page(site, spfp)


def blen(text):
	if not isinstance(text, str):
		raise ValueError ('Invalid Parameter',)
	return len(bytes(text, encoding='utf-8'))


def getlastdate(talks):
	ts = ts_ptn.findall(talks)
	while ts:
		try:
			d = int(ts[-1][0])
			m = months.index(ts[-1][1])+1
			y = int(ts[-1][2])
			return date(y, m, d)
		except ValueError:
			print(ts.pop())
	return None


def sep_secs(tpage, spage, pref):
	oslen, sof = blen(spage.text), False
	sctns = sc_ptn.findall(tpage.text)
	new_s, old_s = sctns[0].rstrip(), ''
	for sec in sctns[1:]:
		sec_s = '\n\n' + sec.strip()
		if sof:
			new_s += sec_s
			continue
		lastdate = getlastdate(sec_s)
		if lastdate is None:
			new_s += sec_s
			continue
		age = date.today() - lastdate
		is_mvb = False
		if age.days > pref['xday']:
			if oslen < 500:
				is_mvb = True
			elif oslen + blen(sec_s) <= pref['arsz']:
				is_mvb = True
			else:
				sof = True
		if is_mvb:
			old_s += sec_s
			oslen += blen(sec_s)
		else:
			new_s += sec_s
	return new_s, old_s, sof


def get_tparams(page):
	for tmpl in page.templatesWithParams():
		if tmpl[0].pageid == 928686:
			prm_l = tmpl[1]
			break
	else:
		return None
	prm_d = {
		'cidx' : 0,
		'xday' : 7,
		'arsz' : 200000,
		'ptrn' : '/সংগ্রহশালা $'
	}
	igl = ('বার্তা-প্রদর্শন', 'archive-link')
	for prm in prm_l:
		if prm.count('=') != 1:
			return None
		name, value = prm.split('=')
		try:
			if name == 'archive-size':
				prm_d['arsz'] = int(value)
			elif name == 'current-index':
				prm_d['cidx'] = int(value)
			elif name == 'max-day':
				prm_d['xday'] = int(value)
			elif name == 'archive-pattern':
				prm_d['ptrn'] = value
			elif name not in igl:
				return None
		except ValueError:
			return None
	if prm_d['cidx'] < 1:
		return None
	if prm_d['xday'] < 1:
		return None
	if prm_d['arsz'] > ARSZ_X:
		return None
	if '$' not in prm_d['ptrn']:
		return None
	if not prm_d['ptrn'].startswith('/'):
		return None
	return prm_d


def chparam(text, pref):
	pp = mwparserfromhell.parse(text,  skip_style_tags=True)
	for tmpl in pp.filter_templates():
		if tmpl.name.strip() == 'স্বয়ংক্রিয় সংগ্রহশালা':
			break
	else:
		return None
	prm = tmpl.get('current-index')
	if str(prm.value).endswith('\n'):
		nval = ' %d\n' % (pref['cidx'] + 1)
	else:
		nval = str(pref['cidx'] + 1)
	prm.value = nval
	return str(pp)


def smsg(page):
	print('%s skipped' % page.title())


def main(tpage):
	if 'archivedtalk' in tpage.properties():
		return smsg(tpage)
	pref = get_tparams(tpage)
	if pref is None:
		return smsg(tpage)
	spage2 = get_spage(tpage, pref, 1)
	while spage2 and spage2.exists():
		pref['cidx'] += 1
		spage2 = get_spage(tpage, pref, 1)
	spage = get_spage(tpage, pref)
	if spage is None:
		return smsg(tpage)
	new, old, sof = sep_secs(tpage, spage, pref)
	if sof:
		new = chparam(new, pref)
		if new is None:
			return smsg(tpage)
	if old:
		if not spage.exists():
			spage.text = '{{সংগ্রহশালার স্বয়ংক্রিয় পরিভ্রমণ}}'
		spage.text += old
		spage.save('বট কর্তৃক সংগ্রহশালায় স্থানান্তর')
	if sof or old:
		tpage.text = new
		tpage.save('বট কর্তৃক [[' + spage.title() + '|সংগ্রহশালায়]] স্থানান্তর')
		sleep(10)


site.login()
site.throttle.setDelays(0, 1)
for tpage in acat.members():
	ns = tpage.namespace().id
	if ns == 4 or (ns & 1) == 1:
		main(tpage)
	else:
		smsg(tpage)
import pywikibot
import re
import mwparserfromhell
from datetime import datetime, timedelta

site = pywikibot.Site('wikipedia:bn')

months = ['জানুয়ারি', 'ফেব্রুয়ারি', 'মার্চ',
'এপ্রিল', 'মে', 'জুন', 'জুলাই', 'আগস্ট',
'সেপ্টেম্বর', 'অক্টোবর', 'নভেম্বর', 'ডিসেম্বর']

ctld = pywikibot.textlib.to_local_digits

ts_ptn = re.compile(r'([১-৩]?[০-৯]) (\S+) (২[০-৯]{3}) \(ইউটিসি\)')
sc_ptn = re.compile(r'(?s:.+?)(?=\Z|\n==[^=\n].*==)')
rm_ptn = re.compile(r'<!--4fd29918-->.*?<!--e4807be0-->')
tm_ptn = re.compile(r'<!--4fd29918-->\{\{সমাধান হওয়া অনুচ্ছেদ\|1=.+?\|t=(2[0-9]{13})\}\}<!--e4807be0-->')

def start_month(talks):
	wiki = mwparserfromhell.parse(talks)
	text = wiki.strip_code()
	for ts in ts_ptn.findall(text):
		if ts[1] not in months:
			continue
		month = months.index(ts[1])
		return int(ts[2]), month
	pt = datetime.utcnow() - timedelta(days=5)
	return pt.year, pt.month-1

class NoticeBoard:
	def __init__(self, title, mrngs, mitv):
		self.page = pywikibot.Page(site, title, ns=4)
		self.mrngs = mrngs
		self.mitv = mitv
	def get_spage(self, talks):
		year, month = start_month(talks)
		mr = self.mrngs[month//self.mitv]
		return ctld(year, 'bn') + mr
	def get_fpage(self, spt):
		title = self.page.title(with_ns=False)
		fpage = title + '/সংগ্রহশালা/' + spt
		return pywikibot.Page(site, fpage, ns=4)

def get_sran_t(text):
	mobj = tm_ptn.search(text)
	if mobj is None:
		return 0
	tval = datetime.strptime(mobj[1], '%Y%m%d%H%M%S')
	age = datetime.utcnow() - tval
	return round(age.total_seconds()/60)

def sep_secs(nb):
	sctns = sc_ptn.findall(nb.page.text)
	new_s = sctns[0].rstrip()
	old_s = dict()
	for sec in sctns[1:]:
		age = get_sran_t(sec)
		if age < 7200:
			new_s += '\n\n' + sec.strip()
		else:
			spt = nb.get_spage(sec)
			if spt not in old_s:
				old_s[spt] = ''
			ssec = rm_ptn.sub('', sec)
			old_s[spt] += '\n\n' + ssec.strip()
	return new_s, old_s

def main(nb):
	new_s, old_s = sep_secs(nb)
	for spt in old_s:
		apg = nb.get_fpage(spt)
		apg.text += old_s[spt]
		apg.save('বট কর্তৃক সংগ্রহশালায় স্থানান্তর')
	if old_s:
		nb.page.text = new_s
		nb.page.save('বট কর্তৃক সংগ্রহশালায় স্থানান্তর')

title = 'প্রশাসকদের আলোচনাসভা'
mrngs = ['/১-৬', '/৭-১২']
AN = NoticeBoard(title, mrngs, 6)

title = 'আলোচনাসভা'
mrngs = ['/১-২', '/৩-৪', '/৫-৬', '/৭-৮', '/৯-১০', '/১১-১২']
VP = NoticeBoard(title, mrngs, 2)

site.login()
main(AN)
main(VP)

Last updated 2023-06-27 17:12:11 +0600