howdoi
Bobloy 7 years ago
parent 447db7a133
commit 6ff12032b3

@ -13,26 +13,27 @@ import glob
import os import os
import random import random
import re import re
import requests
import requests_cache
import sys import sys
from . import __version__
import requests
import requests_cache
from pygments import highlight from pygments import highlight
from pygments.lexers import guess_lexer, get_lexer_by_name
from pygments.formatters.terminal import TerminalFormatter from pygments.formatters.terminal import TerminalFormatter
from pygments.lexers import guess_lexer, get_lexer_by_name
from pygments.util import ClassNotFound from pygments.util import ClassNotFound
from pyquery import PyQuery as pq from pyquery import PyQuery as pq
from requests.exceptions import ConnectionError from requests.exceptions import ConnectionError
from requests.exceptions import SSLError from requests.exceptions import SSLError
from . import __version__
# Handle imports for Python 2 and 3 # Handle imports for Python 2 and 3
if sys.version < '3': if sys.version < '3':
import codecs import codecs
from urllib import quote as url_quote from urllib import quote as url_quote
from urllib import getproxies from urllib import getproxies
# Handling Unicode: http://stackoverflow.com/a/6633040/305414 # Handling Unicode: http://stackoverflow.com/a/6633040/305414
def u(x): def u(x):
return codecs.unicode_escape_decode(x)[0] return codecs.unicode_escape_decode(x)[0]
@ -40,10 +41,10 @@ else:
from urllib.request import getproxies from urllib.request import getproxies
from urllib.parse import quote as url_quote from urllib.parse import quote as url_quote
def u(x): def u(x):
return x return x
if os.getenv('HOWDOI_DISABLE_SSL'): # Set http instead of https if os.getenv('HOWDOI_DISABLE_SSL'): # Set http instead of https
SCHEME = 'http://' SCHEME = 'http://'
VERIFY_SSL_CERTIFICATE = False VERIFY_SSL_CERTIFICATE = False
@ -59,7 +60,7 @@ USER_AGENTS = ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:11.0) Gecko/2010
('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) ' ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) '
'Chrome/19.0.1084.46 Safari/536.5'), 'Chrome/19.0.1084.46 Safari/536.5'),
('Mozilla/5.0 (Windows; Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46' ('Mozilla/5.0 (Windows; Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46'
'Safari/536.5'), ) 'Safari/536.5'),)
SEARCH_URLS = { SEARCH_URLS = {
'bing': SCHEME + 'www.bing.com/search?q=site:{0}%20{1}', 'bing': SCHEME + 'www.bing.com/search?q=site:{0}%20{1}',
'google': SCHEME + 'www.google.com/search?q=site:{0}%20{1}' 'google': SCHEME + 'www.google.com/search?q=site:{0}%20{1}'
@ -104,7 +105,7 @@ def _extract_links_from_bing(html):
def _extract_links_from_google(html): def _extract_links_from_google(html):
return [a.attrib['href'] for a in html('.l')] or \ return [a.attrib['href'] for a in html('.l')] or \
[a.attrib['href'] for a in html('.r')('a')] [a.attrib['href'] for a in html('.r')('a')]
def _extract_links(html, search_engine): def _extract_links(html, search_engine):
@ -220,7 +221,7 @@ def _get_instructions(args):
initial_position = args['pos'] initial_position = args['pos']
spliter_length = 80 spliter_length = 80
answer_spliter = '\n' + '=' * spliter_length + '\n\n' answer_spliter = '\n' + '=' * spliter_length + '\n\n'
for answer_number in range(args['num_answers']): for answer_number in range(args['num_answers']):
current_position = answer_number + initial_position current_position = answer_number + initial_position
args['pos'] = current_position args['pos'] = current_position

@ -1,11 +1,9 @@
import discord import discord
from discord.ext import commands from discord.ext import commands
from redbot.core import Config
from redbot.core.utils.chat_formatting import box
from .utils.chat_formatting import pagify from .howdoi_source import howdoi as hdi
from .utils.chat_formatting import box
from .howdoi_source import howdoi as hdi, Answer
class Howdoi: class Howdoi:
@ -24,7 +22,7 @@ class Howdoi:
"num_answers": 1, "num_answers": 1,
"clear_cache": False, "clear_cache": False,
"version": False "version": False
} }
self.config.register_global(**default_global) self.config.register_global(**default_global)
@ -34,52 +32,52 @@ class Howdoi:
Settings are reset on reload""" Settings are reset on reload"""
if ctx.invoked_subcommand is None: if ctx.invoked_subcommand is None:
await ctx.send_help() await ctx.send_help()
@howdoiset.command(pass_context=True, name="answers") @howdoiset.command(pass_context=True, name="answers")
async def howdoiset_answers(self, ctx, num_answers: int=1): async def howdoiset_answers(self, ctx, num_answers: int = 1):
"""Adjust number of answers provided. """Adjust number of answers provided.
Defaults to 1""" Defaults to 1"""
await self.config.num_answers.set(num_answers) await self.config.num_answers.set(num_answers)
await ctx.send("Number of answers provided will now be {}".format(num_answers)) await ctx.send("Number of answers provided will now be {}".format(num_answers))
@howdoiset.command(pass_context=True, name="link") @howdoiset.command(pass_context=True, name="link")
async def howdoiset_link(self, ctx): async def howdoiset_link(self, ctx):
"""Toggles providing in-line answers or a link """Toggles providing in-line answers or a link
Default On""" Default On"""
await self.config.link.set(not (await self.config.link())) await self.config.link.set(not (await self.config.link()))
if await self.config.link(): if await self.config.link():
await ctx.send("Answers will now be provided as a link") await ctx.send("Answers will now be provided as a link")
else: else:
await ctx.send("Answers will now be provided as the response") await ctx.send("Answers will now be provided as the response")
@howdoiset.command(pass_context=True, name="full") @howdoiset.command(pass_context=True, name="full")
async def howdoiset_full(self, ctx): async def howdoiset_full(self, ctx):
"""Toggles providing full answers or just first code found """Toggles providing full answers or just first code found
Default Off Default Off
Only works if links are turned off""" Only works if links are turned off"""
await self.config.all.set(not (await self.config.all())) await self.config.all.set(not (await self.config.all()))
if await self.config.all(): if await self.config.all():
await ctx.send("Answers will now be provided in full context") await ctx.send("Answers will now be provided in full context")
else: else:
await ctx.send("Answers will now be provided as a code snippet") await ctx.send("Answers will now be provided as a code snippet")
@commands.command(pass_context=True) @commands.command(pass_context=True)
async def howdoi(self, ctx, *question): async def howdoi(self, ctx, *question):
"""Ask a coding question""" """Ask a coding question"""
self.query = " ".join(question) self.query = " ".join(question)
await self.config.query.set(self.query) await self.config.query.set(self.query)
out = hdi.howdoi(await self.config.all()) # .encode('utf-8', 'ignore') out = hdi(await self.config.all()) # .encode('utf-8', 'ignore')
if await self.config.links(): if await self.config.links():
await ctx.send(out) await ctx.send(out)
else: else:
await ctx.send(box(out,"python")) await ctx.send(box(out, "python"))
# for page in pagify(out, shorten_by=24): # for page in pagify(out, shorten_by=24):
# await ctx.send(box(page)) # await ctx.send(box(page))

