-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c75a0a
commit 7e83a50
Showing
4 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
from selenium import webdriver | ||
import time | ||
from urllib.request import urlopen | ||
from selenium.webdriver.common.by import By | ||
from selenium.webdriver.support.ui import WebDriverWait | ||
import selenium.webdriver.support.expected_conditions as EC | ||
from selenium.webdriver.firefox.options import Options | ||
from selenium.common.exceptions import TimeoutException | ||
from collections import OrderedDict | ||
from itertools import repeat | ||
from fpdf import FPDF | ||
import platform | ||
if platform.system()=='Windows': | ||
import winsound | ||
|
||
def ui_func(topic,user_name,psswd,txt_pdf): | ||
options = Options() | ||
options.headless = True | ||
driver = webdriver.Firefox(options=options) | ||
|
||
driver.get("https://medium.com/") | ||
driver.implicitly_wait(10) | ||
#action = webdriver.ActionChains(driver) | ||
time.sleep(2) | ||
|
||
login = driver.find_element_by_link_text('Sign in').click() | ||
|
||
try: | ||
login_google = driver.find_element_by_xpath('/html/body/div[4]/div/div/section/div[1]/div/button[1]').click() | ||
except TimeoutException: | ||
pass | ||
|
||
# You can log-in using google only | ||
print(" Logging in to Medium by using Google ") | ||
time.sleep(3) | ||
assert "Sign in – Google accounts" in driver.title | ||
user = driver.find_element_by_xpath('//*[@id="identifierId"]') | ||
# Enter your email or phone number as registered in Medium | ||
user.send_keys(user_name) # Provide your email or registered phone number here | ||
|
||
nextButton = driver.find_element_by_xpath('//*[@id="identifierNext"]/content') | ||
nextButton.click() | ||
time.sleep(2) | ||
|
||
user = driver.find_element_by_xpath('//*[@id="password"]/div[1]/div/div[1]/input') | ||
|
||
# Place just your password in the pass.txt file | ||
user.send_keys(psswd) | ||
|
||
LOG = driver.find_element_by_xpath('//*[@id="passwordNext"]/content').click() | ||
print('LOGIN SUCCESSFUL \n') | ||
|
||
topics = { | ||
0: 'Home', | ||
1: 'Technology', | ||
2: 'Culture', | ||
3: 'Entrepreneurship', | ||
4: 'Creativity', | ||
5: 'Self', | ||
6: 'Productivity', | ||
7: 'Design', | ||
8: 'Popular' | ||
} | ||
|
||
|
||
if topic == 0: | ||
t = driver.get("https://medium.com/"); | ||
elif topic<9: | ||
t = driver.get("https://medium.com/topic/"+topics[topic]); | ||
else: | ||
print('Please select a correct topic.') | ||
|
||
|
||
|
||
# To Scroll to the bottom/ a portion of page | ||
last_height = 1000 | ||
while True: | ||
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") | ||
time.sleep(2) | ||
new_height = driver.execute_script("return document.body.scrollHeight") | ||
if new_height >= last_height: | ||
break | ||
|
||
tag = driver.find_elements_by_tag_name('h3') | ||
tag_len = len(tag) | ||
links = [i.get_attribute('href') for i in driver.find_elements_by_xpath("//a[@data-action-source]")] | ||
unique_list = list(OrderedDict(zip(links, repeat(None)))) # To remove duplicates from list | ||
|
||
|
||
if txt_pdf == 1: | ||
title = 'Medium Grabber' | ||
pdf = FPDF() | ||
pdf.alias_nb_pages() | ||
pdf.add_page() | ||
pdf.set_title(title) | ||
pdf.set_font('Times', 'B', 24) | ||
pdf.cell(0, 10, 'Medium Grabber Links: ', 0, 1) | ||
pdf.set_font('Times', '', 12) | ||
if topic==0: # Different structure of Home and other topic pages. | ||
if tag_len > 0: | ||
for i,l in zip(range(tag_len), unique_list): | ||
pdf.set_text_color(r=0, g=0, b=0) | ||
pdf.cell(0, 6, str(time.strftime("%Y-%m-%d %H:%M")), 0, 1) # Writing the Article text | ||
text = tag[i].text.encode('latin-1', 'ignore') # Eliminates any non-ASCII charachters | ||
text = text.decode('latin-1') # Again decodes the string into byte-form | ||
pdf.cell(0, 6, text , 0, 1) | ||
pdf.set_text_color(r=0, g=0, b=250) # Writing the Article link | ||
pdf.multi_cell(0, 6, str(l), 0) | ||
pdf.cell(0, 6, ' ', 0, 1) | ||
else: | ||
for i in range(tag_len): | ||
pdf.set_text_color(r=0, g=0, b=0) | ||
pdf.cell(0, 6, time.strftime("%Y-%m-%d %H:%M"), 0, 1) | ||
text = tag[i].text.encode('latin-1', 'ignore') | ||
text = text.decode('latin-1') | ||
pdf.cell(0, 6,text, 0, 1) | ||
l = tag[i].find_element_by_css_selector('a').get_attribute('href') | ||
pdf.set_text_color(r=0, g=0, b=250) | ||
pdf.multi_cell(0, 6, str(l), 0) | ||
pdf.cell(0, 6, ' ', 0, 1) | ||
pdf.output('output.pdf', 'F') | ||
print('FINISHED! Please check the output.pdf file for the links. Happy reading. :) ') | ||
|
||
elif txt_pdf == 2: | ||
f= open("output.txt","a+", encoding='utf8') # Stores ouput in output.txt in the same file directory | ||
if topic==0: # Different structure of Home and other topic pages. | ||
if tag_len > 0: | ||
for i,l in zip(range(tag_len), unique_list): | ||
f.write(time.strftime("%Y-%m-%d %H:%M") + '\n') | ||
f.write(tag[i].text) | ||
f.write('\nLink is --> ' + str(l) + '\n\n') | ||
else: | ||
for i in range(tag_len): | ||
f.write(time.strftime("%Y-%m-%d %H:%M") + '\n') | ||
f.write(tag[i].text) | ||
l = tag[i].find_element_by_css_selector('a').get_attribute('href'); | ||
f.write('\nLink is --> ' + str(l) + '\n\n') | ||
|
||
f.close() | ||
|
||
else: | ||
print('Invalid input.') | ||
|
||
print('Finished') | ||
driver.close() | ||
duration = 2500 # millisecond | ||
freq = 440 # Hz | ||
if platform.system()=='Windows': | ||
winsound.Beep(freq, duration) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
from tkinter import * | ||
from tkinter import ttk | ||
from UI_Function import ui_func | ||
|
||
|
||
|
||
|
||
|
||
|
||
root = Tk() | ||
root.title('Medium Grabber') | ||
root.geometry('{}x{}'.format(460, 350)) | ||
|
||
txt_pdf = 1 | ||
|
||
'''def send_string(): | ||
text.delete(1.0,END) | ||
text.insert(END, "Searching for '" + search_entry.get() + "' ....") | ||
search_entry.delete(0, END)''' | ||
|
||
def Get_Credentials(): | ||
global user,psswd | ||
user = username_widget.get() | ||
psswd = password_widget.get() | ||
display.insert(END,'Now choose file format(by default PDF)\n') | ||
print(user) | ||
|
||
def txt_pdf_func(n): | ||
global txt_pdf | ||
txt_pdf = n | ||
if txt_pdf==1: | ||
display.insert(END,'You chose PDF\n') | ||
else: | ||
display.insert(END,'You chose Text\n') | ||
print(txt_pdf) | ||
|
||
|
||
def button_func(topic): | ||
print(topic) | ||
ui_func(topic,user,psswd,txt_pdf) | ||
display.insert(END,'Finished\n') | ||
|
||
|
||
nb = ttk.Notebook(root) | ||
nb.pack(expan=1, fill='both') | ||
|
||
user = 'gsfg' | ||
psswd = '' | ||
|
||
|
||
tab1 = ttk.Frame(nb) | ||
nb.add(tab1 , text='Medium Grabber') | ||
tab1['padding'] = (5,10) | ||
|
||
|
||
topFrame1 = ttk.Frame(tab1, padding=5) | ||
topFrame1.pack(side=TOP, fill='x') | ||
|
||
topFrame2 = ttk.Frame(tab1, padding=5) | ||
topFrame2.pack(side=TOP, fill='x') | ||
|
||
topFrame3 = ttk.Frame(tab1, padding=5) | ||
topFrame3.pack(side=TOP, fill='x') | ||
|
||
topFrame4 = ttk.Frame(tab1, padding=5) | ||
topFrame4.pack(side=TOP, fill='x') | ||
|
||
centre1 = ttk.Frame(tab1,padding=5) | ||
centre1.pack(fill='x') | ||
|
||
centre2 = ttk.Frame(tab1, padding=5) | ||
centre2.pack(fill='x') | ||
|
||
bottomFrame = ttk.Frame(tab1) | ||
bottomFrame.pack(side=BOTTOM, fill='x') | ||
|
||
userLabel = Label(topFrame1, text='Username :') | ||
username_widget = Entry(topFrame1, width=15) | ||
userLabel.pack(side=LEFT) | ||
username_widget.pack(side=LEFT, fill='x', expand=2) | ||
|
||
passLabel = Label(topFrame2, text=' Password :') | ||
password_widget = Entry(topFrame2, show="*", width=100) | ||
passLabel.pack(side=LEFT) | ||
password_widget.pack(side=LEFT, fill='x', expand=2) | ||
|
||
submit_button = Button(topFrame3, text='Submit', command = Get_Credentials) | ||
submit_button.pack() | ||
|
||
choose_label = Label(topFrame4, text='CHOOSE : ') | ||
choose_label.pack(side=LEFT) | ||
|
||
text = Button(topFrame4, text='TEXT', width=20, command=lambda m=2: txt_pdf_func(m)) | ||
text.pack(side=LEFT) | ||
|
||
pdf = Button(topFrame4, text='PDF',width=20, command=lambda m=1: txt_pdf_func(m)) | ||
pdf.pack(side=LEFT) | ||
|
||
|
||
|
||
button1 = Button(centre1, text='TECHNOLOGY', width=14, command=lambda m=1: button_func(m)) | ||
button1.pack(side=LEFT) | ||
|
||
button2 = Button(centre1, text='CULTURE', width=14, command=lambda m=2: button_func(m)) | ||
button2.pack(side=LEFT) | ||
|
||
button3 = Button(centre1, text='ENTREPRENEURSHIP', width=15, command=lambda m=3: button_func(m)) | ||
button3.pack(side=LEFT) | ||
|
||
button4 = Button(centre1, text='CREATIVITY', width=14, command=lambda m=4: button_func(m)) | ||
button4.pack(side=LEFT) | ||
|
||
button5 = Button(centre2, text='SELF', width=14, command=lambda m=5: button_func(m)) | ||
button5.pack(side=LEFT) | ||
|
||
button6 = Button(centre2, text='PRODUCTIVITY', width=14, command=lambda m=6: button_func(m)) | ||
button6.pack(side=LEFT) | ||
|
||
button7 = Button(centre2, text='DESIGN', width=15, command=lambda m=7: button_func(m)) | ||
button7.pack(side=LEFT) | ||
|
||
button8 = Button(centre2, text='POPULAR', width=14, command=lambda m=8: button_func(m)) | ||
button8.pack(side=LEFT) | ||
|
||
display = Text(bottomFrame, width=50, height= 20) | ||
display.pack() | ||
|
||
|
||
'''tab2 = ttk.Frame(nb) | ||
nb.add(tab2 , text='Tab2') | ||
tab2['padding'] = (5,5) | ||
text = Text(tab2, width=40, height=10) | ||
search_label = Label(tab2, text='Search:') | ||
search_entry = Entry(tab2, width=40) | ||
search_button = Button(tab2, text='Search', command=send_string) | ||
text.pack(side=BOTTOM) | ||
search_entry.pack() | ||
search_button.pack()''' | ||
|
||
|
||
|
||
root.mainloop() | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ urllib3==1.24.1 | |
fpdf | ||
pytest | ||
flask | ||
tkinter |