Created
November 7, 2012 21:56
-
-
Save margusmartsepp/4034761 to your computer and use it in GitHub Desktop.
Map financial entity by it's relations using Google finance
This file contains hidden or 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
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| // Requires reference to WebDriver.dll and WebDriver.Support.dll | |
| // Furthermore, Firefox uses "selenium-ide-1.9.0.xpi" plugin. | |
| using OpenQA.Selenium; | |
| using OpenQA.Selenium.Firefox; | |
| using OpenQA.Selenium.Support.UI; | |
| namespace SeleniumGoogleFinances | |
| { | |
| class Program | |
| { | |
| public class RelatedFinances | |
| { | |
| Dictionary<string, List<string>> results; | |
| Dictionary<string, string> knownURLs = new Dictionary<string, string>(); | |
| public RelatedFinances(string root) | |
| { | |
| this.results = new Dictionary<string, List<string>>(); | |
| this.branchRelated(root); | |
| } | |
| public override string ToString() | |
| { | |
| StringBuilder sb = new StringBuilder(); | |
| foreach (var key in results.Keys) | |
| foreach (var item in results[key]) | |
| sb.Append(key).Append(',').Append(item).Append('\n'); | |
| return sb.ToString(); | |
| } | |
| public void branchExpand() | |
| { | |
| HashSet<string> leafes = new HashSet<string>(); | |
| foreach (var items in results.Values) | |
| foreach (var item in items) | |
| leafes.Add(item); | |
| foreach (var item in leafes) | |
| this.branchRelated(item); | |
| } | |
| public bool branchRelated(string symbol) | |
| { | |
| if (results.ContainsKey(symbol)) | |
| return false; | |
| List<string> result = new List<string>(); | |
| IWebDriver browser = new FirefoxDriver(); | |
| WebDriverWait w = new WebDriverWait(browser, new TimeSpan(0, 0, 5)); | |
| try | |
| { | |
| string url = (this.knownURLs.ContainsKey(symbol) ? | |
| this.knownURLs[symbol] : "http://www.google.com/finance?q=" + symbol); | |
| browser.Navigate().GoToUrl(url); | |
| w.Until(d => d.FindElement(By.LinkText("Related companies"))); | |
| browser.FindElement(By.LinkText("Related companies")).Click(); | |
| w.Until(d => d.FindElement(By.ClassName("ctname"))); | |
| foreach (var item in browser.FindElements(By.ClassName("ctname"))){ | |
| result.Add(item.Text); | |
| try { | |
| var sibling = item.FindElement(By.XPath("//a[@title='" + item.Text + "']")); | |
| knownURLs.Add(item.Text, sibling.GetAttribute("href")); | |
| } catch (Exception) {/* ignore */} | |
| } | |
| } | |
| catch(TimeoutException te){ | |
| Console.Error.WriteLine("Error: " + symbol); | |
| } | |
| finally | |
| { | |
| browser.Quit(); | |
| } | |
| return this.add(result); | |
| } | |
| private bool add(List<string> result) | |
| { | |
| if (result.Count() == 0) | |
| return false; | |
| String key = result[0]; | |
| if (results.ContainsKey(key)) | |
| return false; | |
| result.RemoveAt(0); | |
| this.results.Add(key, result); | |
| return true; | |
| } | |
| } | |
| static void Main(string[] args) | |
| { | |
| string root = (args.Length > 0 ? args[0] : "Apple"); | |
| RelatedFinances rf = new RelatedFinances(root); | |
| rf.branchExpand(); | |
| rf.branchExpand(); | |
| Console.WriteLine(rf); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment