Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support more than one answer code detection #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
StackOverflow Importer
======================

Update to take the code of at most 10 answers into consider

.. code:: python

>>> from stackoverflow import quick_sort

>>> print(quick_sort.quickSort([1, 3, 2, 5, 4]))
[1, 2, 3, 4, 5]

======================

Do you ever feel like all you’re doing is copy/pasting from Stack
Overflow?

Expand Down
12 changes: 2 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
from stackoverflow import quick_sort, split_into_chunks
print(quick_sort.sort([1, 3, 2, 5, 4]))
print(list(split_into_chunks.chunk("very good chunk func")))
print("gotta take a break")
from time import time
t1 = time()
from stackoverflow import time_delay
print("that's enough, let's continue", time()-t1)
print("I wonder who made split_into_chunks", split_into_chunks.__author__)
print("but what's the license? Can I really use this?", quick_sort.__license__)
from stackoverflow import quick_sort
print(quick_sort.quickSort([1, 3, 2, 5, 4]))
19 changes: 12 additions & 7 deletions stackoverflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,21 @@ def _fetch_url(cls, query):
}).json()
if not ans["items"]:
raise ImportError("Couldn't find any question matching `" + query + "`")
return ans["items"][0]["link"]
# return ans["items"][0]["link"]
return ans["items"]

@classmethod
def _fetch_code(cls, url):
q = requests.get(url)
return cls._find_code_in_html(q.text)
def _fetch_code(cls, anses):
ans_html = []
for ans in anses[:10]:
ans_html.append(requests.get(ans["link"]))
return cls._find_code_in_html(ans_html)

@staticmethod
def _find_code_in_html(s):
answers = re.findall(r'<div id="answer-.*?</table', s, re.DOTALL) # come get me, Zalgo
def _find_code_in_html(ans_html):
answers = []
for an_html in ans_html:
answers.extend(re.findall(r'<div id="answer-.*?</table', an_html.text, re.DOTALL)) # come get me, Zalgo

def votecount(x):
"""
Expand All @@ -89,7 +94,7 @@ def votecount(x):
codez = map(lambda x: x.group(1), codez)
for code in sorted(codez, key=lambda x: -len(x)): # more code is obviously better
# don't forget attribution
author = s
author = an_html.text
author = author[author.find(code):]
author = author[:author.find(">share<")]
author = author[author.rfind('<a href="') + len('<a href="'):]
Expand Down