-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjourney.py
79 lines (68 loc) · 2.55 KB
/
journey.py
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
from flask import Flask, request
import urllib2
from bs4 import BeautifulSoup
import urllib
import pandas as pd
# from python-graph import *
from itertools import chain
import time
import random
from collections import Counter
from getWikiSubset import *
import time
from selenium import webdriver
from Tkinter import *
#
def highlight(element):
"""Highlights (blinks) a Selenium Webdriver element"""
driver = element._parent
def apply_style(s):
driver.execute_script("arguments[0].setAttribute('style', arguments[1]);",
element, s)
apply_style("background: yellow; border: 2px solid red;")
def takeJourney(start_node, end_node):
with open('trees/Full_Graph_400.txt', 'r') as f:
graph = f.read().split('\t')
paths = get_all_paths(graph, start_node, end_node, 5)
path_lengths = [len(p) for p in paths]
shortest_path = paths[path_lengths.index(min(path_lengths))]
driver = webdriver.Chrome('/usr/local/bin/chromedriver') # Optional argument, if not specified will search path.
mainWikiURL = "https://en.wikipedia.org%s"
template_wikiURL = "/wiki/%s"
start_URL = mainWikiURL % template_wikiURL % shortest_path[0]
driver.get(start_URL)
#
for step in shortest_path[1:]:
time.sleep(2.5)
element = driver.find_elements_by_xpath('//a[@href="%s"]' % template_wikiURL % step)[0]
driver.execute_script("return arguments[0].scrollIntoView();", element)
time.sleep(.5)
highlight(element)
time.sleep(2)
try:
element.click()
except:
driver.get(mainWikiURL % template_wikiURL % step)
# with open('trees/Full_Graph_400.txt', 'r') as f:
# graph = f.read().split('\t')
#
# node_picks = list(set([e for e in graph if e != 'abcdefg']))
# start_node = end_node = node_picks[random.randint(0, len(node_picks)-1)]
# while end_node == start_node:
# end_node = node_picks[random.randint(0, len(node_picks)-1)]
#
# paths = get_all_paths(graph, start_node, end_node, 5)
# path_lengths = [len(p) for p in paths]
# shortest_path = paths[path_lengths.index(min(path_lengths))]
#
# print "There were a total of %d paths from %s to %s " % (len(paths), start_node, end_node)
# print "The shortest path is:"
# print ' --> '.join(shortest_path)
# shortest_path = shortest_path[1:]
#
# shortest_path = ['Flag_of_Washington',
# 'Roland_H._Hartley',
# 'Republican_Party_(United_States)',
# 'Donald_Trump',
# 'Mike_Tyson_vs._Michael_Spinks',
# 'Oprah_Winfrey']