Skip to content

Commit

Permalink
Added search command
Browse files Browse the repository at this point in the history
  • Loading branch information
AcidWeb committed Mar 15, 2019
1 parent 12bb9c9 commit 31ccb50
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 25 deletions.
14 changes: 13 additions & 1 deletion CB/Core.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import os
import sys
import json
import html
import shutil
import zipfile
import datetime
import requests
from bs4 import BeautifulSoup
from tqdm import tqdm
from checksumdir import dirhash
from . import __version__
from . import retry, __version__
from .ElvUI import ElvUIAddon
from .CurseForge import CurseForgeAddon
from .WoWInterface import WoWInterfaceAddon
Expand Down Expand Up @@ -194,6 +197,15 @@ def find_orphans(self):
orphaneconfig.append(os.path.join(root, f)[4:])
return orphanedaddon, orphaneconfig

@retry(custom_error='Failed to execute the search.')
def search(self, query):
results = []
soup = BeautifulSoup(requests.get(f'https://www.curseforge.com/wow/addons/search?search='
f'{html.escape(query.strip())}').content, 'html.parser')
for row in soup.find_all('h2', attrs={'class': 'list-item__title strong mg-b-05'}):
results.append(f'https://www.curseforge.com{row.parent["href"]}')
return results

def create_reg(self):
with open('CurseBreaker.reg', 'w') as outfile:
outfile.write('Windows Registry Editor Version 5.00\n[HKEY_CURRENT_USER\Software\Classes\curse]\n"URL Proto'
Expand Down
6 changes: 3 additions & 3 deletions CB/CurseForge.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


class CurseForgeAddon:
@retry
@retry()
def __init__(self, url):
if url.startswith('https://www.curseforge.com/wow/addons/'):
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
Expand All @@ -33,7 +33,7 @@ def version_search(self, tag):
break
return version

@retry
@retry()
def get_current_version(self):
self.currentVersion = self.version_search('Release')
if self.currentVersion is None:
Expand All @@ -50,7 +50,7 @@ def get_current_version(self):
if self.currentVersion is None:
raise RuntimeError

@retry
@retry()
def get_addon(self):
self.archive = zipfile.ZipFile(io.BytesIO(requests.get(self.downloadUrl).content))
for file in self.archive.namelist():
Expand Down
4 changes: 2 additions & 2 deletions CB/ElvUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class ElvUIAddon:
@retry
@retry()
def __init__(self, branch):
self.soup = BeautifulSoup(requests.get(f'https://git.tukui.org/elvui/elvui/tree/{branch}').content,
'html.parser')
Expand All @@ -25,7 +25,7 @@ def get_current_version(self):
except Exception:
raise RuntimeError('Failed to parse addon page. URL is wrong or your source has some issues.')

@retry
@retry()
def get_addon(self):
self.archive = zipfile.ZipFile(io.BytesIO(requests.get(self.downloadUrl).content))
for file in self.archive.namelist():
Expand Down
4 changes: 2 additions & 2 deletions CB/TukUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class TukUIAddon:
@retry
@retry()
def __init__(self):
self.soup = BeautifulSoup(requests.get('https://git.tukui.org/Tukz/Tukui/tree/master').content, 'html.parser')
self.name = 'TukUI'
Expand All @@ -23,7 +23,7 @@ def get_current_version(self):
except Exception:
raise RuntimeError('Failed to parse addon page. URL is wrong or your source has some issues.')

@retry
@retry()
def get_addon(self):
self.archive = zipfile.ZipFile(io.BytesIO(requests.get(self.downloadUrl).content))
for file in self.archive.namelist():
Expand Down
4 changes: 2 additions & 2 deletions CB/WoWInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


class WoWInterfaceAddon:
@retry
@retry()
def __init__(self, url):
self.soup = BeautifulSoup(requests.get(url).content, 'html.parser')
self.name = self.soup.find('meta', attrs={'property': 'og:title'})['content']
Expand All @@ -22,7 +22,7 @@ def get_current_version(self):
except Exception:
raise RuntimeError('Failed to parse addon page. URL is wrong or your source has some issues.')

@retry
@retry()
def get_addon(self):
dsoup = BeautifulSoup(requests.get(self.downloadUrl).content, 'html.parser')
self.archive = zipfile.ZipFile(io.BytesIO(requests.get(dsoup.find('div', attrs={'class': 'manuallink'}).
Expand Down
34 changes: 20 additions & 14 deletions CB/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@
__docformat__ = 'restructuredtext en'


def retry(func):
def inner(*args, **kwargs):
for i in range(3):
# noinspection PyBroadException
try:
result = func(*args, **kwargs)
except KeyboardInterrupt:
raise
except Exception:
continue
def retry(custom_error=False):
def wraps(func):
def inner(*args, **kwargs):
for i in range(3):
# noinspection PyBroadException
try:
result = func(*args, **kwargs)
except KeyboardInterrupt:
raise
except Exception:
continue
else:
return result
else:
return result
else:
raise RuntimeError('Failed to parse addon page. URL is wrong or your source has some issues.')
return inner
if custom_error:
raise RuntimeError(custom_error)
else:
raise RuntimeError('Failed to parse addon page. URL is wrong or your source has some issues.')
return inner
return wraps

12 changes: 11 additions & 1 deletion CurseBreaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def print_header(self):
f'{__version__}</ansibrightred> ~~~</ansibrightblack>\n'))

def setup_completer(self):
commands = ['install', 'uninstall', 'update', 'force_update', 'status', 'orphans', 'toggle_backup',
commands = ['install', 'uninstall', 'update', 'force_update', 'status', 'orphans', 'search', 'toggle_backup',
'uri_integration', 'help', 'exit']
addons = sorted(self.core.config['Addons'], key=lambda k: k['Name'].lower())
for addon in addons:
Expand Down Expand Up @@ -254,6 +254,15 @@ def c_toggle_backup(self, _):
printft('Backup of WTF directory is now:',
HTML('<ansigreen>ENABLED</ansigreen>') if status else HTML('<ansired>DISABLED</ansired>'))

def c_search(self, args):
if args:
results = self.core.search(args)
printft(HTML('<ansigreen>Top results of your search:</ansigreen>'))
for url in results:
printft(url)
else:
printft(HTML('<ansigreen>Usage:</ansigreen>\n\tThis command accepts a search query as an argument.'))

def c_help(self, _):
printft(HTML('<ansigreen>install [URL]</ansigreen>\n\tCommand accepts a comma-separated list of links.'))
printft(HTML('<ansigreen>uninstall [URL/Name]</ansigreen>\n\tCommand accepts a comma-separated list of links or'
Expand All @@ -265,6 +274,7 @@ def c_help(self, _):
'ate.'))
printft(HTML('<ansigreen>status</ansigreen>\n\tPrints the current state of all installed addons.'))
printft(HTML('<ansigreen>orphans</ansigreen>\n\tPrints list of orphaned directories and files.'))
printft(HTML('<ansigreen>search [Keyword]</ansigreen>\n\tExecute addon search on CurseForge.'))
printft(HTML('<ansigreen>toggle_backup</ansigreen>\n\tEnable/disable automatic daily backup of WTF directory.'))
printft(HTML('<ansigreen>uri_integration</ansigreen>\n\tEnable integration with CurseForge page. "Install" butt'
'on will now start this application.'))
Expand Down

0 comments on commit 31ccb50

Please sign in to comment.