Created
August 15, 2015 16:11
-
-
Save mikeobrien/fd15a184e048b5f07598 to your computer and use it in GitHub Desktop.
USPTO Search
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
public static class StringExtensions | |
{ | |
public static string StripHtml(this string value) | |
{ | |
return Regex.Replace(value, @"<[^>]+>| ", "").Trim(); | |
} | |
} |
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
public class Trademark | |
{ | |
public string SerialNumber { get; set; } | |
public string RegistrationNumber { get; set; } | |
public string WordMark { get; set; } | |
public string Status { get; set; } | |
public string Url { get; set; } | |
} | |
public interface ITrademarkSearch | |
{ | |
List<Trademark> Search(string query); | |
} | |
public class TrademarkSearch : ITrademarkSearch | |
{ | |
private const string ResultsRegex = | |
@"(<TR>\s*?" + | |
@"<TD>\d.*<\/TD>\s*?" + | |
@"<TD>\s*?<a.*?>(.*?)<\/a>\s*?<\/TD>\s*?" + | |
@"<TD>\s*?<a.*?>(.*?)<\/a>\s*?<\/TD>\s*?" + | |
@"<TD>\s*?<a.*?>(.*?)<\/a>\s*?<\/TD>\s*?" + | |
@"<TD>\s*?<a HREF=""(.*?)"">.*?<\/a>\s*?<\/TD>\s*?" + | |
@"<TD>\s*?<a.*?>(.*?)<\/a>\s*?<\/TD>(.|\s)*?" + | |
@"<\/TR>).*"; | |
public List<Trademark> Search(string query) | |
{ | |
// TODO: Add caching | |
using (var client = new WebClient()) | |
{ | |
var response = Encoding.UTF8.GetString(client | |
.UploadData("http://tmsearch.uspto.gov/bin/gate.exe ", | |
Encoding.UTF8.GetBytes("f=toc&p_lang=english&p_d=trmk&p_L=500" + | |
$"&p_plural=yes&p_s_ALL={query}&a_search=Submit Query"))); | |
var results = new Regex(ResultsRegex, RegexOptions.IgnoreCase | | |
RegexOptions.Multiline).Matches(response); | |
return results.Cast<Match>().Select(x => new Trademark | |
{ | |
SerialNumber = x.Groups[2].Value.StripHtml(), | |
RegistrationNumber = x.Groups[3].Value.StripHtml(), | |
WordMark = x.Groups[4].Value.StripHtml(), | |
Url = x.Groups[5].Value, | |
Status = x.Groups[6].Value.StripHtml() | |
}).ToList(); | |
} | |
} | |
} |
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
[TestFixture] | |
public class TrademarkSearchTests | |
{ | |
[Test] | |
public void Should_return_search_results() | |
{ | |
var results = new TrademarkSearch().Search("(fark)[COMB]"); | |
results.Count.ShouldBeGreaterThanOrEqualTo(8); | |
var result = results.FirstOrDefault(x => x.SerialNumber == "77326033"); | |
result.ShouldNotBeNull(); | |
result.RegistrationNumber.ShouldEqual("3501114"); | |
result.WordMark.ShouldEqual("IT'S NOT NEWS, IT'S FARK.COM"); | |
result.Status.ShouldEqual("LIVE"); | |
result.Url.ShouldEqual("http://tsdr.uspto.gov/#caseNumber=77326033" + | |
"&caseType=SERIAL_NO&searchType=statusSearch"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment