Created
January 19, 2013 12:25
-
-
Save bcambel/4572404 to your computer and use it in GitHub Desktop.
Funda API Test
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.Net; | |
| using System.IO; | |
| using System.Web; | |
| using System.Text.RegularExpressions; | |
| namespace FundaAPI | |
| { | |
| class MainClass | |
| { | |
| public static void Main (string[] args) | |
| { | |
| Console.WriteLine ("Hello World!"); | |
| FundaApier api = new FundaApier(); | |
| api.Begin(); | |
| Console.ReadLine(); | |
| } | |
| } | |
| class FundaApier{ | |
| static string FundaAPIEndPoint = "http://partnerapi.funda.nl/feeds/Aanbod.svc/json/a001e6c3ee6e4853ab18fe44cc1494de/?type=koop&zo=/amsterdam/tuin/&page={0}&pagesize=25"; | |
| static string pat = @"MakelaarId\"":(\d{4,6})\,\""MakelaarNaam\""";//:\"".+{10,20}\""\,\""MobileURL"; //\""(\s{0,50})\"" | |
| static string paging = @"AantalPaginas\"":(\d{1,6})\,\""HuidigePagina\"":(\d{1,6})"; | |
| static string CaptureText= @"{0}. Capture= {1}"; | |
| private Regex r ; | |
| private Regex pageReg; | |
| public FundaApier(){ | |
| this.r = new Regex(pat, RegexOptions.IgnoreCase); | |
| this.pageReg = new Regex(paging, RegexOptions.IgnoreCase); | |
| } | |
| public void Begin () | |
| { | |
| Iterate(0); | |
| } | |
| public bool Iterate (int page) | |
| { | |
| Console.WriteLine (String.Format ("=========BEGIN PAGE {0}============",page)); | |
| string content = makeRequest (page); | |
| Match m = r.Match (content); | |
| int idx = 1; | |
| while (m.Success) { | |
| Group makText = m.Groups [0]; | |
| Group makId = m.Groups [1]; | |
| var makelaarId = makId.Captures [0].Value; | |
| CaptureCollection cc = makText.Captures; | |
| for (int j = 0; j < cc.Count; j++) { | |
| Capture c = cc [j]; | |
| System.Console.WriteLine (String.Format (CaptureText, idx, makelaarId) + j + "='" + c + "', Position=" + c.Index); | |
| } | |
| idx++; | |
| m = m.NextMatch (); | |
| } | |
| Match pagingMatch = pageReg.Match (content); | |
| var pageMatchGroup = pagingMatch.Groups; | |
| string totalPagesStr = pageMatchGroup [1].Captures [0].Value; | |
| string currentPageStr = pageMatchGroup [2].Captures [0].Value; | |
| Console.WriteLine (String.Format ("Page {0} of {1}", currentPageStr, totalPagesStr)); | |
| Console.WriteLine (String.Format("==========PAGE {0} END ===========",currentPageStr)); | |
| int currentPage = 0; | |
| int totalPages = 0; | |
| var result = false; | |
| if (int.TryParse (totalPagesStr, out totalPages)) { | |
| if (int.TryParse(currentPageStr, out currentPage)){ | |
| if (currentPage >= totalPages){ | |
| Console.WriteLine("We're done baby!"); | |
| return true; | |
| } | |
| else{ | |
| var next_page = currentPage + 1; | |
| result = Iterate(next_page); | |
| } | |
| } | |
| } | |
| return false; | |
| } | |
| public string makeRequest(int page=0) | |
| { | |
| var url = String.Format(FundaAPIEndPoint,page); | |
| WebRequest webrequest = WebRequest.Create(url); | |
| var response = webrequest.GetResponse(); | |
| Stream stream = response.GetResponseStream(); | |
| StreamReader reader = new StreamReader(stream); | |
| string content = reader.ReadToEnd(); | |
| return content; | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment