Skip to content

Instantly share code, notes, and snippets.

View anderser's full-sized avatar

Anders Eriksen anderser

View GitHub Profile
@anderser
anderser / urls.py
Created January 24, 2012 10:28
Using cache decorator in urls.py to keep the app DRY
from django.conf.urls.defaults import *
from timelineit.views import *
from django.views.decorators.cache import cache_control,never_cache
urlpatterns = patterns('timelineit.views',
url(r'^desk/(?P<slug>[-\w\d]+)',
never_cache(timeline_preview),
name="timeline_preview"),
@anderser
anderser / api.py
Created March 26, 2012 14:31
Cached tastypie model resource
class CachedModelResource(ModelResource):
def create_response(self, *args, **kwargs):
resp = super(CachedModelResource, self).create_response(*args, **kwargs)
resp['Cache-Control'] = "max-age=600"
return resp
@anderser
anderser / grabber.py
Created May 22, 2012 18:13
Grab webpage from python/django with wkhtmltopdf
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import shlex, subprocess
import logging
from django.conf import settings
logger = logging.getLogger('pagetracker.grabber')
def grab_page(url, outfile):
@anderser
anderser / views.py
Created September 28, 2012 12:27
Presidentpoll - get data fra HuffPost president pollofpoll and pass to Django template
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
from django.http import HttpResponseRedirect, Http404, HttpResponse
from django.template import RequestContext
from django.core.cache import cache
import iso8601
from pollster import Pollster
def presidentmatch(request):
@anderser
anderser / articleanalytics.py
Created November 15, 2012 21:16
Get Google analytics metrics for single url/article
from local_settings import GOOGLE_USER, GOOGLE_PWD
from googleanalytics import Connection
from datetime import date
#googleanalytics is found here: https://github.com/clintecker/python-googleanalytics/
gaaccounts = {
'myaccountname': '1234567',
}
@anderser
anderser / app.py
Last active December 18, 2015 19:49
Retrieve video urls from Instagram API using instagram python client. Has to use the raw json response since the python client does not yet support video.
api = client.InstagramAPI(client_id=CONFIG['client_id'], client_secret=CONFIG['client_secret'])
instagen, pages = api.tag_recent_media(30, 0, "thetag", max_pages=1, return_json=True)
videos = []
for item in instagen:
if "videos" in item:
videos.append(
{'user': item['user']['username'],
'videourl': item['videos']['standard_resolution']
@anderser
anderser / streaks.py
Last active January 19, 2016 20:32
Agate compute method to generate streaks of consecutive values in a dataset. WIP
import agate
class Streaks(agate.Computation):
"""
Computes the streaks of consecutive values in a column.
Each streak will be given an increasing
integer value so that you can group by this later to
find longest consecutive streak.
"""
@anderser
anderser / mydag.py
Created April 11, 2016 19:08
Error in Airflow DAG
from __future__ import print_function
from builtins import range
from airflow.operators import PythonOperator, PostgresOperator, DummyOperator
from airflow.models import DAG
from datetime import datetime, timedelta
import time
from pprint import pprint
seven_days_ago = datetime.combine(
@anderser
anderser / pipeline.md
Created April 12, 2016 18:53
A sample pipeline/DAG for Airflow
  1. Fetch some json/XML file from an external API
  2. Store the file on S3 (as a backup) with a file name with a i.e. a timestamp. Pass the filename on to next task
  3. Read the json/xml from S3 and into some table structure (pandas, agate etc) and change field types etc.
  4. Store the table in a postgres database in a temp table
  5. Compare the temp table to a "main" table and see if there are changes (some SQL diff). Find out which records have to be added/removed/updated in the "main" table.
  6. If nothing has changed, abort everything. If it has, pass on which records are new, deleted and updated.
  7. a) insert new records in main table, alert newsroom on slack of new items.b) delete items in main table not in temp-table. Alert via slack. c) Update records in main table, alert via slack
  8. The end
@anderser
anderser / boatswithin.sql
Created January 6, 2017 08:57
Båter ved oppdrettslokasjon på distinkte datoer (innenfor 200m)
WITH ais AS
(SELECT *
FROM aisdata.bronnbaater_resampled_min_lowspeed),
lokaliteter AS
(SELECT *
FROM fishandfjord.fiskeridir_alle_lokaliteter
WHERE loknr = '11763')
SELECT truncdate,
loknr,
lokalitet,