Skip to content

Commit 0dae4e2

Browse files
author
Ross Karchner
committed
Not 100% working yet, but I'm replacing httplib2 and poster with requests
1 parent 8591869 commit 0dae4e2

File tree

4 files changed

+29
-22
lines changed

4 files changed

+29
-22
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
socrata.cfg
3+
4+
Socrata.pyc

Socrata.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
"""
1616

1717
import json
18-
import re
19-
from poster.encode import multipart_encode
20-
from poster.streaminghttp import register_openers
21-
from httplib2 import Http
18+
import re, logging
19+
import requests
2220
from urllib import urlencode
23-
from urllib2 import Request, urlopen
21+
22+
23+
DEBUG=False
24+
2425

2526
class SocrataBase:
2627
"""Base class for all Socrata API objects"""
@@ -32,26 +33,25 @@ def __init__(self, configuration):
3233
"""
3334

3435
self.config = configuration
35-
self.username, password, host = (self.config.get('credentials', 'user'),
36+
self.username, self.password, self.host = (self.config.get('credentials', 'user'),
3637
self.config.get('credentials', 'password'),
3738
self.config.get('server', 'host'))
3839

3940
self.app_token = self.config.get('credentials', 'app_token')
40-
self.api = Http()
41-
self.url = host
41+
self.url = self.host
4242
self.id_pattern = re.compile('^[0-9a-z]{4}-[0-9a-z]{4}$')
43-
self.api.add_credentials(self.username, password)
44-
45-
# For multipart upload/streaming
46-
register_openers()
4743

48-
def _request(self, service, type, data = {}):
44+
def _request(self, service, type='GET', data = {}):
4945
"""Generic HTTP request, encoding data as JSON and decoding the response"""
50-
response, content = self.api.request(
51-
self.url + service, type,
52-
headers = { 'Content-type:': 'application/json',
46+
client= getattr(requests, type.lower())
47+
uri=self.url + service
48+
logging.warning(uri)
49+
response= client(uri + service,
50+
headers = { 'Content-type:': 'application/json',
5351
'X-App-Token': self.app_token },
54-
body = json.dumps(data) )
52+
data = json.dumps(data), auth=(self.username, self.password ))
53+
54+
content=response.text
5555
if content != None and len(content) > 0:
5656
response_parsed = json.loads(content)
5757
if hasattr(response_parsed, 'has_key') and \
@@ -167,8 +167,8 @@ def multipart_post(self, url, filename, field='file'):
167167
headers['X-App-Token'] = self.app_token
168168

169169
request = Request("%s%s" % (self.url, url), datagen, headers)
170-
response = urlopen(request).read()
171-
return json.loads(response)
170+
response = request.get(request).read()
171+
return json.loads(response.text)
172172

173173
# Is the string 'id' a valid four-four ID?
174174
def is_id(self, id):

rss_capturer.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
import feedparser
99
from xml.dom import minidom
1010

11-
REDDIT_RSS = "http://www.reddit.com/.rss"
11+
Socrata.DEBUG=True
1212

13+
REDDIT_RSS = "http://www.reddit.com/.rss"
1314

1415

1516
def create_dataset_with_columns(dataset, title = 'RSS Feed Dataset', description = ''):
@@ -41,8 +42,10 @@ def create_dataset_with_columns(dataset, title = 'RSS Feed Dataset', description
4142
dataset = Socrata.Dataset(cfg)
4243

4344
print "Searching for existing dataset"
45+
#import pdb;pdb.set_trace()
4446
existing = dataset.find_datasets({'q':'RSS Feed Dataset',
45-
'for_user': dataset.username})[0]
47+
'for_user': dataset.username})
48+
4649
if existing['count'] > 0:
4750
print "Dataset exists, using it"
4851
dataset.use_existing(existing['results'][0]['id'])

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
version = '0.2',
99
packages = find_packages(),
1010

11-
install_requires=['feedparser','httplib2','poster'],
11+
install_requires=['feedparser','requests','poster'],
1212

1313
author = 'Aiden Scandella',
1414
author_email = '[email protected]',

0 commit comments

Comments
 (0)