Skip to content

Repair it to search for multiple sources and change suggested page to look like they are working #20

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

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
31 changes: 16 additions & 15 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Overflow?

Let’s take it one step further.

| ``from stackoverflow import quick_sort`` will go through the search
| ``from stackoverflow import sort`` will go through the search
results
| of ``[python] quick sort`` looking for the largest code block that
| of ``[python] sort`` looking for the largest code block that
doesn’t
| syntax error in the highest voted answer from the highest voted
question
Expand All @@ -18,20 +18,21 @@ Let’s take it one step further.

.. code:: python

>>> from stackoverflow import quick_sort, split_into_chunks

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

>>> print(list(split_into_chunks.chunk("very good chunk func")))
['very ', 'good ', 'chunk', ' func']

>>> print("I wonder who made split_into_chunks", split_into_chunks.__author__)
I wonder who made split_into_chunks https://stackoverflow.com/a/35107113

>>> print("but what's the license? Can I really use this?", quick_sort.__license__)
from stackoverflow import sort
print(sort.sort_iterable([3,1,2]))
>>>
print("I wonder who made sort", sort.__author__)
print("but what's the license? Can I really use this?", sort.__license__)

from stackoverflow import file_copy
print(file_copy.copyfile("main.py", "main2.py"))

""" prints
[1, 2, 3]
I wonder who made sort https://stackoverflow.com/a/49073645
but what's the license? Can I really use this? CC BY-SA 3.0
>>> assert("nice, attribution!")
████████████████████ [100.00%]main2.py
"""

This module is licensed under whatever license you want it to be as
long as the license is compatible with the fact that I blatantly
Expand Down
17 changes: 7 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
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 sort
print(sort.sort_iterable([3,1,2]))
print("I wonder who made sort", sort.__author__)
print("but what's the license? Can I really use this?", sort.__license__)

from stackoverflow import file_copy
print(file_copy.copyfile("main.py", "main2.py"))
30 changes: 19 additions & 11 deletions stackoverflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import html
import re
import sys
from copy import copy
from importlib._bootstrap import spec_from_loader

import requests


class StackOverflowImporter:
"""
`from stackoverflow import quick_sort` will go through the search results
Expand All @@ -19,9 +19,8 @@ class StackOverflowImporter:
@classmethod
def find_spec(cls, fullname, path=None, target=None):
spec = spec_from_loader(fullname, cls, origin='hell')
spec.__license__ = "CC BY-SA 3.0"
spec._url = cls._fetch_url(spec.name)
spec._code, spec.__author__ = cls._fetch_code(spec._url)
spec.__license__ = "CC BY-SA 3.0" # todo: fix this
spec._code, spec._url, spec.__author__ = cls.get_code_url_author(spec.name)
return spec

@classmethod
Expand All @@ -36,16 +35,24 @@ def exec_module(cls, module=None):
exec(module._code, module.__dict__)
except:
print(module._url)
print(module._code)
print(module)
raise

@classmethod
def get_code(cls, fullname):
return compile(cls._fetch_code(cls._fetch_url(fullname)), 'StackOverflow.com/' + fullname, 'exec')
def get_code_url_author(cls, fullname):
urls = cls._fetch_urls(fullname)
for url in urls:
try:
code, author = cls._fetch_code(url)
return compile(code, 'StackOverflow.com/' + fullname, 'exec') , url, author
except ImportError:
pass
raise ImportError("This question ain't got no good code.")


@classmethod
def get_source(cls, fullname):
return cls.get_code(fullname)
return cls.get_code_url_author(fullname)[0]

@classmethod
def is_package(cls, fullname):
Expand All @@ -54,7 +61,7 @@ def is_package(cls, fullname):
############################

@classmethod
def _fetch_url(cls, query):
def _fetch_urls(cls, query):
query = query.lower().replace("stackoverflow.", "").replace("_", " ")
ans = requests.get(cls.API_URL + "/search", {
"order": "desc",
Expand All @@ -65,7 +72,8 @@ def _fetch_url(cls, query):
}).json()
if not ans["items"]:
raise ImportError("Couldn't find any question matching `" + query + "`")
return ans["items"][0]["link"]
items = ans["items"]
return [i["link"] for i in items]

@classmethod
def _fetch_code(cls, url):
Expand All @@ -74,7 +82,7 @@ def _fetch_code(cls, url):

@staticmethod
def _find_code_in_html(s):
answers = re.findall(r'<div id="answer-.*?</table', s, re.DOTALL) # come get me, Zalgo
answers = re.findall(r'<div id="answer-.*<div class="answercell.*class="suggest-edit-post"', s, re.DOTALL) # come get me, Zalgo

def votecount(x):
"""
Expand Down