user@linuxtrack:~ $ python -c 'print("Soyez les bienvenus !")'

Vous n'êtes pas identifié(e).

#1 08-03-2015 04:08:36

azgarech
Crazy one

[Aide modification Module] Pour le irc bot willie

Bonjour à tous,

Je me casse les dents pour modifier ce script https://github.com/embolalia/willie/blo … /github.py

En gros sur le chan #viperr je cherche à avoir un bot qui peut recenser les incidents des visiteurs sur un github.

Pour le moment l'user peut faire ".makeissue Titre".

Tout ce qui est entré après .makeissue devient le titre et c'est pas glop.

J'aimerai donc pouvoir modifier ce comportement en ceci ".makeissue Titre | body"

Le pipe  étant là pour déterminer où commence la description.

Je suis nul en python même si je m'y essai et je serai super content d'un petit coup de pouce.

PS: petite dédicasse à WarLocG et bot _pl0p  pour m'avoir motivé à mettre un bot sur le chan Viperr.


Edit:


La parie du script à modifier est :

#########################

@commands('makeissue', 'makebug')
def issue(bot, trigger):
    """Create a GitHub issue, also known as a bug report. Syntax: .makeissue Title of the bug report"""
    # check input
    if not trigger.group(2):
        return bot.say('Please title the issue')

    # Is the Oauth token and repo available?
    gitAPI = checkConfig(bot)
    if not gitAPI:
        return bot.say('Git module not configured, make sure github.oauth_token and github.repo are defined')

    # parse input
    now = ' '.join(str(datetime.utcnow()).split(' ')).split('.')[0] + ' UTC'
    body = 'Submitted by: %s\nFrom channel: %s\nAt %s' % (trigger.nick, trigger.sender, now)
    data = {"title": trigger.group(2), "body": body}
    # submit
    try:
        raw = web.post('https://api.github.com/repos/' + gitAPI[1] + '/issues?access_token=' + gitAPI[0], json.dumps(data))
    except HTTPError:
        bot.say('The GitHub API returned an error.')
        return NOLIMIT

    data = json.loads(raw)
    bot.say('Issue #%s posted. %s' % (data['number'], data['html_url']))
    LOGGER.warning('Issue #%s created in %s', data['number'], trigger.sender)

#########################

trigger.group(2) correspond à tout ce qui est entré après la commande .makeissue

C'est cette valeur qu'il faudra passer à la moulinette.

Dernière modification par azgarech (08-03-2015 05:10:54)


Security is always excessive until it’s not enough. — Robbie Sinclair

Hors ligne

#2 08-03-2015 15:05:52

azgarech
Crazy one

Re : [Aide modification Module] Pour le irc bot willie

J'ai réussi à m'en sortir:

###########################

@commands('makeissue', 'makebug')
def issue(bot, trigger):
    """Create a GitHub issue, also known as a bug report. Syntax: .makeissue Title of the bug report"""
    # check input
    if not trigger.group(2):
        return bot.say('usage : .makeissue Title . Description')

    # Is the Oauth token and repo available?
    gitAPI = checkConfig(bot)
    if not gitAPI:
        return bot.say('Git module not configured, make sure github.oauth_token and github.repo are defined')

    # parse input
    now = ' '.join(str(datetime.utcnow()).split(' ')).split('.')[0] + ' UTC'
    body = trigger.group(2).split('.')[1] +"\n"+"\n"+' Submitted by: %s\nFrom channel: %s\nAt %s' % (trigger.nick, trigger.sender, now)
    data = {"title": trigger.group(2).split('.')[0], "body": body }
    # submit
    try:
        raw = web.post('https://api.github.com/repos/' + gitAPI[1] + '/issues?access_token=' + gitAPI[0], json.dumps(data))
    except HTTPError:
        bot.say('The GitHub API returned an error.')
        return NOLIMIT

    data = json.loads(raw)
    bot.say('Issue #%s posted. %s' % (data['number'], data['html_url']))
    LOGGER.warning('Issue #%s created in %s', data['number'], trigger.sender)

