File tree 5 files changed +55
-7
lines changed
5 files changed +55
-7
lines changed Original file line number Diff line number Diff line change
1
+ # Wiki cli searcher
2
+
3
+ Right now it is only able to search in Wikipedia. Techichally can search in every MediaWiki website (api to be added)
4
+
5
+ Works by requesting generated page contents, filtering them, converting them to Markdown and then displaying them.
6
+
7
+ ## Usage
8
+
9
+ ``` bash
10
+ python3 wiki < keyword>
11
+ ```
12
+
13
+ ## Example
14
+
15
+ ![ tool screenshot] ( imgs/screenshot.png )
Original file line number Diff line number Diff line change 1
1
import argparse
2
+ import logging as log
3
+ import sys
4
+
2
5
from bs4 import BeautifulSoup
3
6
from markdownify import MarkdownConverter
4
7
from rich .console import Console
5
8
from rich .markdown import Markdown
6
- from soup_filters import filter_soup
7
- from wikipedia import NoArticleFound , fetch_wiki_data
8
- from options import Options
9
- import logging as log
9
+
10
+ from . options import Options
11
+ from . soup_filters import filter_soup
12
+ from . wikipedia import NoArticleFound , fetch_wiki_data
10
13
11
14
log .basicConfig (level = log .INFO )
12
15
26
29
page_info = fetch_wiki_data (options , args .keyword )
27
30
except NoArticleFound as e :
28
31
console .print (f'No article "{ e .fprompt } " found' )
29
- exit (1 )
32
+ sys . exit (1 )
30
33
31
34
soup = BeautifulSoup (page_info ["text" ]["*" ], "html.parser" )
32
35
Original file line number Diff line number Diff line change
1
+ """Wikipedia search options module"""
1
2
from dataclasses import dataclass
2
3
3
4
4
5
@dataclass
5
6
class Options :
7
+ """Wikipedia search options class"""
8
+
6
9
class_prefix : str = "en"
7
10
skip_tables : bool = True
Original file line number Diff line number Diff line change
1
+ """A bs4 filtering module."""
1
2
from bs4 import BeautifulSoup
2
- from options import Options
3
+
4
+ from .options import Options
3
5
4
6
5
7
def filter_soup (opts : Options , title : str , soup : BeautifulSoup ) -> BeautifulSoup :
8
+ """Apply filtering to the soup.
9
+
10
+ :param opts: Request options.
11
+ :type opts: Options
12
+ :param title: Title of the article.
13
+ :type title: str
14
+ :param soup: BeautifoulSoup to filter.
15
+ :type soup: BeautifulSoup
16
+ :return: Filtered BeautifulSoup.
17
+ :rtype: BeautifulSoup
18
+ """
6
19
body = soup .find ("div" , class_ = "mw-parser-output" )
7
20
for tag in body .find_all ("table" , class_ = "infobox" ):
8
21
tag .decompose ()
Original file line number Diff line number Diff line change
1
+ """Wikipedia api request module."""
1
2
import requests
2
- from options import Options
3
+
4
+ from .options import Options
3
5
4
6
5
7
class NoArticleFound (Exception ):
8
+ """No found article exception class."""
9
+
6
10
def __init__ (self , failed_prompt : str ):
7
11
self .fprompt = failed_prompt
8
12
9
13
10
14
def fetch_wiki_data (opts : Options , prompt : str ) -> str :
15
+ """Fetch data html from Wiki api
16
+
17
+ :param opts: Request options.
18
+ :type opts: Options
19
+ :param prompt: Prompt to search at Wikipedia.
20
+ :type prompt: str
21
+ :raises NoArticleFound: Raised if no article found at Wikipedia.
22
+ :return: String of HTML returned by API
23
+ :rtype: str
24
+ """
11
25
url = f"https://{ opts .class_prefix } .wikipedia.org/w/api.php"
12
26
params = {
13
27
"action" : "parse" ,
You can’t perform that action at this time.
0 commit comments