@ -13,26 +13,27 @@ import glob
import os import os
import random import random
import re import re
import requests
import requests_cache
import sys import sys
from . import __version__
import requests
import requests_cache
from pygments import highlight from pygments import highlight
from pygments.lexers import guess_lexer, get_lexer_by_name
from pygments.formatters.terminal import TerminalFormatter from pygments.formatters.terminal import TerminalFormatter
from pygments.lexers import guess_lexer, get_lexer_by_name
from pygments.util import ClassNotFound from pygments.util import ClassNotFound
from pyquery import PyQuery as pq from pyquery import PyQuery as pq
from requests.exceptions import ConnectionError from requests.exceptions import ConnectionError
from requests.exceptions import SSLError from requests.exceptions import SSLError
from . import __version__
# Handle imports for Python 2 and 3 # Handle imports for Python 2 and 3
if sys.version < '3': if sys.version < '3':
import codecs import codecs
from urllib import quote as url_quote from urllib import quote as url_quote
from urllib import getproxies from urllib import getproxies
# Handling Unicode: http://stackoverflow.com/a/6633040/305414 # Handling Unicode: http://stackoverflow.com/a/6633040/305414
def u(x): def u(x):
return codecs.unicode_escape_decode(x)[0] return codecs.unicode_escape_decode(x)[0]
@ -40,10 +41,10 @@ else:
from urllib.request import getproxies from urllib.request import getproxies
from urllib.parse import quote as url_quote from urllib.parse import quote as url_quote
def u(x): def u(x):
return x return x
if os.getenv('HOWDOI_DISABLE_SSL'): # Set http instead of https if os.getenv('HOWDOI_DISABLE_SSL'): # Set http instead of https
SCHEME = 'http://' SCHEME = 'http://'
VERIFY_SSL_CERTIFICATE = False VERIFY_SSL_CERTIFICATE = False
@ -59,7 +60,7 @@ USER_AGENTS = ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:11.0) Gecko/2010
('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) ' ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) '
'Chrome/19.0.1084.46 Safari/536.5'), 'Chrome/19.0.1084.46 Safari/536.5'),
('Mozilla/5.0 (Windows; Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46' ('Mozilla/5.0 (Windows; Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46'
'Safari/536.5'), ) 'Safari/536.5'),)
SEARCH_URLS = { SEARCH_URLS = {
'bing': SCHEME + 'www.bing.com/search?q=site:{0}%20{1}', 'bing': SCHEME + 'www.bing.com/search?q=site:{0}%20{1}',
'google': SCHEME + 'www.google.com/search?q=site:{0}%20{1}' 'google': SCHEME + 'www.google.com/search?q=site:{0}%20{1}'
@ -109,7 +110,7 @@ def _extract_links_from_bing(html):
def _extract_links_from_google(html): def _extract_links_from_google(html):
return [a.attrib['href'] for a in html('.l')] or \ return [a.attrib['href'] for a in html('.l')] or \
[a.attrib['href'] for a in html('.r')('a')] [a.attrib['href'] for a in html('.r')('a')]
def _extract_links(html, search_engine): def _extract_links(html, search_engine):

@ -1,10 +1,28 @@
{ {
"author" : ["Bobloy"], "author": [
"bot_version" : [3,0,0], "Bobloy"
"description" : "Answer coding questions using stackexchange", ],
"hidden" : false, "bot_version": [
"install_msg" : "Thank you for installing Howdoi.", 3,
"requirements" : ["Pygments==2.1.1", "argparse==1.4.0", "cssselect==0.9.1", "pyquery==1.4.0", "requests==2.9.1", "requests-cache==0.4.11"], 0,
"short" : "Answer coding questions", 0
"tags" : ["coding", "tools", "utils", "bobloy"] ],
"description": "Answer coding questions using stackexchange",
"hidden": false,
"install_msg": "Thank you for installing Howdoi.",
"requirements": [
"Pygments==2.1.1",
"argparse==1.4.0",
"cssselect==0.9.1",
"pyquery==1.4.0",
"requests==2.9.1",
"requests-cache==0.4.11"
],
"short": "Answer coding questions",
"tags": [
"coding",
"tools",
"utils",
"bobloy"
]
} }
Loading…
Cancel
Save