Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

qwesdfxcv

  • Loading branch information...
commit c8bc3f6df553f01ffea508d923f75e020f785b87 1 parent 48fc4ac
@DFectuoso authored
View
354 foo
@@ -0,0 +1,354 @@
+0
+Twitter for iPhone
+2012-08-24 00:34:37
+1
+Twitter for iPhone
+2012-08-24 00:24:01
+2
+Twitter for iPhone
+2012-08-24 00:19:07
+3
+Twitter for iPhone
+2012-08-24 00:17:32
+4
+Twitter for iPhone
+2012-08-23 17:28:04
+5
+Twitter for iPhone
+2012-08-22 19:45:29
+6
+Twitter for iPhone
+2012-08-22 17:24:14
+7
+Twitter for iPhone
+2012-08-18 04:05:37
+8
+Twitter for iPhone
+2012-08-17 19:52:17
+9
+Twitter for iPhone
+2012-08-17 19:48:59
+10
+Twitter for iPhone
+2012-08-07 21:10:19
+11
+Twitter for iPhone
+2012-08-07 20:54:03
+12
+Twitter for iPhone
+2012-08-07 20:49:32
+13
+Twitter for iPhone
+2012-08-07 20:43:24
+14
+Twitter for iPhone
+2012-08-07 20:39:05
+15
+Twitter for iPhone
+2012-08-07 20:36:32
+16
+Twitter for iPhone
+2012-08-07 20:32:56
+17
+Twitter for iPhone
+2012-08-07 20:25:27
+18
+Twitter for iPhone
+2012-08-07 20:21:52
+19
+Twitter for iPhone
+2012-08-04 16:50:11
+20
+Twitter for iPhone
+2012-07-31 21:41:35
+21
+Twitter for iPhone
+2012-07-31 21:35:04
+22
+Twitter for iPhone
+2012-07-31 21:28:46
+23
+Twitter for iPhone
+2012-07-31 21:27:17
+24
+Twitter for iPhone
+2012-07-31 21:23:44
+25
+Twitter for iPhone
+2012-07-31 21:18:23
+26
+Twitter for iPhone
+2012-07-31 21:12:32
+27
+Twitter for iPhone
+2012-07-31 21:10:36
+28
+Twitter for iPhone
+2012-07-31 19:07:05
+29
+Twitter for iPhone
+2012-07-31 19:04:22
+30
+Twitter for iPhone
+2012-07-31 19:03:20
+31
+Twitter for iPhone
+2012-07-31 15:58:50
+32
+Twitter for iPhone
+2012-07-31 15:57:43
+33
+Twitter for iPhone
+2012-07-31 15:57:20
+34
+Twitter for iPhone
+2012-07-31 15:56:01
+35
+Twitter for iPhone
+2012-07-31 15:55:34
+36
+Twitter for iPhone
+2012-07-31 15:53:55
+37
+Twitter for iPhone
+2012-07-31 15:51:57
+38
+Twitter for iPhone
+2012-07-31 15:51:27
+39
+Twitter for iPhone
+2012-07-31 15:50:02
+40
+Twitter for iPhone
+2012-07-31 15:49:05
+41
+Twitter for iPhone
+2012-07-31 15:46:08
+42
+Twitter for iPhone
+2012-07-31 15:40:46
+43
+Twitter for iPhone
+2012-07-31 06:09:24
+44
+Twitter for iPad
+2012-07-31 01:47:53
+45
+Twitter for iPad
+2012-07-31 01:45:38
+46
+Twitter for iPad
+2012-07-31 01:41:13
+47
+Twitter for iPhone
+2012-07-30 03:25:17
+48
+Twitter for iPhone
+2012-07-28 03:01:56
+49
+Twitter for iPhone
+2012-07-28 03:00:45
+50
+Twitter for iPhone
+2012-07-27 19:56:27
+51
+Twitter for iPhone
+2012-07-26 20:54:53
+52
+Twitter for iPhone
+2012-07-24 14:48:38
+53
+Twitter for iPad
+2012-07-23 17:58:38
+54
+Twitter for iPhone
+2012-07-15 15:54:53
+55
+Twitter for iPhone
+2012-07-13 15:07:28
+56
+Twitter for iPhone
+2012-07-11 18:30:38
+57
+Twitter for iPhone
+2012-07-04 03:38:48
+58
+Twitter for iPhone
+2012-06-23 17:32:35
+59
+Twitter for iPhone
+2012-08-24 00:34:37
+60
+Twitter for iPhone
+2012-08-24 00:24:01
+61
+Twitter for iPhone
+2012-08-24 00:19:07
+62
+Twitter for iPhone
+2012-08-24 00:17:32
+63
+Twitter for iPhone
+2012-08-23 17:28:04
+64
+Twitter for iPhone
+2012-08-22 19:45:29
+65
+Twitter for iPhone
+2012-08-22 17:24:14
+66
+Twitter for iPhone
+2012-08-18 04:05:37
+67
+Twitter for iPhone
+2012-08-17 19:52:17
+68
+Twitter for iPhone
+2012-08-17 19:48:59
+69
+Twitter for iPhone
+2012-08-07 21:10:19
+70
+Twitter for iPhone
+2012-08-07 20:54:03
+71
+Twitter for iPhone
+2012-08-07 20:49:32
+72
+Twitter for iPhone
+2012-08-07 20:43:24
+73
+Twitter for iPhone
+2012-08-07 20:39:05
+74
+Twitter for iPhone
+2012-08-07 20:36:32
+75
+Twitter for iPhone
+2012-08-07 20:32:56
+76
+Twitter for iPhone
+2012-08-07 20:25:27
+77
+Twitter for iPhone
+2012-08-07 20:21:52
+78
+Twitter for iPhone
+2012-08-04 16:50:11
+79
+Twitter for iPhone
+2012-07-31 21:41:35
+80
+Twitter for iPhone
+2012-07-31 21:35:04
+81
+Twitter for iPhone
+2012-07-31 21:28:46
+82
+Twitter for iPhone
+2012-07-31 21:27:17
+83
+Twitter for iPhone
+2012-07-31 21:23:44
+84
+Twitter for iPhone
+2012-07-31 21:18:23
+85
+Twitter for iPhone
+2012-07-31 21:12:32
+86
+Twitter for iPhone
+2012-07-31 21:10:36
+87
+Twitter for iPhone
+2012-07-31 19:07:05
+88
+Twitter for iPhone
+2012-07-31 19:04:22
+89
+Twitter for iPhone
+2012-07-31 19:03:20
+90
+Twitter for iPhone
+2012-07-31 15:58:50
+91
+Twitter for iPhone
+2012-07-31 15:57:43
+92
+Twitter for iPhone
+2012-07-31 15:57:20
+93
+Twitter for iPhone
+2012-07-31 15:56:01
+94
+Twitter for iPhone
+2012-07-31 15:55:34
+95
+Twitter for iPhone
+2012-07-31 15:53:55
+96
+Twitter for iPhone
+2012-07-31 15:51:57
+97
+Twitter for iPhone
+2012-07-31 15:51:27
+98
+Twitter for iPhone
+2012-07-31 15:50:02
+99
+Twitter for iPhone
+2012-07-31 15:49:05
+100
+Twitter for iPhone
+2012-07-31 15:46:08
+101
+Twitter for iPhone
+2012-07-31 15:40:46
+102
+Twitter for iPhone
+2012-07-31 06:09:24
+103
+Twitter for iPad
+2012-07-31 01:47:53
+104
+Twitter for iPad
+2012-07-31 01:45:38
+105
+Twitter for iPad
+2012-07-31 01:41:13
+106
+Twitter for iPhone
+2012-07-30 03:25:17
+107
+Twitter for iPhone
+2012-07-28 03:01:56
+108
+Twitter for iPhone
+2012-07-28 03:00:45
+109
+Twitter for iPhone
+2012-07-27 19:56:27
+110
+Twitter for iPhone
+2012-07-26 20:54:53
+111
+Twitter for iPhone
+2012-07-24 14:48:38
+112
+Twitter for iPad
+2012-07-23 17:58:38
+113
+Twitter for iPhone
+2012-07-15 15:54:53
+114
+Twitter for iPhone
+2012-07-13 15:07:28
+115
+Twitter for iPhone
+2012-07-11 18:30:38
+116
+Twitter for iPhone
+2012-07-04 03:38:48
+117
+Twitter for iPhone
+2012-06-23 17:32:35
View
BIN  libs/.DS_Store
Binary file not shown
View
0  libs/__init__.py
No changes.
View
BIN  libs/__init__.pyc
Binary file not shown
View
225 libs/bitly.py
@@ -0,0 +1,225 @@
+#!/usr/bin/python2.4
+#
+# Copyright 2009 Empeeric LTD. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import urllib,urllib2
+import urlparse
+import string
+from django.utils import simplejson
+
+BITLY_BASE_URL = "http://api.bit.ly/"
+BITLY_API_VERSION = "2.0.1"
+
+VERBS_PARAM = {
+ 'shorten':'longUrl',
+ 'expand':'shortUrl',
+ 'info':'shortUrl',
+ 'stats':'shortUrl',
+ 'errors':'',
+}
+
+class BitlyError(Exception):
+ '''Base class for bitly errors'''
+
+ @property
+ def message(self):
+ '''Returns the first argument used to construct this error.'''
+ return self.args[0]
+
+class Api(object):
+ """ API class for bit.ly """
+ def __init__(self, login, apikey):
+ self.login = login
+ self.apikey = apikey
+ self._urllib = urllib2
+
+ def shorten(self,longURLs,params={}):
+ """
+ Takes either:
+ A long URL string and returns shortened URL string
+ Or a list of long URL strings and returns a list of shortened URL strings.
+ """
+ want_result_list = True
+ if not isinstance(longURLs, list):
+ longURLs = [longURLs]
+ want_result_list = False
+
+ for index,url in enumerate(longURLs):
+ if not '://' in url:
+ longURLs[index] = "http://" + url
+
+ request = self._getURL("shorten",longURLs,params)
+ result = self._fetchUrl(request)
+ json = simplejson.loads(result)
+ self._CheckForError(json)
+
+ results = json['results']
+ res = [self._extract_short_url(results[url]) for url in longURLs]
+
+ if want_result_list:
+ return res
+ else:
+ return res[0]
+
+ def _extract_short_url(self,item):
+ if item['shortKeywordUrl'] == "":
+ return item['shortUrl']
+ else:
+ return item['shortKeywordUrl']
+
+ def expand(self,shortURL,params={}):
+ """ Given a bit.ly url or hash, return long source url """
+ request = self._getURL("expand",shortURL,params)
+ result = self._fetchUrl(request)
+ json = simplejson.loads(result)
+ self._CheckForError(json)
+ return json['results'][string.split(shortURL, '/')[-1]]['longUrl']
+
+ def info(self,shortURL,params={}):
+ """
+ Given a bit.ly url or hash,
+ return information about that page,
+ such as the long source url
+ """
+ request = self._getURL("info",shortURL,params)
+ result = self._fetchUrl(request)
+ json = simplejson.loads(result)
+ self._CheckForError(json)
+ return json['results'][string.split(shortURL, '/')[-1]]
+
+ def stats(self,shortURL,params={}):
+ """ Given a bit.ly url or hash, return traffic and referrer data. """
+ request = self._getURL("stats",shortURL,params)
+ result = self._fetchUrl(request)
+ json = simplejson.loads(result)
+ self._CheckForError(json)
+ return Stats.NewFromJsonDict(json['results'])
+
+ def errors(self,params={}):
+ """ Get a list of bit.ly API error codes. """
+ request = self._getURL("errors","",params)
+ result = self._fetchUrl(request)
+ json = simplejson.loads(result)
+ self._CheckForError(json)
+ return json['results']
+
+ def setUrllib(self, urllib):
+ '''Override the default urllib implementation.
+
+ Args:
+ urllib: an instance that supports the same API as the urllib2 module
+ '''
+ self._urllib = urllib
+
+ def _getURL(self,verb,paramVal,more_params={}):
+ if not isinstance(paramVal, list):
+ paramVal = [paramVal]
+
+ params = {
+ 'version':BITLY_API_VERSION,
+ 'format':'json',
+ 'login':self.login,
+ 'apiKey':self.apikey,
+ }
+
+ params.update(more_params)
+ params = params.items()
+
+ verbParam = VERBS_PARAM[verb]
+ if verbParam:
+ for val in paramVal:
+ params.append(( verbParam,val ))
+
+ encoded_params = urllib.urlencode(params)
+ return "%s%s?%s" % (BITLY_BASE_URL,verb,encoded_params)
+
+ def _fetchUrl(self,url):
+ '''Fetch a URL
+
+ Args:
+ url: The URL to retrieve
+
+ Returns:
+ A string containing the body of the response.
+ '''
+
+ # Open and return the URL
+ url_data = self._urllib.urlopen(url).read()
+ return url_data
+
+ def _CheckForError(self, data):
+ """Raises a BitlyError if bitly returns an error message.
+
+ Args:
+ data: A python dict created from the bitly json response
+ Raises:
+ BitlyError wrapping the bitly error message if one exists.
+ """
+ # bitly errors are relatively unlikely, so it is faster
+ # to check first, rather than try and catch the exception
+ if 'ERROR' in data or data['statusCode'] == 'ERROR':
+ raise BitlyError, data['errorMessage']
+ for key in data['results']:
+ if type(data['results']) is dict and type(data['results'][key]) is dict:
+ if 'statusCode' in data['results'][key] and data['results'][key]['statusCode'] == 'ERROR':
+ raise BitlyError, data['results'][key]['errorMessage']
+
+class Stats(object):
+ '''A class representing the Statistics returned by the bitly api.
+
+ The Stats structure exposes the following properties:
+ status.user_clicks # read only
+ status.clicks # read only
+ '''
+
+ def __init__(self,user_clicks=None,total_clicks=None):
+ self.user_clicks = user_clicks
+ self.total_clicks = total_clicks
+
+ @staticmethod
+ def NewFromJsonDict(data):
+ '''Create a new instance based on a JSON dict.
+
+ Args:
+ data: A JSON dict, as converted from the JSON in the bitly API
+ Returns:
+ A bitly.Stats instance
+ '''
+ return Stats(user_clicks=data.get('userClicks', None),
+ total_clicks=data.get('clicks', None))
+
+
+if __name__ == '__main__':
+ testURL1="www.yahoo.com"
+ testURL2="www.cnn.com"
+ a=Api(login="pythonbitly",apikey="R_06871db6b7fd31a4242709acaf1b6648")
+ short=a.shorten(testURL1)
+ print "Short URL = %s" % short
+ short=a.shorten(testURL1,{'history':1})
+ print "Short URL with history = %s" % short
+ urlList=[testURL1,testURL2]
+ shortList=a.shorten(urlList)
+ print "Short URL list = %s" % shortList
+ long=a.expand(short)
+ print "Expanded URL = %s" % long
+ info=a.info(short)
+ print "Info: %s" % info
+ stats=a.stats(short)
+ print "User clicks %s, total clicks: %s" % (stats.user_clicks,stats.total_clicks)
+ errors=a.errors()
+ print "Errors: %s" % errors
+ testURL3=["www.google.com"]
+ short=a.shorten(testURL3)
+ print "Short url in list = %s" % short
View
BIN  libs/bitly.pyc
Binary file not shown
View
9 libs/bitly_api/__init__.py
@@ -0,0 +1,9 @@
+from bitly_api import Connection, BitlyError, Error
+__version__ = '0.1.b'
+__author__ = "Jehiah Czebotar <jehiah@gmail.com>"
+__all__ = ["Connection", "BitlyError", "Error"]
+__doc__ = """
+This is a python library for the bit.ly api
+
+all methods raise BitlyError on an unexpected response, or a problem with input format
+"""
View
262 libs/bitly_api/bitly_api.py
@@ -0,0 +1,262 @@
+import bitly_http
+import hashlib
+try:
+ import json
+except ImportError:
+ import simplejson as json
+import sys
+import time
+import types
+import urllib
+
+class Error(Exception):
+ pass
+
+class BitlyError(Error):
+ def __init__(self, code, message):
+ Error.__init__(self,message)
+ self.code = code
+
+
+class Connection(object):
+ """
+ This is a python library for accessing the bit.ly api
+ http://github.com/bitly/bitly-api-python
+
+ Usage:
+ import bitly_api
+ c = bitly_api.Connection('bitlyapidemo','R_{{apikey}}')
+ c.shorten('http://www.google.com/')
+ """
+ def __init__(self, login, api_key, secret=None, preferred_domain='bit.ly'):
+ self.host = 'api.bit.ly'
+ self.preferred_domain = preferred_domain # bit.ly or j.mp
+ self.login = login
+ self.api_key = api_key
+ self.secret = secret
+ (major, minor, micro, releaselevel, serial) = sys.version_info
+ self.user_agent = "Python/%d.%d.%d bitly_api/%s" % (major, minor, micro, '?')
+
+ def keyword(self, hash, keyword):
+ """ assign a keyword to a hash """
+ data = self._call(self.host, 'v3/keyword', {
+ 'login':self.login,
+ 'apiKey':self.api_key,
+ 'hash':hash,
+ 'keyword':keyword
+ }, self.secret)
+ return data['data']
+
+ def shorten(self, uri, x_login=None, x_apiKey=None, preferred_domain=None):
+ """ creates a bit.ly link for a given long url
+ @parameter uri: long url to shorten
+ @parameter x_login: login of a user to shorten on behalf of
+ @parameter x_apiKey: apiKey of a user to shorten on behalf of
+ @parameter preferred_domain: bit.ly[default] or j.mp
+ """
+ preferred_domain = preferred_domain or self.preferred_domain
+ params = {
+ 'login': self.login,
+ 'apiKey': self.api_key,
+ 'uri':uri.encode('UTF-8')
+ }
+ if preferred_domain:
+ params['domain'] = preferred_domain
+ if x_login:
+ params.update({
+ 'x_login':x_login,
+ 'x_apiKey':x_apiKey})
+ data = self._call(self.host, 'v3/shorten', params, self.secret)
+ return data['data']
+
+ def expand(self, hash=None, shortUrl=None):
+ """ given a bit.ly url or hash, decode it and return the target url
+ @parameter hash: one or more bit.ly hashes
+ @parameter shortUrl: one or more bit.ly short urls
+ """
+ if not hash and not shortUrl:
+ raise BitlyError(500, 'MISSING_ARG_SHORTURL')
+ params = {
+ 'login' : self.login,
+ 'apiKey' : self.api_key
+ }
+ if hash:
+ params['hash'] = hash
+ if shortUrl:
+ params['shortUrl'] = shortUrl
+
+ data = self._call(self.host, 'v3/expand', params, self.secret)
+ return data['data']['expand']
+
+ def clicks(self, hash=None, shortUrl=None):
+ """ given a bit.ly url or hash, get statistics about the clicks on that link """
+ if not hash and not shortUrl:
+ raise BitlyError(500, 'MISSING_ARG_SHORTURL')
+ params = {
+ 'login' : self.login,
+ 'apiKey' : self.api_key
+ }
+ if hash:
+ params['hash'] = hash
+ if shortUrl:
+ params['shortUrl'] = shortUrl
+
+ data = self._call(self.host, 'v3/clicks', params, self.secret)
+ return data['data']['clicks']
+
+ def referrers(self, hash=None, shortUrl=None):
+ """ given a bit.ly url or hash, get statistics about the referrers of that link """
+ if not hash and not shortUrl:
+ raise BitlyError(500, 'MISSING_ARG_SHORTURL')
+ params = {
+ 'login' : self.login,
+ 'apiKey' : self.api_key
+ }
+ if hash:
+ params['hash'] = hash
+ if shortUrl:
+ params['shortUrl'] = shortUrl
+
+ data = self._call(self.host, 'v3/referrers', params, self.secret)
+ return data['data']['referrers']
+
+ def clicks_by_day(self, hash=None, shortUrl=None):
+ """ given a bit.ly url or hash, get a time series of clicks
+ per day for the last 30 days in reverse chronological order
+ (most recent to least recent) """
+ if not hash and not shortUrl:
+ raise BitlyError(500, 'MISSING_ARG_SHORTURL')
+ params = {
+ 'login' : self.login,
+ 'apiKey' : self.api_key
+ }
+ if hash:
+ params['hash'] = hash
+ if shortUrl:
+ params['shortUrl'] = shortUrl
+
+ data = self._call(self.host, 'v3/clicks_by_day', params, self.secret)
+ return data['data']['clicks_by_day']
+
+ def clicks_by_minute(self, hash=None, shortUrl=None):
+ """ given a bit.ly url or hash, get a time series of clicks
+ per minute for the last 30 minutes in reverse chronological
+ order (most recent to least recent)"""
+ if not hash and not shortUrl:
+ raise BitlyError(500, 'MISSING_ARG_SHORTURL')
+ params = {
+ 'login' : self.login,
+ 'apiKey' : self.api_key
+ }
+ if hash:
+ params['hash'] = hash
+ if shortUrl:
+ params['shortUrl'] = shortUrl
+
+ data = self._call(self.host, 'v3/clicks_by_minute', params, self.secret)
+ return data['data']['clicks_by_minute']
+
+ def info(self, hash=None, shortUrl=None):
+ """ return the page title for a given bit.ly link """
+ if not hash and not shortUrl:
+ raise BitlyError(500, 'MISSING_ARG_SHORTURL')
+ params = {
+ 'login' : self.login,
+ 'apiKey' : self.api_key
+ }
+ if hash:
+ params['hash'] = hash
+ if shortUrl:
+ params['shortUrl'] = shortUrl
+
+ data = self._call(self.host, 'v3/info', params, self.secret)
+ return data['data']['info']
+
+ def lookup(self, url):
+ """ query for a bit.ly link based on a long url """
+ params = {
+ 'url' : url,
+ 'login' : self.login,
+ 'apiKey' : self.api_key
+ }
+
+ data = self._call(self.host, 'v3/lookup', params, self.secret)
+ return data['data']['lookup']
+
+ def pro_domain(self, domain, format='json'):
+ """ is the domain assigned for bitly.pro? """
+ end_point = 'v3/bitly_pro_domain'
+
+ if not domain:
+ raise BitlyError(500, 'MISSING_ARG_DOMAIN')
+
+ protocol_prefix = ('http://', 'https://')
+ if domain.lower().startswith(protocol_prefix):
+ raise BitlyError(500, 'INVALID_BARE_DOMAIN')
+ params = {
+ 'domain': domain,
+ 'login' : self.login,
+ 'apiKey' : self.api_key,
+ 'format': format,
+ }
+ data = self._call(self.host, end_point, params, self.secret)
+ return data['data']['bitly_pro_domain']
+
+ @classmethod
+ def _generateSignature(self, params, secret):
+ if not params or not secret:
+ return ""
+ hash_string = ""
+ if not params.get('t'):
+ # note, this uses a utc timestamp not a local timestamp
+ params['t'] = str(int(time.mktime(time.gmtime())))
+
+ keys = params.keys()
+ keys.sort()
+ for k in keys:
+ if type(params[k]) in [types.ListType, types.TupleType]:
+ for v in params[k]:
+ hash_string += v
+ else:
+ hash_string += params[k]
+ hash_string += secret
+ signature = hashlib.md5(hash_string).hexdigest()[:10]
+ return signature
+
+ def _call(self, host, method, params, secret=None, timeout=2500):
+ # default to json
+ params['format']=params.get('format','json')
+
+ if secret:
+ params['signature'] = self._generateSignature(params, secret)
+
+ # force to utf8 to fix ascii codec errors
+ encoded_params = []
+ for k,v in params.items():
+ if type(v) in [types.ListType, types.TupleType]:
+ v = [e.encode('UTF8') for e in v]
+ else:
+ v = v.encode('UTF8')
+ encoded_params.append((k,v))
+ params = dict(encoded_params)
+
+ request = "http://%(host)s/%(method)s?%(params)s" % {
+ 'host':host,
+ 'method':method,
+ 'params':urllib.urlencode(params, doseq=1)
+ }
+ try:
+ http_response = bitly_http.get(request, timeout, user_agent = self.user_agent)
+ if http_response['http_status_code'] != 200:
+ raise BitlyError(500, http_response['result'])
+ if not http_response['result'].startswith('{'):
+ raise BitlyError(500, http_response['result'])
+ data = json.loads(http_response['result'])
+ if data.get('status_code', 500) != 200:
+ raise BitlyError(data.get('status_code', 500), data.get('status_txt', 'UNKNOWN_ERROR'))
+ return data
+ except BitlyError:
+ raise
+ except:
+ raise BitlyError(None, sys.exc_info()[1])
+
View
BIN  libs/bitly_api/bitly_api.pyc
Binary file not shown
View
73 libs/bitly_api/bitly_http.py
@@ -0,0 +1,73 @@
+"""
+This class is an abstracted http handler that uses multiple underlying http libraries
+
+it will default to
+a) google urlfetch
+b) pycurl
+c) urllib2
+
+"""
+try:
+ import pycurl
+ PYCURL = True
+except ImportError:
+ PYCURL = False
+
+import urllib2
+import cStringIO
+
+class DontRedirect(urllib2.HTTPRedirectHandler):
+ def redirect_request(self, req, fp, code, msg, headers, newurl):
+ if code in (301, 302, 303, 307):
+ raise urllib2.HTTPError(req.get_full_url(), code, msg, headers, fp)
+
+def makeUrllib2Http(url, user_agent):
+ dont_redirect = DontRedirect()
+ opener = urllib2.build_opener(dont_redirect)
+ opener.addheaders = [('User-agent', user_agent + ' urllib')]
+
+ try:
+ response = opener.open(url)
+ code = response.code
+ data = response.read()
+ except urllib2.URLError, e:
+ return 500, str(e)
+ except urllib2.HTTPError, e:
+ code = e.code
+ data = e.read()
+ return code, data
+
+def makePycurlHttp(url, timeout, user_agent):
+ try:
+ buffer = cStringIO.StringIO()
+
+ curl = pycurl.Curl()
+ curl.setopt(pycurl.TIMEOUT_MS, timeout)
+ curl.setopt(pycurl.URL, url)
+ curl.setopt(pycurl.FOLLOWLOCATION, 0)
+ curl.setopt(pycurl.MAXREDIRS, 0)
+ curl.setopt(pycurl.WRITEFUNCTION, buffer.write)
+ curl.setopt(pycurl.NOSIGNAL, 1)
+ curl.setopt(pycurl.USERAGENT, user_agent + ' ' + pycurl.version)
+ # if referer:
+ # curl.setopt(pycurl.REFERER, referer)
+
+ curl.perform()
+ result = buffer.getvalue()
+ buffer.close()
+ http_status_code = curl.getinfo(pycurl.HTTP_CODE)
+ curl.close()
+ except:
+ try:
+ curl.close()
+ except:
+ pass
+ raise
+ return http_status_code, result
+
+def get(url, timeout, user_agent):
+ if PYCURL:
+ code, result = makePycurlHttp(url, timeout, user_agent)
+ else:
+ code, result = makeUrllib2Http(url, user_agent)
+ return {'http_status_code':code, 'result':result}
View
BIN  libs/bitly_api/bitly_http.pyc
Binary file not shown
View
BIN  libs/tweepy.zip
Binary file not shown
View
118 task_python.py
@@ -0,0 +1,118 @@
+from datetime import datetime
+import sys
+
+import urllib2
+
+#import tweepy
+sys.path.insert(0, 'libs/tweepy.zip')
+from tweepy import *
+
+BASE_URL = "0.0.0.0"
+
+def get_messages_from_user(user, page=0):
+ api = API()
+ messages = api.user_timeline(user_id=user,count=200, page=page)
+ print "Fetching page:" + str(page)
+
+ ##### THis forces one page only
+ return messages
+
+ if len(messages) > 0:
+ return messages + get_messages_from_user(user,page+1)
+ else:
+ return messages
+
+def get_id_to_crawl():
+ #### dfect
+ return "1981171"
+
+ #### rockstartup1
+ return "608629166"
+
+ response = urllib2.urlopen('http://' + BASE_URL + '/get-number')
+ return reponse
+
+def send_id(user_id,hit_30,hit_15,hit_10,hit_5,hit_3,hit_1,hit_i_30,hit_i_15,hit_i_10,hit_i_5,hit_i_3,hit_i_1):
+ response = urllib2.urlopen('http://' + BASE_URL + '/set-info?user='+user_id+"&hit_30="+hit_30+"&hit_15="+ hit_15+"&hit_10="+ hit_10 +"&hit_5="+ hit_5 +"&hit_3="+ hit_3 +"&hit_1="+ hit_1 +"&hit_i_30="+ hit_i_30 +"&hit_i_15="+ hit_i_15 +"&hit_i_10="+i hit_i_10 +"&hit_i_5="+ hit_i_5 +"&hit_i_3="+ hit_i_3 +"&hit_i_1=" + hit_i_1)
+ return
+
+def check_for_hit(time_delta_minutes, target_message, messages):
+ for message in messages:
+ ## Message is now the user message that if is within the time range and with the client, is a hit
+ if target_message[0] == message.source:
+ message_time = datetime.strptime(message.created_at,"%Y-%m-%d %H:%M:%S")
+ timedelta = abs((target_message[1] - message_time).total_seconds())
+ hit_timedelta = time_delta_minutes * 60
+ if timedelta < hit_timedelta:
+ return True
+ return False
+
+# print message.source
+# print message.created_at
+
+# Wake up, read rockstartuo file
+file = open( "foo", "r" )
+rockstartup = []
+while 1:
+ index = file.readline()
+ if not index:
+ break
+ client = file.readline()
+ message_time = datetime.strptime(file.readline(), "%Y-%m-%d %H:%M:%S")
+ rockstartup.append([client,message_time])
+
+while 1:
+ hit_30 = 0
+ hit_15 = 0
+ hit_10 = 0
+ hit_5 = 0
+ hit_3 = 0
+ hit_1 = 0
+ hit_i_30 = 0
+ hit_i_15 = 0
+ hit_i_10 = 0
+ hit_i_5 = 0
+ hit_i_3 = 0
+ hit_i_1 = 0
+
+ # Ask for id
+ id_to_crawl = get_id_to_crawl()
+ if id_to_crawl == "FAIL":
+ break
+ # Get all messages
+ messages = get_messages_from_user(id_to_crawl)
+
+ for rockstartup_message in rockstarup:
+ ### For each message, we check if we have any message that is <5,5>,<3,3>,<1,1>
+ if check_for_hit(30,rockstartup_message,messages):
+ hit_30 = hit_30 + 1
+ if rockstartup_message == "Twitter for iPad":
+ hit_i_30 = hit_i_30 + 1
+
+ if check_for_hit(15,rockstartup_message,messages):
+ hit_15 = hit_15 + 1
+ if rockstartup_message == "Twitter for iPad":
+ hit_i_15 = hit_i_15 + 1
+
+ if check_for_hit(10,rockstartup_message,messages):
+ hit_10 = hit_10 + 1
+ if rockstartup_message == "Twitter for iPad":
+ hit_i_10 = hit_i_10 + 1
+
+ if check_for_hit(5,rockstartup_message,messages):
+ hit_5 = hit_5 + 1
+ if rockstartup_message == "Twitter for iPad":
+ hit_i_5 = hit_i_5 + 1
+
+ if check_for_hit(3,rockstartup_message,messages):
+ hit_3 = hit_3 + 1
+ if rockstartup_message == "Twitter for iPad":
+ hit_i_3 = hit_i_3 + 1
+
+ if check_for_hit(1,rockstartup_message,messages):
+ hit_1 = hit_1 + 1
+ if rockstartup_message == "Twitter for iPad":
+ hit_i_1 = hit_i_1 + 1
+
+ send_id(id_to_crawl,hit_30,hit_15,hit_10,hit_5,hit_3,hit_1,hit_i_30,hit_i_15,hit_i_10,hit_i_5,hit_i_3,hit_i_1)
+ break
Please sign in to comment.
Something went wrong with that request. Please try again.