Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/c2corg/CampBot
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeauchesne committed Feb 17, 2022
2 parents 74b44af + 709b238 commit 5fe5761
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 40 deletions.
17 changes: 13 additions & 4 deletions campbot/__main__.py
Expand Up @@ -3,7 +3,7 @@
Usage:
campbot clean_rc <days> [--login=<login>] [--password=<password>] [--delay=<seconds>] [--batch]
campbot report_rc <days> [--login=<login>] [--password=<password>] [--delay=<seconds>] [--batch]
campbot report_rc <days> <lang> <thread_url> [--login=<login>] [--password=<password>] [--delay=<seconds>]
campbot clean <url_or_file> <langs> [--login=<login>] [--password=<password>] [--delay=<seconds>] [--batch] [--bbcode]
campbot report <url_or_file> <lang> [--login=<login>] [--password=<password>] [--delay=<seconds>]
campbot contribs [--out=<filename>] [--starts=<start_date>] [--ends=<end_date>] [--delay=<seconds>]
Expand Down Expand Up @@ -42,7 +42,9 @@
import logging
import os

logging.basicConfig(level=logging.INFO, format="%(asctime)-15s %(message)s")
logging.basicConfig(
level=logging.INFO, format="%(asctime)-15s %(levelname)s %(message)s"
)


def get_campbot(args):
Expand All @@ -58,6 +60,12 @@ def get_campbot(args):
"@", 1
)

if "CAMPBOT_LOGIN" in os.environ and not args["--login"]:
args["--login"] = os.environ["CAMPBOT_LOGIN"]

if "CAMPBOT_PASSWORD" in os.environ and not args["--password"]:
args["--password"] = os.environ["CAMPBOT_PASSWORD"]

bot = CampBot(proxies=proxies, min_delay=args["--delay"])

if args["--login"] and args["--password"]:
Expand All @@ -76,8 +84,9 @@ def main(args):

report_recent_changes(
get_campbot(args),
days=int(args["<days>"]),
ask_before_saving=not args["--batch"],
days=float(args["<days>"]),
lang=args["<lang>"],
thread_url=args["<thread_url>"],
)

elif args["clean_rc"]:
Expand Down
57 changes: 29 additions & 28 deletions campbot/checkers.py
Expand Up @@ -21,6 +21,7 @@

from dateutil import parser
import datetime
import logging
from campbot import utils


Expand Down Expand Up @@ -193,51 +194,51 @@ def get_report(self, bot, lang):
return "\n".join(result)


def report_recent_changes(bot, days, ask_before_saving):
check_message_url = (
"https://forum.camptocamp.org/t/topoguide-verifications-automatiques/201480"
)
lang = "fr"
def report_recent_changes(bot, days, lang, thread_url):

newest_date = utils.today().replace(hour=0, minute=0, second=0, microsecond=0)
oldest_date = newest_date - datetime.timedelta(days=days)

tests = get_fixed_tests(lang)
tests += get_re_tests(bot.forum.get_post(url=check_message_url), lang)
tests += get_re_tests(bot.forum.get_post(url=thread_url), lang)

logging.info(f"Get modified documents from {oldest_date} to {newest_date}")
items = bot.get_modified_documents(
lang=lang, oldest_date=oldest_date, newest_date=newest_date
).values()

reports = []

for i, contributions in enumerate(items):
print("Build report {}/{}".format(i, len(items)))
reports.append(DocumentReport(bot, contributions, tests))
logging.info(f"Found {len(items)} contributions, processing...")

messages = [
"[Explications]({})\n".format(check_message_url),
"[details=Signification des icônes]\n<table>",
"<tr><th>Test</th><th>A relire</th><th>Corrigé</th></tr>",
]
for i, contributions in enumerate(items):
logging.info("Build report {}/{}".format(i, len(items)))
report = DocumentReport(bot, contributions, tests)
if report.need_report:
reports.append(report)

if len(reports) != 0:
messages = [
"[Explications]({})\n".format(thread_url),
"[details=Signification des icônes]\n<table>",
"<tr><th>Test</th><th>A relire</th><th>Corrigé</th></tr>",
]

for test in tests:
messages.append("<tr>")
messages.append("<th>{}</th>".format(test.name))
messages.append("<td>{}</td>".format(test.fail_marker))
messages.append("<td>{}</td>".format(test.success_marker))
messages.append("</tr>")
for test in tests:
messages.append("<tr>")
messages.append("<th>{}</th>".format(test.name))
messages.append("<td>{}</td>".format(test.fail_marker))
messages.append("<td>{}</td>".format(test.success_marker))
messages.append("</tr>")

