From 0a242d70304cc9594b050ab3e1a58406a8648e28 Mon Sep 17 00:00:00 2001 From: lokas Date: Mon, 10 Apr 2023 18:35:00 +0200 Subject: [PATCH 1/4] init --- tasks/archive/__init__.py | 0 tasks/archive/users/__init__.py | 0 tasks/archive/users/run.py | 0 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 tasks/archive/__init__.py create mode 100644 tasks/archive/users/__init__.py create mode 100644 tasks/archive/users/run.py diff --git a/tasks/archive/__init__.py b/tasks/archive/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tasks/archive/users/__init__.py b/tasks/archive/users/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tasks/archive/users/run.py b/tasks/archive/users/run.py new file mode 100644 index 00000000..e69de29b From 7ce5f3b2881e91bb4c7d82fc263771834acf8e10 Mon Sep 17 00:00:00 2001 From: lokas Date: Mon, 10 Apr 2023 18:56:37 +0200 Subject: [PATCH 2/4] add base read --- tasks/archive/users/run.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tasks/archive/users/run.py b/tasks/archive/users/run.py index e69de29b..5d1b36b7 100644 --- a/tasks/archive/users/run.py +++ b/tasks/archive/users/run.py @@ -0,0 +1,13 @@ +import pywikibot + +site = pywikibot.Site() + +template_name = "قالب:أرشفة آلية" + +template = pywikibot.Page(site, template_name) + +gen = template.embeddedin(filter_redirects=False, namespaces=3) + +for user_take in gen: + if user_take.depth == 0: + print(user_take) From 26ecec898d218e5e83e497a4f257dd2156b5f1cf Mon Sep 17 00:00:00 2001 From: lokas Date: Mon, 10 Apr 2023 19:00:36 +0200 Subject: [PATCH 3/4] check if can edit page --- tasks/archive/users/run.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tasks/archive/users/run.py b/tasks/archive/users/run.py index 5d1b36b7..c493d2aa 100644 --- a/tasks/archive/users/run.py +++ b/tasks/archive/users/run.py @@ -8,6 +8,6 @@ gen = template.embeddedin(filter_redirects=False, namespaces=3) -for user_take in gen: - if user_take.depth == 0: - print(user_take) +for user_talk in gen: + if user_talk.depth == 0 and user_talk.has_permission(action='edit'): + print(user_talk) From c757fc5ed2f247b6992d2f686f056bac13134ecb Mon Sep 17 00:00:00 2001 From: lokas Date: Mon, 10 Apr 2023 19:31:41 +0200 Subject: [PATCH 4/4] add read_archive_template() --- tasks/archive/users/module.py | 39 +++++++++++++++++++++++++++++++++++ tasks/archive/users/run.py | 13 +++++++----- 2 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 tasks/archive/users/module.py diff --git a/tasks/archive/users/module.py b/tasks/archive/users/module.py new file mode 100644 index 00000000..b3797666 --- /dev/null +++ b/tasks/archive/users/module.py @@ -0,0 +1,39 @@ +import wikitextparser as wtp + +from core.utils.helpers import prepare_str + +template_name = "أرشفة آلية" + + +def read_archive_template(page_text, archive_template_name): + # Parse the page text using the wikitextparser library + parsed = wtp.parse(page_text) + + # Set default values for template type and value + template_type = 'section' + template_value = 10 + + # Loop over all templates in the parsed page text + for template in parsed.templates: + # Check if the current template is the archive template we are looking for + if prepare_str(template.name) == prepare_str(archive_template_name): + try: + # Attempt to parse the template type string from the first argument of the template + template_type_str = prepare_str(template.arguments[0].value) + if template_type_str == prepare_str('حجم'): + template_type = 'size' + elif template_type_str == prepare_str('قسم'): + template_type = 'section' + except: + # If an error occurs while parsing the template type, skip this block and use the default value + pass + try: + # Attempt to parse the template value integer from the second argument of the template + template_value_str = template.arguments[1].value + template_value = int(template_value_str) + except: + # If an error occurs while parsing the template value, skip this block and use the default value + pass + + # Return the template type and value + return template_type, template_value diff --git a/tasks/archive/users/run.py b/tasks/archive/users/run.py index c493d2aa..44733f3f 100644 --- a/tasks/archive/users/run.py +++ b/tasks/archive/users/run.py @@ -1,13 +1,16 @@ import pywikibot -site = pywikibot.Site() +from tasks.archive.users.module import template_name, read_archive_template -template_name = "قالب:أرشفة آلية" +site = pywikibot.Site() -template = pywikibot.Page(site, template_name) +template = pywikibot.Page(site, f"قالب:{template_name}") -gen = template.embeddedin(filter_redirects=False, namespaces=3) +gen = template.embeddedin(filter_redirects=False, namespaces=3, content=True) for user_talk in gen: - if user_talk.depth == 0 and user_talk.has_permission(action='edit'): + if user_talk.depth == 0: print(user_talk) + template_type, template_value = read_archive_template(page_text=user_talk.text, + archive_template_name=template_name) + print(template_type, template_value)