Skip to content

Commit

Permalink
Tweaked CF support
Browse files Browse the repository at this point in the history
  • Loading branch information
AcidWeb committed Sep 12, 2019
1 parent f7ef661 commit a604d56
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 25 deletions.
11 changes: 6 additions & 5 deletions CB/Core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pathlib import Path
from checksumdir import dirhash
from xml.dom.minidom import parse
from . import retry, __version__
from . import retry, HEADERS, __version__
from .GitLab import GitLabAddon
from .CurseForge import CurseForgeAddon
from .WoWInterface import WoWInterfaceAddon
Expand Down Expand Up @@ -270,7 +270,7 @@ def find_orphans(self):
def search(self, query):
results = []
payload = requests.get(f'https://addons-ecs.forgesvc.net/api/v2/addon/search?gameId=1&pageSize=10&searchFilter='
f'{html.escape(query.strip())}').json()
f'{html.escape(query.strip())}', headers=HEADERS).json()
for result in payload:
results.append(result['websiteUrl'])
return results
Expand All @@ -289,7 +289,7 @@ def create_reg(self):
def parse_cf_xml(self, path):
xml = parse(path)
project = xml.childNodes[0].getElementsByTagName('project')[0].getAttribute('id')
payload = requests.get(f'https://addons-ecs.forgesvc.net/api/v2/addon/{project}').json()
payload = requests.get(f'https://addons-ecs.forgesvc.net/api/v2/addon/{project}', headers=HEADERS).json()
url = payload['websiteUrl'].strip()
self.config['CurseCache'][url] = project
self.save_config()
Expand All @@ -305,10 +305,11 @@ def bulk_check(self, addons):
elif addon['URL'].startswith('https://www.wowinterface.com/downloads/'):
ids_wowi.append(re.findall(r'\d+', addon['URL'])[0].strip())
if len(ids_cf) > 0:
payload = requests.post('https://addons-ecs.forgesvc.net/api/v2/addon', json=ids_cf).json()
payload = requests.post('https://addons-ecs.forgesvc.net/api/v2/addon', json=ids_cf, headers=HEADERS).json()
for addon in payload:
self.cfCache[str(addon['id'])] = addon
if len(ids_wowi) > 0:
payload = requests.get(f'https://api.mmoui.com/v3/game/WOW/filedetails/{",".join(ids_wowi)}.json').json()
payload = requests.get(f'https://api.mmoui.com/v3/game/WOW/filedetails/{",".join(ids_wowi)}.json',
headers=HEADERS).json()
for addon in payload:
self.wowiCache[str(addon['UID'])] = addon
9 changes: 5 additions & 4 deletions CB/CurseForge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import io
import zipfile
import requests
from . import retry
from . import retry, HEADERS
from operator import itemgetter
from xml.dom.minidom import parseString

Expand All @@ -13,13 +13,14 @@ def __init__(self, url, idcache, checkcache, clienttype, allowdev):
if url in idcache:
project = idcache[url]
else:
xml = parseString(requests.get(url + '/download-client').text)
xml = parseString(requests.get(url + '/download-client', headers=HEADERS).text)
project = xml.childNodes[0].getElementsByTagName('project')[0].getAttribute('id')
self.cacheID = project
if project in checkcache:
self.payload = checkcache[project]
else:
self.payload = requests.get(f'https://addons-ecs.forgesvc.net/api/v2/addon/{project}').json()
self.payload = requests.get(f'https://addons-ecs.forgesvc.net/api/v2/addon/{project}',
headers=HEADERS).json()
if not len(self.payload['latestFiles']) > 0:
raise RuntimeError
self.name = self.payload['name'].strip().strip('\u200b')
Expand Down Expand Up @@ -47,7 +48,7 @@ def get_current_version(self):

@retry()
def get_addon(self):
self.archive = zipfile.ZipFile(io.BytesIO(requests.get(self.downloadUrl).content))
self.archive = zipfile.ZipFile(io.BytesIO(requests.get(self.downloadUrl, headers=HEADERS).content))
for file in self.archive.namelist():
if '/' not in os.path.dirname(file):
self.directories.append(os.path.dirname(file))
Expand Down
8 changes: 4 additions & 4 deletions CB/GitLab.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import shutil
import zipfile
import requests
from . import retry
from . import retry, HEADERS


class GitLabAddon:
@retry()
def __init__(self, name, projectid, path, branch):
self.payload = requests.get(f'https://git.tukui.org/api/v4/projects/{projectid}/repository/branches/{branch}')\
.json()
self.payload = requests.get(f'https://git.tukui.org/api/v4/projects/{projectid}/repository/branches/{branch}',
headers=HEADERS).json()
if not self.payload['commit']:
raise RuntimeError
self.name = name
Expand All @@ -22,7 +22,7 @@ def __init__(self, name, projectid, path, branch):

@retry()
def get_addon(self):
self.archive = zipfile.ZipFile(io.BytesIO(requests.get(self.downloadUrl).content))
self.archive = zipfile.ZipFile(io.BytesIO(requests.get(self.downloadUrl, headers=HEADERS).content))
for file in self.archive.namelist():
file_info = self.archive.getinfo(file)
if file_info.is_dir() and file_info.filename.count('/') == 2 and '.gitlab' not in file_info.filename:
Expand Down
6 changes: 3 additions & 3 deletions CB/WeakAura.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import requests
from lupa import LuaRuntime
from pathlib import Path
from . import retry
from . import retry, HEADERS


