Skip to content

Instantly share code, notes, and snippets.

View h2rd's full-sized avatar

Igor Skrynkovskyy h2rd

  • Route4Me
  • Lviv, Ukraine
View GitHub Profile
<script type="text/javascript">
$(function(){
$('input#submit').click(function(){
var file = $('input[type="file"]').val();
var exts = ['doc','docx','rtf','odt'];
// first check if file field has any value
if ( file ) {
// split file name at dot
var get_ext = file.split('.');
// reverse name to check extension
import re
def fff(url):
query = re.search('\?(.*)', url)
return query and query.group() or '/'
def fff2(url):
query = re.search('(https?://)?\w/(.*)', url)
return "/%s" % (query and query.group(2) or "", )
class Post < ActiveRecord::Base
  STATUS = %w(draft published)

  def draft?
    status == 'draft'
  end

  def published?
 status == 'published'
#!/usr/bin/env python
# coding: utf8
from __future__ import print_function
import requests
URL = 'https://graph.facebook.com/?ids=%s';
def get_url_data(url):

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

@h2rd
h2rd / gist:9c38373161a39cc4077e
Created February 23, 2015 00:48
Dynamic Creation of Item Classes Scrapy
from scrapy.item import DictItem, Field
def create_item_class(class_name, field_list):
fields = {field_name: Field() for field_name in field_list}
return type(class_name, (DictItem,), {'fields': fields})
@h2rd
h2rd / Tokenable
Last active August 29, 2015 14:16
Ruby concerns example
# app/models/model_name.rb
class ModelName < ActiveRecord::Base
include Tokenable
end
# app/models/concerns/tokenable.rb
module Tokenable
extend ActiveSupport::Concern
included do
@h2rd
h2rd / parse.rb
Last active August 29, 2015 14:26
require 'json'
raise Exception, 'you must provide a json file' unless ARGV[0]
buffer = File.open(ARGV[0]).read
buffer.gsub!("\n", '')
buffer.gsub!("\t", '')
json = JSON.parse(buffer)
puts json.first.collect {|k,v| k}.join(',')
require 'json'
require 'csv'
raise Exception, 'you must provide a json file' unless ARGV[0]
buffer = File.open(ARGV[0]).read
buffer.gsub!("\n", '')
buffer.gsub!("\t", '')
json = JSON.parse!(buffer)
@h2rd
h2rd / tmux.md
Created October 14, 2015 23:08 — forked from andreyvit/tmux.md
tmux cheatsheet

tmux cheat sheet

(C-x means ctrl+x, M-x means alt+x)

Prefix key

The default prefix is C-b. If you (or your muscle memory) prefer C-a, you need to add this to ~/.tmux.conf:

remap prefix to Control + a