#####################

Me reste à gérer le check sur la présence ou non de body


Security is always excessive until it’s not enough. — Robbie Sinclair

Hors ligne

#3 08-03-2015 20:55:10

penthium2
Modérateur

Re : [Aide modification Module] Pour le irc bot willie

rooo la honte ! dans sa précipitation il en oublie les balises code big_smile

bien jouer azgarech


vi est mon ami pour la vie
Viperr
Ph'nglui nglw-nafh Cthulhu R'lyeh wgah-nagl fhtagn

Hors ligne

#4 09-03-2015 01:18:55

WarLocG
#! modo de compet

Re : [Aide modification Module] Pour le irc bot willie

Félicitation pour avoir trouvé smile

Et pour finir tu as mis le message du body après un . et non plus entre () comme tu avais convenu au départ.

J'aimerai donc pouvoir modifier ce comportement en ceci ".makeissue Titre | body"

bah meme ainsi tant qu'a faire : ".makeissue Titre => body" wink

Je te propose quand même cette amélioration pour ton body

body = '%s\n\nSubmitted by: %s\nFrom channel: %s\nAt %s' % (trigger.group(2).split('.')[1], trigger.nick, trigger.sender, now)

Me reste à gérer le check sur la présence ou non de body

re.search ou re.match => https://docs.python.org/2/library/re.html , et du coup avec cela tu pourras obtenir un nouveau group() wink

Et bot-pl0p te remercie aussi pour lui avoir créé un copain big_smile


Avant de poser vos questions, jeter un oeil ici
Mon CodeVault et Wiki : ici
Les messages privés envers le staff sont uniquement pour les cas d'urgence ou affaires privées (personnelles). Les demandes se feront exclusivement sur le forum. Merci de respecter cette clause sous peine de sanctions.

Hors ligne

#5 09-03-2015 02:17:36

azgarech
Crazy one

Re : [Aide modification Module] Pour le irc bot willie

Je comprend totalement ta modification, c'est cool j'en apprend de plus en plus.
Je suis arrivé au point ou si il n'y a pas de body après le "." alors je n'envois pas l'issue.
Malheuresement mon message disant qu'il manque le body ne passe pas XD .

Je crois que je me suis trouvé la carotte pour me faire apprendre python. Demain soir si j'ai le temps je commence les cours openclassroom .

Ligne 74 à 78

[== Indéfini ==]
# coding=utf8
"""
github.py - Willie Github Module
Copyright 2012, Dimitri Molenaars http://tyrope.nl/
Licensed under the Eiffel Forum License 2.

http://willie.dftba.net/
"""
from __future__ import unicode_literals

from datetime import datetime
import sys
if sys.version_info.major < 3:
    from urllib2 import HTTPError
else:
    from urllib.error import HTTPError
import json
from willie import web, tools
from willie.module import commands, rule, NOLIMIT
import os
import re
from willie.logger import get_logger

LOGGER = get_logger(__name__)

issueURL = (r'https?://(?:www\.)?github.com/'
            '([A-z0-9\-]+/[A-z0-9\-]+)/'
            '(?:issues|pull)/'
            '([\d]+)')
regex = re.compile(issueURL)


def checkConfig(bot):
    if not bot.config.has_option('github', 'oauth_token') or not bot.config.has_option('github', 'repo'):
        return False
    else:
        return [bot.config.github.oauth_token, bot.config.github.repo]


def configure(config):
    """
    | [github] | example | purpose |
    | -------- | ------- | ------- |
    | oauth_token | 5868e7af57496cc3ae255868e7af57496cc3ae25 | The OAuth token to connect to your github repo |
    | repo | embolalia/willie | The GitHub repo you're working from. |
    """
    chunk = ''
    if config.option('Configuring github issue reporting and searching module', False):
        config.interactive_add('github', 'oauth_token', 'Github API Oauth2 token', '')
        config.interactive_add('github', 'repo', 'Github repository', 'embolalia/willie')
    return chunk


