Skip to content

Instantly share code, notes, and snippets.

View vector623's full-sized avatar

David Gallmeier vector623

View GitHub Profile
require 'net/http'
require 'nokogiri'
#you need Nokogiri for this to work: gem install 'nokogiri'
#fetch html if not cached
html_cache_file = 'blackjack-chart.html'
if File.exist? html_cache_file
html = File.read(html_cache_file)
else
var page = require('webpage').create();
page.open('http://davidgallmeier.com', function() {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
var results = page.evaluate(function() {
var firstlinktext = $('a.post-link:first').
text().
toString();
var firstlinkhref = $('a.post-link').
map(function () {
return $( this ).attr('href');
@vector623
vector623 / AggregateUntilUsage.cs
Last active February 10, 2018 02:26
AggregateUntil() example
public static List<Label> GetLabels()
{
/*
* use linq's lazy loading feature to enumerate an unlimited number of page requests. so i created AggregateUntil() to
* take in a Func() which will inform when to stop iterating - in this case, we want to stop after the entries count
* exceeds the totalNumEntries value, which Google Adwords returns from the first query
*/
var labels = Enumerable
.Range(0, Int32.MaxValue) //range generates a list of integers, from 1 to practically infinity
.AggregateUntil(
@vector623
vector623 / PhantomRunner.cs
Created February 20, 2018 17:57 — forked from DotNetNerd/PhantomRunner.cs
Grabbing a site in C# using phantomJS
class Program
{
static void Main(string[] args)
{
var grabby = new Grabby();
string output = grabby.Grab("http://www.dotnetnerd.dk/cv");
Console.WriteLine(output);
File.WriteAllText("c:\\test.html", output);
}
@vector623
vector623 / RouteValueDictionaryUsage.cs
Created March 5, 2018 21:41
use RouteValueDictionary to de-anonymize dynamic types
using (var reportResponse = reportRequest.GetResponse())
using (var reportReader = new StreamReader(reportResponse.Stream))
using (var csv = new CsvReader(reportReader))
{
var records = csv
.GetRecords<dynamic>()
.Select(record =>
{
var recordDictionary = new RouteValueDictionary(record);
var newKeywordStat = new KeywordPerformanceStat() {
@vector623
vector623 / RetryWebRequests.cs
Last active March 6, 2018 13:53
retry web requests, with exponential backoff, using functional linq in c#
var labelsPage = Enumerable
.Range(0, 5)
.Select(pageAttempt =>
{
Thread.Sleep(2500 * pageAttempt * pageAttempt);
try
{
var labelsAwql = "select LabelId,LabelName,LabelStatus limit 0,100";
var attemptedlabelsPage = Config.LabelService.query(labelsAwql);
@vector623
vector623 / AggregateUntil.cs
Created March 7, 2018 20:19
A customized version of C#'s Aggregate() method, tailored for paged web API results
public static TAccumulate AggregateUntil<TSource, TAccumulate>(this IEnumerable<TSource> source,
TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate,bool> untilFunc)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (func == null)
{
@vector623
vector623 / AggregateUntilAdWordsExample.cs
Created March 7, 2018 20:55
example usage of AggregateUntil() with Google's AdWords API
private static List<CriterionBidLandscape> GetCriterionBidLandscapes(DataService dataService, string awql)
{
var bidLandscapes = Enumerable
.Range(0, Int32.MaxValue)
.AggregateUntil(
new CriterionBidLandscapePage()
{
entries = new CriterionBidLandscape[] { },
totalNumEntries = 0,
},
@vector623
vector623 / CsvHelperExample.cs
Created March 14, 2018 17:19
Example use of CsvHelper nuget package; also example usage of dynamic and RouteValueDictionary
public static void ImportPricingFile()
{
using (var streamReader = new StreamReader(@"C:\Users\d.gallmeier\gits\daytoday\20180308\pricingFile.csv"))
using (var csv = new CsvReader(streamReader))
using (var db = new SqlConnection(Config.AthenaConnectionString))
{
var pricingLines = csv
.GetRecords<dynamic>()
.Select(record =>
{
@vector623
vector623 / AssignmentStatementExample.cs
Last active March 19, 2018 14:35
Example of assignment statement
var apolloUpdateAdjustment = new Apollo.UpdateAdjustment();
apolloUpdateAdjustment.itemid = Convert.ToInt64(record["ItemID"].ToString());
apolloUpdateAdjustment.reason_for_update = record["Reason For Update"].ToString();
apolloUpdateAdjustment.release_date = record["Release Date"].ToString();
apolloUpdateAdjustment.prosales = record["ProSales"].ToString();
apolloUpdateAdjustment.supply = record["SUPPLY"].ToString();