forked from ulshell/StackCli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.py
More file actions
166 lines (128 loc) · 4.59 KB
/
services.py
File metadata and controls
166 lines (128 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
from termcolor import colored
from sys import platform
from pyfiglet import figlet_format
import requests
import os
import random
import click
#######################
###### STACK CLI ######
#######################
#Defining global parameters to reduce complexity
#System variables to ensure proper functioning
global chrome_options
global chromedriver
global driver
global questions
global answers
print(figlet_format('StackCLI', font='starwars'))
def configure_headless():
global chrome_options
global chromedriver
global driver
# Initiang the browser in headless mode
# --headless option can be removed to view the browser
# Using 1920x1080 as default resolution
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")
# Setting webdriver files based on the platform StackCLI is running
if platform == "linux" or platform == "linux2":
name = "/linux"
elif platform == "darwin":
name = "/mac"
elif platform == "win32":
name = "/win.exe"
#Platform detection enabled for chromedriver files
chrome_driver = os.getcwd() + name
#Passing location of the chromedriver executable
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
def nth_answer(n, question, option=''):
global questions
#Passing no parameter to ensure, print is not initiated
get_answers(question, 'NULL')
link = questions[n]
# Searching for links (Iterating through the <a href></a> tags
for a in link.find_all('a'):
if a.has_attr('href'):
ans = a.attrs['href']
# If user request for Nth answer, newurl needs to be generated
url = 'http://stackoverflow.com'+ans
if option == 'screenshot':
driver.get(url)
filename = str(random.randint(0,1000))
driver.get_screenshot_as_file(filename + '.png')
# Getting a random name for screenshot
# ensuring previous ones don't get overwritten
else:
page = requests.get(url)
data = page.text
soup = BeautifulSoup(data,'html.parser')
soup = soup.find('div', {"id":"content"})
heading = soup.find('a', {"class":"question-hyperlink"})
click.echo(colored('Q : '+heading.text, 'red'))
body = soup.find('div', {"class":"post-text"})
click.echo(colored(body.text, 'blue'))
# Getting content from the page
def stackoverflow(question):
configure_headless()
# Setting option by calling configure_headless()
global chrome_options
global chromedriver
global driver
#opening stackoverflow in the headless chrome browser
driver.get("https://stackoverflow.com/")
#location search box using the Xpath (XML Path)
search = driver.find_element_by_name("q")
search.send_keys(question)
#writing queries to the search box using send_keys
search_button = driver.find_element_by_xpath("/html/body/header/div/form/div/button")
search_button.click()
#Simulating click on the seach button on the page
return driver.current_url
#Returning the url of the page after initiating click
def display_answers(questions, answers):
index = 0
#Picking elements from the question and answers list using the index
for index in range(0, len(questions)):
#Priting the question on index
click.echo(colored(questions[index].text.replace(" ",""), 'red'))
link = questions[index]
for a in link.find_all('a'):
if a.has_attr('href'):
ans = a.attrs['href']
#Generating clickable link on the terminal
click.echo(colored('http://stackoverflow.com'+ans, 'green'))
#Priting the answer on the index
click.echo(colored(answers[index].text.replace(" ",""), 'blue'))
def kill_browser():
#killing the webdriver processes for cleaning up the memory due to
#unexpected network error
driver.close()
driver.quit()
def get_answers(question, option=''):
global questions
url = stackoverflow(question)
page = requests.get(url)
data = page.text
soup = BeautifulSoup(data,'html.parser')
#generating the source of the page and locating Q&A section
soup = soup.find("div", {"class":"js-search-results"})
try:
questions = soup.find_all('div', {"class":"result-link"})
answers = soup.find_all('div', {"class":"excerpt"})
#eaching for questions and answers using the class id's
if option == "search":
display_answers(questions, answers)
except:
#get_answers(question, option)
click.echo('Network Error --> Killing Browser')
def main(question, screenshot):
global driver
get_answers(question, 'screenshot')
filename = str(random.randint(0,1000))
driver.get_screenshot_as_file(filename + '.png')
#Generating screenshot and saving it with a random name