def setup(bot):
    if not bot.memory.contains('url_callbacks'):
        bot.memory['url_callbacks'] = tools.WillieMemory()
    bot.memory['url_callbacks'][regex] = issue_info


def shutdown(bot):
    del bot.memory['url_callbacks'][regex]


@commands('makeissue', 'makebug')
def issue(bot, trigger):
    """Create a GitHub issue, also known as a bug report. Syntax: .makeissue Title of the bug report"""
    # check input
    if not trigger.group(2):
        return bot.say('usage : .makeissue Title . Description')

    # Is the Oauth token and repo available?
    gitAPI = checkConfig(bot)
    # test if body exist
    bodyexist = '%s' % (trigger.group(2).split('.')[1])
    if not gitAPI:
        return bot.say('Git module not configured, make sure github.oauth_token and github.repo are defined')
	
    if  not bodyexist:
        return bot.say('You Forgot to had a description . Usage : .makeissue Title . Description')
        
    # parse input
    now = ' '.join(str(datetime.utcnow()).split(' ')).split('.')[0] + ' UTC'
    body = '%s\n\nSubmitted by: %s\nFrom channel: %s\nAt %s' % (trigger.group(2).split('.')[1], trigger.nick, trigger.sender, now)
    data = {"title": trigger.group(2).split('.')[0], "body": body }
    # submit
    try:
        raw = web.post('https://api.github.com/repos/' + gitAPI[1] + '/issues?access_token=' + gitAPI[0], json.dumps(data))
    except HTTPError:
        bot.say('The GitHub API returned an error.')
        return NOLIMIT

    data = json.loads(raw)
    bot.say('Issue #%s posted. %s' % (data['number'], data['html_url']))
    LOGGER.warning('Issue #%s created in %s', data['number'], trigger.sender)


@commands('addtrace', 'addtraceback')
def add_traceback(bot, trigger):
    """Add a traceback to a GitHub issue.

    This pulls the traceback from the exceptions log file. To use, put .addtrace
    followed by the issue number to add the comment to, then the signature of
    the error (the message shown to the channel when the error occured). This
    command will only work for errors from unhandled exceptions."""
    # Make sure the API is set up
    gitAPI = checkConfig(bot)
    if not gitAPI:
        return bot.say('GitHub module not configured, make sure github.oauth_token and github.repo are defined')

    if not trigger.group(2):
        bot.say('Please give both the issue number and the error message.')
        return

    # Make sure the input is valid
    args = trigger.group(2).split(None, 1)
    if len(args) != 2:
        bot.say('Please give both the issue number and the error message.')
        return
    number, trace = args

    # Make sure the given issue number exists
    issue_data = web.get('https://api.github.com/repos/%s/issues/%s' % (gitAPI[1], number))
    issue_data = json.loads(issue_data)
    if 'message' in issue_data and issue_data['message'] == 'Not Found':
        return bot.say("That issue doesn't exist.")

    # Find the relevant lines from the log file
    post = ''
    logfile = os.path.join(bot.config.logdir, 'exceptions.log')
    with open(logfile) as log:
        in_trace = False
        for data in log:
            if data == 'Signature: ' + trace + '\n':
                post = data
                in_trace = True
            elif data == '----------------------------------------\n':
                in_trace = False
            elif in_trace:
                post += data

    # Give an error if we didn't find the traceback
    if not post:
        return bot.say("I don't remember getting that error. Please post it "
                       "yourself at https://github.com/%s/issues/%s"
                       % (gitAPI[1], number))

    # Make the comment
    try:
        raw = web.post('https://api.github.com/repos/' + gitAPI[1] + '/issues/'
                       + number + '/comments?access_token=' + gitAPI[0],
                       json.dumps({'body': '``\n' + post + '``'}))
    except OSError:  # HTTPError:
        bot.say('The GitHub API returned an error.')
        return NOLIMIT

    data = json.loads(raw)
    bot.say('Added traceback to issue #%s. %s' % (number, data['html_url']))
    LOGGER.warning('Traceback added to #%s in %s.', number, trigger.sender)


