Skip to content

Instantly share code, notes, and snippets.

View beratdogan's full-sized avatar

Berat Doğan beratdogan

View GitHub Profile
@beratdogan
beratdogan / milliyetnews.py
Last active August 29, 2015 14:23
parsing sabah.com.tr's rss
import requests
from urlparse import urlparse, urljoin
from lxml import html, etree
from lxml.cssselect import CSSSelector
def get_content(url):
return requests.get(url)
def get_selector(css_selector):
return CSSSelector(css_selector)
@beratdogan
beratdogan / admin.py
Created June 25, 2015 14:15
[Django] Changing field widget when when using inlines
from django import forms
from django.contrib import admin
from django.contrib.sites.models import Site
from django.contrib.sites.admin import SiteAdmin
from django.utils.translation import ugettext_lazy as _
from colorful.fields import RGBColorField # third-party app
from .models import SiteOption
@beratdogan
beratdogan / async.coffee
Created June 24, 2015 22:58
Detecting if an image is really an image
request = new XMLHttpRequest()
request.onreadystatechange = ->
if request.readyState is 4
mimetype = request.getResponseHeader('content-type')
if mimetype.split('/').shift() is 'image'
alert 'Image!'
else
alert 'Not Image!'
request.open 'HEAD', 'http://upload.wikimedia.org/wikipedia/commons/5/5b/Ultraviolet_image_of_the_Cygnus_Loop_Nebula_crop.jpg', true
@beratdogan
beratdogan / Dockerfile
Last active August 29, 2015 14:22
Sample Dockerfile for Python 2.7 + lxml
FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN apt-get update && apt-get -y install libxml2-dev libxslt1-dev libxslt-dev python-lxml
RUN pip install lxml
@beratdogan
beratdogan / model2.py
Last active August 29, 2015 14:15
[Django] Farkli App Icerisindeki Model'leri Ayni Grupta Gosterebilmek
# app_1.models.py
from django.db import models
class Model2(models.Model):
field = models.ForeignKey('app_1.Model3')
import requests
import lxml.html
from lxml.cssselect import CSSSelector
def get_filtered_nodes(content, selector):
dom_tree = lxml.html.fromstring(content)
selector = CSSSelector(selector)
return selector(dom_tree)
@beratdogan
beratdogan / populator.js
Last active August 29, 2015 14:06
hipchat room private populator
function makeid()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
@beratdogan
beratdogan / spf.js
Created June 29, 2014 23:08
Compiled and minified spf.js file. Can be found more details here: https://github.com/youtube/spfjs
/*
SPF
(c) 2012-2014 Google, Inc.
License: MIT
*/
(function(){function l(a,b,c){var d=Array.prototype.slice.call(arguments,2);return function(){var c=d.slice();c.push.apply(c,arguments);return a.apply(b,c)}}function aa(a,b){if(a){var c=Array.prototype.slice.call(arguments,1);try{return a.apply(null,c)}catch(d){return d}}}function m(a,b){if(document.createEvent){var c=document.createEvent("CustomEvent");c.initCustomEvent("spf"+a,!0,!0,b);return document.dispatchEvent(c)}return!0}function n(){return(new Date).getTime()};function p(a,b){return q[a]=b}var q=window._spf_state||{};window._spf_state=q;var ba={"animation-class":"spf-animate","animation-duration":425,"cache-lifetime":6E5,"cache-max":50,"cache-unified":!1,"link-class":"spf-link","nolink-class":"spf-nolink","navigate-limit":20,"navigate-lifetime":864E5,"prefetch-on-mousedown":!1,"process-async":!1,"request-timeout":0,"url-identifier":"?spf=__type__"};function r(a){return ca()[a]}function ca(a){return!a&&"config"in q?q.config:p("config",a||{})};function
@beratdogan
beratdogan / i18n.coffee
Created May 27, 2014 13:35
Google Chrome Extension Utils - with contributions of Limk.com
# You can easly internionalize your extension's HTMLs with this.
$('[data-i18n]').each ->
me = $(@)
key = me.data 'i18n'
switch me.attr 'data-i18n-method'
when 'placeholder'
me.attr 'placeholder', chrome.i18n.getMessage key
@beratdogan
beratdogan / string_htmlspecialchars.coffee
Last active August 29, 2015 14:01
Coffee type extends
String::htmlspecialchars = (quote_style = 2, double_encode) ->
optTemp = 0
i = 0
noquotes = false
string = @.toString()
string = string.replace(/&/g, '&amp;') if double_encode isnt false
string = string.replace(/</g, '&lt;').replace(/>/g, '&gt;')