messages.append("</table>\n[/details]\n\n----\n\n")
messages += [
report.get_report(bot, lang) for report in reports if report.need_report
]
messages.append("</table>\n[/details]\n\n----\n\n")
messages += [report.get_report(bot, lang) for report in reports]

for m in messages:
print(m)
logging.info(f"Reporting {len(reports)} reports:\n\n" + "\n".join(messages))

if len(messages) != 0:
bot.forum.post_message("\n".join(messages), check_message_url)
bot.forum.post_message("\n".join(messages), thread_url)
else:
logging.info("Nothing to report")


def emoji(src, text):
Expand Down
5 changes: 5 additions & 0 deletions campbot/core.py
Expand Up @@ -467,15 +467,20 @@ def login(self, login, password):
"""

logging.info(f"Logging to wiki with account {login}...")
res = self.wiki.post(
"/users/login", {"username": login, "password": password, "discourse": True}
)
token = res["token"]
self.moderator = "moderator" in res["roles"]
self.wiki.headers["Authorization"] = 'JWT token="{}"'.format(token)

logging.info(f"Logging to forum with account {login}...")
self.forum.get(res["redirect_internal"].replace(self.forum.api_url, ""))
self.forum.headers["X-CSRF-Token"] = self.forum.get("/session/csrf")["csrf"]

logging.info(f"Logging with account {login} OK")

def get_documents(self, url_or_filename):
"""
Get a generator of document, given a filename or a URL.
Expand Down
8 changes: 5 additions & 3 deletions docs/CLI/report_rc.rst
Expand Up @@ -16,14 +16,16 @@ Command line

.. code-block:: bash
campbot report_rc <days> [--login=<login>] [--password=<password>] [--delay=<seconds>]
campbot report_rc <days> <lang> <thread_url> [--login=<login>] [--password=<password>] [--delay=<seconds>]
Arguments and options
---------------------

* ``<days>`` : Number of day to check Let says that we are on 17 june 2018, 12h. if ``<days>`` is ``2``, the process will run on all contributions made from ``2018-06-15 00:00:00`` to ``2018-06-16 23:59:59``
* ``<login>`` : your bot login
* ``<password>`` : your bot password
* ``<lang>`` : Only contributions made on this lang will be checked
* ``<days>`` : Thread's URL where the report will be published
* ``<login>`` : bot's login
* ``<password>`` : bot's password
* ``<delay>`` : delay, in seconds between each request. By defaut, 3 seconds

.. warning::
Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
@@ -1,4 +1,4 @@
docopt==0.6.2
requests==2.25.1
python-dateutil==2.8.1
pytz==2020.4
requests==2.27.1
python-dateutil==2.8.2
pytz==2021.3
9 changes: 7 additions & 2 deletions tests/test_miscs.py
Expand Up @@ -227,6 +227,7 @@ def get_main_args(action, others=None):
"<days>": "1",
"<lang>": "fr",
"<langs>": "fr,de",
"<thread_url>": "https://forum.camptocamp.org/t/topoguide-verifications-automatiques/201480",
"<url>": "outings#u=286726",
"<url_or_file>": "outings#u=286726",
"--ends": "2999-12-31",
Expand All @@ -242,7 +243,8 @@ def get_main_args(action, others=None):
result[action] = True

os.environ["HTTPS_PROXY"] = ""
os.environ["CAMPBOT_CREDENTIALS"] = "patate@douce"
os.environ["CAMPBOT_LOGIN"] = "patate"
os.environ["CAMPBOT_PASSWORD"] = "douce"

return result

Expand Down Expand Up @@ -311,8 +313,11 @@ def test_weird(fix_requests, fix_input):
obj = bot.wiki.get_wiki_object(123, "o")
assert obj.is_personal() == True

os.environ["CAMPBOT_CREDENTIALS"] = "x@y"
os.environ["CAMPBOT_LOGIN"] = "x"
os.environ["CAMPBOT_PASSWORD"] = "y"
args = get_main_args("clean")
assert args["--login"] == "x"
assert args["--password"] == "y"
args["--login"] = False
__main__.main(args)

Expand Down

0 comments on commit 5fe5761

Please sign in to comment.