@commands('findissue', 'findbug')
def findIssue(bot, trigger):
    """Search for a GitHub issue by keyword or ID. usage: .findissue search keywords/ID (optional) You can specify the first keyword as "CLOSED" to search closed issues."""
    if not trigger.group(2):
        return bot.reply('What are you searching for?')

    # Is the Oauth token and repo available?
    gitAPI = checkConfig(bot)
    if not gitAPI:
        return bot.say('Git module not configured, make sure github.oauth_token and github.repo are defined')
    firstParam = trigger.group(2).split(' ')[0]
    if firstParam.isdigit():
        URL = 'https://api.github.com/repos/%s/issues/%s' % (gitAPI[1], firstParam)
    elif firstParam == 'CLOSED':
        if '%20'.join(trigger.group(2).split(' ')[1:]) not in ('', '\x02', '\x03'):
            URL = 'https://api.github.com/legacy/issues/search/' + gitAPI[1] + '/closed/' + '%20'.join(trigger.group(2).split(' ')[1:])
        else:
            return bot.reply('What are you searching for?')
    else:
        URL = 'https://api.github.com/legacy/issues/search/%s/open/%s' % (gitAPI[1], web.quote(trigger.group(2)))

    try:
        raw = web.get(URL)
    except HTTPError:
        bot.say('The GitHub API returned an error.')
        return NOLIMIT

    try:
        if firstParam.isdigit():
            data = json.loads(raw)
        else:
            data = json.loads(raw)['issues'][-1]
    except (KeyError, IndexError):
        return bot.say('No search results.')
    try:
        if len(data['body'].split('\n')) > 1:
            body = data['body'].split('\n')[0] + '...'
        else:
            body = data['body'].split('\n')[0]
    except (KeyError):
        LOGGER.exception('API returned an invalid result on query request %s',
                      trigger.group(2))
        bot.say('Invalid result, please try again later.')
        return NOLIMIT
    bot.reply('[#%s]\x02title:\x02 %s \x02|\x02 %s' % (data['number'], data['title'], body))
    bot.say(data['html_url'])


@rule('.*%s.*' % issueURL)
def issue_info(bot, trigger, match=None):
    match = match or trigger
    URL = 'https://api.github.com/repos/%s/issues/%s' % (match.group(1), match.group(2))

    try:
        raw = web.get(URL)
    except HTTPError:
        bot.say('The GitHub API returned an error.')
        return NOLIMIT
    data = json.loads(raw)
    try:
        if len(data['body'].split('\n')) > 1:
            body = data['body'].split('\n')[0] + '...'
        else:
            body = data['body'].split('\n')[0]
    except (KeyError):
        bot.say('The API says this is an invalid issue. Please report this if you know it\'s a correct link!')
        return NOLIMIT
    bot.say('[#%s]\x02title:\x02 %s \x02|\x02 %s' % (data['number'], data['title'], body))

Dernière modification par azgarech (09-03-2015 02:18:44)


Security is always excessive until it’s not enough. — Robbie Sinclair

Hors ligne

#6 09-03-2015 02:52:23

WarLocG
#! modo de compet

Re : [Aide modification Module] Pour le irc bot willie

Si je peux me permettre de t'apporter une suggestion pour ton test:

Python 2.7.3 (default, Mar 14 2014, 11:57:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> test=re.search('^[\w ]+\.[\w ]+$',"Title . Description")
>>> print test
<_sre.SRE_Match object at 0xb747d410>
>>> test=re.search('^[\w ]+\.[\w ]+$',"Title")
>>> print test
None

Le \w gère les caractères [a-zA-Z0-9_] ca devrait être suffisant smile


Avant de poser vos questions, jeter un oeil ici
Mon CodeVault et Wiki : ici
Les messages privés envers le staff sont uniquement pour les cas d'urgence ou affaires privées (personnelles). Les demandes se feront exclusivement sur le forum. Merci de respecter cette clause sous peine de sanctions.

Hors ligne

Pied de page des forums