class WeakAuraUpdater:
Expand Down Expand Up @@ -56,7 +56,7 @@ def check_updates(self):
wa = [[], []]
if len(self.waList) > 0:
payload = requests.get(f'https://data.wago.io/api/check/weakauras?ids={",".join(self.waList.keys())}',
headers={'api-key': self.apiKey}).json()
headers={'api-key': self.apiKey, 'User-Agent': HEADERS['User-Agent']}).json()
if 'error' in payload or 'msg' in payload:
raise RuntimeError('Wago API failed to return proper data. '
'The page is down or provided API key is incorrect.')
Expand All @@ -76,7 +76,7 @@ def check_updates(self):
@retry('Failed to parse WeakAura data.')
def update_aura(self, aura):
raw = requests.get(f'https://data.wago.io/api/raw/encoded?id={aura["slug"]}',
headers={'api-key': self.apiKey}).text
headers={'api-key': self.apiKey, 'User-Agent': HEADERS['User-Agent']}).text
slug = f' ["{aura["slug"]}"] = {{\n name = [=[{aura["name"]}]=],\n author = [=[' \
f'{aura["username"]}]=],\n encoded = [=[{raw}]=],\n wagoVersion = [=[' \
f'{aura["version"]}]=],\n wagoSemver = [=[{aura["versionString"]}]=],\n }},\n'
Expand Down
7 changes: 4 additions & 3 deletions CB/WoWInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import zipfile
import requests
from . import retry
from . import retry, HEADERS


class WoWInterfaceAddon:
Expand All @@ -13,7 +13,8 @@ def __init__(self, url, checkcache):
if project in checkcache:
self.payload = checkcache[project]
else:
self.payload = requests.get(f'https://api.mmoui.com/v3/game/WOW/filedetails/{project}.json').json()[0]
self.payload = requests.get(f'https://api.mmoui.com/v3/game/WOW/filedetails/{project}.json',
headers=HEADERS).json()[0]
if not self.payload['UID'] == project:
raise RuntimeError
self.name = self.payload['UIName'].strip().strip('\u200b')
Expand All @@ -24,7 +25,7 @@ def __init__(self, url, checkcache):

@retry()
def get_addon(self):
self.archive = zipfile.ZipFile(io.BytesIO(requests.get(self.downloadUrl).content))
self.archive = zipfile.ZipFile(io.BytesIO(requests.get(self.downloadUrl, headers=HEADERS).content))
for file in self.archive.namelist():
if '/' not in os.path.dirname(file):
self.directories.append(os.path.dirname(file))
Expand Down
6 changes: 5 additions & 1 deletion CB/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
__version__ = '2.3.0'
import string
import random

__version__ = '2.3.1'
__license__ = 'GPLv3'
__copyright__ = '2019, Paweł Jastrzębski <[email protected]>'
__docformat__ = 'restructuredtext en'
Expand Down Expand Up @@ -53,3 +56,4 @@ def __init__(self):


AC = AnsiCodes()
HEADERS = {'User-Agent': f'CB-{"".join(random.choices(string.ascii_uppercase + string.digits, k=10))}/{__version__}'}
13 changes: 8 additions & 5 deletions CurseBreaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from prompt_toolkit.completion import WordCompleter
from ctypes import windll, wintypes, byref
from distutils.version import StrictVersion
from CB import AC, __version__
from CB import AC, HEADERS, __version__
from CB.Core import Core
from CB.WeakAura import WeakAuraUpdater

Expand Down Expand Up @@ -146,7 +146,8 @@ def start(self):
def auto_update(self):
if getattr(sys, 'frozen', False):
try:
payload = requests.get('https://api.github.com/repos/AcidWeb/CurseBreaker/releases/latest').json()
payload = requests.get('https://api.github.com/repos/AcidWeb/CurseBreaker/releases/latest',
headers=HEADERS).json()
remoteversion = payload['name']
changelog = payload['body']
url = payload['assets'][0]['browser_download_url']
Expand All @@ -155,7 +156,7 @@ def auto_update(self):
if os.path.isfile(sys.executable + '.old'):
os.remove(sys.executable + '.old')
shutil.move(sys.executable, sys.executable + '.old')
payload = requests.get(url)
payload = requests.get(url, headers=HEADERS)
with open(sys.executable, 'wb') as f:
f.write(payload.content)
printft(HTML(f'<ansibrightgreen>Update complete! Please restart the application.</ansibrightgreen'
Expand Down Expand Up @@ -196,9 +197,11 @@ def setup_completer(self):
# noinspection PyBroadException
try:
self.cfSlugs = pickle.load(gzip.open(io.BytesIO(
requests.get('https://storage.googleapis.com/cursebreaker/cfslugs.pickle.gz').content)))
requests.get('https://storage.googleapis.com/cursebreaker/cfslugs.pickle.gz',
headers=HEADERS).content)))
self.wowiSlugs = pickle.load(gzip.open(io.BytesIO(
requests.get('https://storage.googleapis.com/cursebreaker/wowislugs.pickle.gz').content)))
requests.get('https://storage.googleapis.com/cursebreaker/wowislugs.pickle.gz',
headers=HEADERS).content)))
except Exception:
self.cfSlugs = []
self.wowiSlugs = []
Expand Down

0 comments on commit a604d56

Please sign in to comment.