Skip to content

Instantly share code, notes, and snippets.

View dangayle's full-sized avatar

Dan Gayle dangayle

View GitHub Profile
import datetime
import random
import Twython
from twisted.internet import task
from twisted.internet import reactor
TIMEOUT = datetime.timedelta(hours=1).seconds
twitter = Twython("YOUR API KEY",
"YOUR API SECRET",
@dangayle
dangayle / unique.py
Created October 29, 2014 03:56
unique list
from collections import OrderedDict
from itertools import repeat, izip
alist = ["a", "b", "c", "a"]
n = repeat(None)
print(list(OrderedDict(izip(alist,n))))
@dangayle
dangayle / switch.py
Last active August 29, 2015 14:10 — forked from Lucretiel/switch.py
from contextlib import contextmanager
class SwitchError(RuntimeError):
pass
@contextmanager
def switch(switch_value, *, ignore_nomatch=True):
blocks = {}
blocks.default = None

Share Counts

I have always struggled with getting all the various share buttons from Facebook, Twitter, Google Plus, Pinterest, etc to align correctly and to not look like a tacky explosion of buttons. Seeing a number of sites rolling their own share buttons with counts, for example The Next Web I decided to look into the various APIs on how to simply return the share count.

If you want to roll up all of these into a single jQuery plugin check out Sharrre

Many of these API calls and methods are undocumented, so anticipate that they will change in the future. Also, if you are planning on rolling these out across a site I would recommend creating a simple endpoint that periodically caches results from all of the APIs so that you are not overloading the services will requests.

Twitter

@dangayle
dangayle / golden_ratio.scss
Last active June 24, 2021 22:56
The only css grid you need, based on the golden ratio.
$grid-gutter-width: 30px;
.l-1{
// larger portion of golden ratio
width: calc(#{percentage(1/1.618)} - #{$grid-gutter-width / 2});
}
.l-2{
// Smaller portion of golden ratio
width: calc(#{percentage(1-1/1.618)} - #{$grid-gutter-width / 2});
}
def leaders(text, delimiter):
text = text.split(delimiter)
return "{0}{1:.>30}".format(*text)
def get_date_tree():
"""
Get all dates with stories, store in Redis. Cached for 1 day.
Returns an OrderedDict, with datetime objects as keys.
OrderedDict([(
datetime.date(1963, 1, 1), OrderedDict([(
datetime.date(1963, 11, 1), [
datetime.date(1963, 11, 22),
// XPath CheatSheet
// To test XPath in your Chrome Debugger: $x('/html/body')
// http://www.jittuu.com/2012/2/14/Testing-XPath-In-Chrome/
// 0. XPath Examples.
// More: http://xpath.alephzarro.com/content/cheatsheet.html
'//hr[@class="edge" and position()=1]' // every first hr of 'edge' class
@dangayle
dangayle / mixin.py
Created December 1, 2015 18:42 — forked from cyberdelia/mixin.py
Django class based view mixins
# -*- coding: utf-8 -*-
from django.contrib.auth.decorators import login_required
from django.utils.cache import patch_response_headers
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page, never_cache
from django.views.decorators.csrf import csrf_exempt
class NeverCacheMixin(object):
@method_decorator(never_cache)
@dangayle
dangayle / celery_janitor.py
Created December 10, 2015 21:46 — forked from jonhuber/celery_janitor.py
This is a script to clean up deadlocked Celery workers. I typically run it on servers every 15 minutes via cron.
from celery.app.control import Control
import datetime
import psutil
import socket
def main():
control = Control()
inspect = control.inspect()
hostname = socket.gethostname()