-
Notifications
You must be signed in to change notification settings - Fork 52
New module, speccy.py #208
base: gonzobot
Are you sure you want to change the base?
Changes from 11 commits
7f19215
e4adf75
ef2487f
3ed293c
f46eabb
cec4812
31aae3d
f2d3c28
4245df6
07e8a30
efceb1d
42e876d
dc2480a
48d1711
bafcc32
ee495da
fb17d50
c977e3c
d49eb27
601e599
9d2d695
cadcfa4
efa4758
bf3cb84
1ca2b0b
dc5a781
60cefc9
55bcc6c
1a2d613
ec039fc
a0baf00
6ee430c
c211e9a
4725b3f
288f03d
b10c6b0
9487894
196a1b5
1b73957
241abb2
77123d9
2dcd32a
a739caa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# -*- coding: utf-8 -*- | ||
from cloudbot import hook | ||
from cloudbot.event import EventType | ||
|
||
import asyncio | ||
import re | ||
import requests | ||
import tempfile | ||
|
||
from bs4 import BeautifulSoup as BS | ||
|
||
|
||
@asyncio.coroutine | ||
@hook.event([EventType.message, EventType.action], singlethread=True) | ||
def get_speccy_url(conn, message, chan, content, nick): | ||
re_content = re.search( | ||
r"https?:\/\/speccy.piriform.com\/results\/[A-z0-9]+", content) | ||
if re_content: | ||
return parse_speccy(message, nick, str(re_content.group(0))) | ||
|
||
|
||
def parse_speccy(message, nick, url): | ||
|
||
response = requests.get(url) | ||
if not response: | ||
return None | ||
|
||
respHtml = response.content | ||
speccy = tempfile.NamedTemporaryFile() | ||
|
||
with open(speccy.name, 'wb') as f: | ||
f.write(respHtml) | ||
|
||
soup = BS(open(speccy.name), "lxml-xml") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why use a file here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Going to look into the other two requests, but about this one. This was actually an optimization improvement. If you know a better way lmk. https://i.imgur.com/G2Si7PP.png I noticed it also parses the link faster too: https://i.imgur.com/R1eKAZT.png test.py = the method you mentioned test2.py = the current method seen in snippet There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please include your test case. Also the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't exactly know why it's faster, it was just a guess that ended up working, but here so you can try yourself: https://gist.github.com/Jessexd/14990cbadad5e62a22fb590bb25a4f9d Should I add multithreaded=True ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, you should remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, alright, as for BS, what should be done in terms of parsing links faster instead of writing to disk? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally just pass the content from the response straight in. import requests
from bs4 import BeautifulSoup
with requests.get(url) as response:
soup = BeautifulSoup(response.content, 'lxml', from_encoding=response.encoding)
print(soup) It may not be the fastest option, but it's the standard way as something like disk i/o can be extremely unpredictable. |
||
|
||
try: | ||
osspec = soup.body.find( | ||
"div", text='Operating System').next_sibling.next_sibling.text | ||
except AttributeError: | ||
return "Invalid Speccy URL" | ||
|
||
try: | ||
ramspec = soup.body.find( | ||
"div", text='RAM').next_sibling.next_sibling.text | ||
except AttributeError: | ||
ramspec = None | ||
|
||
try: | ||
cpuspec = soup.body.find( | ||
"div", text='CPU').next_sibling.next_sibling.text | ||
except AttributeError: | ||
cpuspec = None | ||
|
||
try: | ||
gpufind = soup.body.find( | ||
"div", text='Graphics').next_sibling.next_sibling.text | ||
gpuspec = "" | ||
for gpustring in re.finditer( | ||
r".*(amd|radeon|intel|integrated|nvidia|geforce|gtx).*\n.*", | ||
gpufind, re.IGNORECASE): | ||
gpuspec += gpustring.group() | ||
except AttributeError: | ||
gpuspec = None | ||
|
||
try: | ||
picospec = soup.body.find("div", text=re.compile('.*pico', re.I)).text | ||
except AttributeError: | ||
picospec = None | ||
|
||
try: | ||
kmsspec = soup.body.find("div", text=re.compile('.*kms', re.I)).text | ||
except AttributeError: | ||
kmsspec = None | ||
|
||
try: | ||
boosterspec = soup.body.find( | ||
"div", text=re.compile('.*booster', re.I)).text | ||
except AttributeError: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be replaces with |
||
boosterspec = None | ||
|
||
try: | ||
reviverspec = soup.body.find( | ||
"div", text=re.compile('.*reviver', re.I)).text | ||
except AttributeError: | ||
reviverspec = None | ||
|
||
try: | ||
killerspec = soup.body.find( | ||
"div", text=re.compile('.*Killer.+Service', re.I)).text | ||
except AttributeError: | ||
killerspec = None | ||
|
||
def smartcheck(): | ||
drivespec = soup.body.find_all("div", text="05") | ||
number_of_drives = len(drivespec) | ||
|
||
values = [] | ||
for i in range(0, number_of_drives): | ||
z = drivespec[i].next_sibling.next_sibling.stripped_strings | ||
saucy = list(z) | ||
rv_index = saucy.index("Raw Value:") | ||
raw_value = saucy[rv_index + 1] | ||
if raw_value != "0000000000": | ||
values.append(str(i + 1)) | ||
return values | ||
|
||
try: | ||
z = smartcheck() | ||
if len(z) != 0: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To check if a list is empty, you should use the |
||
smartspec = " Disk:" | ||
for item in z: | ||
smartspec += " #" + item + " " | ||
else: | ||
smartspec = None | ||
except Exception: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid broad exception clauses like this. Either log the returned exception in some way or make the except clause itself more precise. Do not consume errors that should be logged. |
||
smartspec = None | ||
|
||
badware_list = [picospec, kmsspec, boosterspec, reviverspec, killerspec] | ||
badware = ', '.join(filter(None, badware_list)) | ||
if not badware: | ||
badware = None | ||
|
||
specin = "\x02OS:\x02 {}\ | ||
● \x02RAM:\x02 {}\ | ||
● \x02CPU:\x02 {}\ | ||
● \x02GPU:\x02 {}\ | ||
● \x02Badware:\x02 {}\ | ||
● \x02Failing Drive(s):\x02 {}\ | ||
".format( | ||
osspec, ramspec, cpuspec, gpuspec, badware, smartspec) | ||
|
||
specout = re.sub("\s{2,}|\r\n|\n", " ", specin) | ||
|
||
return specout | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use
hook.regex()
rather than re-implementing the functionality.