Skip to content

Commit

Permalink
Merge pull request #40 from e-beach/develop
Browse files Browse the repository at this point in the history
fix #39 by using non-urwid interactive mode on windows
  • Loading branch information
gautamkrishnar authored Apr 8, 2017
2 parents 3c07eeb + c6dd73f commit 2ed4f59
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions socli/socli.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,95 @@ def get_question_stats_and_answer(url):
answers = [s.get_text() for s in soup.find_all("div", class_="post-text")][1:] # first post is question, discard it.
return question_title, question_desc, question_stats, answers


def socli_interactive_windows(query):
"""
Interactive mode
:param query:
:return:
"""
try:
search_res = requests.get(soqurl + query, verify=False)
soup = BeautifulSoup(search_res.text, 'html.parser')
try:
soup.find_all("div", class_="question-summary")[0] # For explictly raising exception
tmp = (soup.find_all("div", class_="question-summary"))
tmp1 = (soup.find_all("div", class_="excerpt"))
i = 0
question_local_url = []
print(bold("\nSelect a question below:\n"))
while (i < len(tmp)):
if i == 10: break # limiting results
question_text = ' '.join((tmp[i].a.get_text()).split())
question_text = question_text.replace("Q: ","")
question_desc = (tmp1[i].get_text()).replace("'\r\n", "")
question_desc = ' '.join(question_desc.split())
print_warning(str(i + 1) + ". " + dispstr(question_text))
question_local_url.append(tmp[i].a.get("href"))
print(" " + dispstr(question_desc) + "\n")
i = i + 1
try:
op = int(inputs("\nType the option no to continue or any other key to exit:"))
while 1:
if (op > 0) and (op <= i):
dispres(sourl + question_local_url[op - 1])
cnt = 1 # this is because the 1st post is the question itself
while 1:
global tmpsoup
qna = inputs("Type " + bold("o") + " to open in browser, " + bold("n") + " to next answer, "+ bold("b") + " for previous answer or any other key to exit:")
if qna in ["n", "N"]:
try:
answer = (tmpsoup.find_all("div",class_="post-text")[cnt + 1].get_text())
print_green("\n\nAnswer:\n")
print("-------\n" + answer + "\n-------\n")
cnt = cnt + 1
except IndexError as e:
print_warning(" No more answers found for this question. Exiting...")
sys.exit(0)
continue
elif qna in ["b", "B"]:
if cnt == 1:
print_warning(" You cant go further back. You are on the first answer!")
continue
answer = (tmpsoup.find_all("div",class_="post-text")[cnt - 1].get_text())
print_green("\n\nAnswer:\n")
print("-------\n" + answer + "\n-------\n")
cnt = cnt - 1
continue
elif qna in ["o", "O"]:
import webbrowser
print_warning("Opening in your browser...")
webbrowser.open(sourl + question_local_url[op - 1])
else:
break
sys.exit(0)
else:
op = int(input("\n\nWrong option. select the option no to continue:"))
except Exception as e:
showerror(e)
print_warning("\n Exiting...")
sys.exit(0)
except IndexError:
print_warning("No results found...")
sys.exit(0)

except UnicodeEncodeError:
print_warning("\n\nEncoding error: Use \"chcp 65001\" command before using socli...")
sys.exit(0)
except requests.exceptions.ConnectionError:
print_fail("Please check your internet connectivity...")
except Exception as e:
showerror(e)
sys.exit(0)


def socli_interactive(query):
"""
Interactive mode
:return:
"""
if sys.platform == 'win32':
return socli_interactive_windows(query)

class UnicodeText(urwid.Text):
""" encode all text to utf-8 """
Expand Down

0 comments on commit 2ed4f59

Please # to comment.