Commits

Ferran Pegueroles committed 459dd6c

Correct command line script

  • Participants
  • Parent commits 1e1b62b

Comments (0)

Files changed (6)

File peerreach/api.py

 class NoArgsException(ApiException):
     pass
 
+
 class TooMuchArgsException(ApiException):
     pass
 
+
 class HttpError(ApiException):
-   pass
+    pass
 
 
 class Api(object):
         self.api_multi_user_url = self.api_url + '/multi-user/lookup.json'
 
         if parser:
-           self.parser = parser
+            self.parser = parser
         else:
-           self.parser = JSONParser()
+            self.parser = JSONParser()
 
     def parse(self, data):
         return self.parser.parse(data)
 
-
     def get_data(self, url, **options):
 
         response = requests.get(url, **options)
 
-        if str(response.status)[0] != '2':
-           raise HttpException(response.status)
+        if str(response.status_code)[0] != '2':
+            raise HttpError(response.status)
 
-        return self.parse(response.data)
-
+        return self.parse(response.text)
 
     def user_lookup(self, user_id=None, screen_name=None):
         if not any((user_id, screen_name)):
-           raise NoArgsException()
+            raise NoArgsException()
         if all((user_id, screen_name)):
-           raise TooMuchArgsException()
+            raise TooMuchArgsException()
 
         if user_id:
-           params = {"user_id": user_id}
+            params = {"user_id": user_id}
         else:
-           params = {"screen_name": screen_name}
+            params = {"screen_name": screen_name}
 
         url = "%s?%s" % (self.api_user_url, urllib.urlencode(params))
 
 
     def multi_user_lookup(self, user_ids=None, screen_names=None):
         if not any((user_ids, screen_names)):
-           raise NoArgsException()
+            raise NoArgsException()
         if all((user_ids, screen_names)):
-           raise TooMuchArgsException()
+            raise TooMuchArgsException()
 
         if user_ids:
-           params = {"user_id": ",".join(map(str, user_ids))}
+            params = {"user_id": ",".join(map(str, user_ids))}
         else:
-           params = {"screen_name": ",".join(screen_names)}
+            params = {"screen_name": ",".join(screen_names)}
 
         url = "%s?%s" % (self.api_multi_user_url, urllib.urlencode(params))
 

File peerreach/parsers.py

 import json
 
+
 class RawParser(object):
     def parse(self, raw):
         return raw
 
+
 class JSONParser(object):
     def parse(self, raw):
         return json.loads(raw)
 
+
 class PeerreachObject(object):
     def __init__(self, raw):
         data = json.loads(raw)
         self.__dict__.update(data)
 
+
 class ObjectParser(object):
     def __init__(self, klass=PeerreachObject):
         self.klass = klass

File peerreach/peerreach.py

-
-
-# http://api.peerreach.com/v1/user/lookup.json?screen_name=fredwilson
-# http://api.peerreach.com/v1/user/lookup.json?user_id=fredwilson
-
-# http://api.peerreach.com/v1/multi-user/lookup.json?screen_name=PeerReach,Scobleizer
-# http://api.peerreach.com/v1/multi-user/lookup.json?user_id=PeerReach,Scobleizer
-
-# __init__.py
-
-# __version__= "0.1"
-
-# from .api import Api
-# from .parsers import RawParser, JSONParser, ObjectParser
-
-
-# parsers.py
-
-
-# class RawParser(object):
-#     def parse(raw):
-#         return raw
-
-# class JSONParser(object):
-#     def parse(raw):
-#         return json.loads(raw)
-
-# class PeerreachObject(object):
-#     def __init__(self, raw):
-#         data = json.loads(raw)
-#         self.__dict__.update(data)
-
-# class ObjectParser(object):
-#     def __init__(self, klass=PeerreachObject):
-#     	self.klass = klass
-
-#     def parse(raw):
-#         return klass(raw)
-
-
-# api.py
-
-# import urllib
-
-# class ApiException(Exception):
-#     pass
-
-
-# class NoArgsException(ApiException):
-#     pass
-
-# class TooMuchArgsException(ApiException):
-#     pass
-
-# class HttpError(ApiException)
-#    pass
-
-
-# class Api(object):
-
-
-#     def __init__(self, parser=None, url=None):
-
-# 	self.api_url = url or 'http://api.peerreach.com/v1'
-# 	aelf.api_user_url = api_url + '/user/lookup.json'
-# 	aelf.api_multi_user_url = api_url + '/multi-user/lookup.json'
-
-#         if parser:
-#            self.parser = parser
-# 	slse:
-# 	   self.parser = JSONParser()
-
-#     def parse(data):
-#     	return self.parser.parse(data)
-
-
-#     def get_data(self, url, **options):
-
-#         response = requests.get(url, **options)
-
-#         if str(response.status)[0] != '2':
-#            raise HttpException(response.status)
-
-#         return self.parse(response.data)
-
-
-#     def user_lookup(self, user_id=None, screen_name=None):
-#     	if not any(user_id, screen_name):
-#     	   raise NoArgsException()
-#     	if all(user_id, screen_name):
-#     	   raise TooMuchArgsException()
-
-#     	if user_id:
-#     	   params = {"user_id": user_id}
-#     	else:
-#     	   params = {"screen_name": screen_name}
-
-#     	url = "%s?%s" % (self.api_user_url, urllib.urlencode(params))
-
-#         return self.get_data(url)
-
-#     def multi_user_lookup(self, user_ids=None, screen_names=None):
-#     	if not any(user_ids, screen_names):
-#     	   raise NoArgsException()
-#     	if all(user_ids, screen_names):
-#     	   raise TooMuchArgsException()
-
-#     	if self.user_id:
-#     	   params = {"user_id": ",".join(map(str, user_id))}
-#     	else:
-#     	   params = {"screen_name": ",".join(screen_name)}
-
-#         url = "%s?%s" % (self.api_multi_user_url, urllib.urlencode(params))
-
-#         return self.get_data(url)
-
-
-# runner.py
-
-
-# def main():
-
-#    # -r --raw : Output raw data
-#    # -i --ids : Use ids instead of screen names
-
-#    args, options = parser.parse_args()
-
-#    if not args:
-#       parser.error()
-
-#    if options.raw:
-#       parser = RawParser()
-#    else:
-#       parser = JSONParser()
-
-#    api = Api(parser=parser)
-
-
-#    if len(args) == 1:
-#       if option.ids:
-#          data = api.user_lookup(user_id=args[0])
-#       else:
-#          data = api.user_lookup(screen_name=args[0])
-#    else:
-#       if option.ids:
-#          data = api.multi_user_lookup(user_ids=args)
-#       else:
-#          data = api.multi_user_lookup(screen_names=args)
-
-#    if options.raw:
-#       print data
-#    else:
-#       pprint.pprint(data)
-
-
-
-# tests.py
-
-
-# class TestParser(TestCase):
-
-#     raw = "{'some_raw_data': 1234}"
-
-#     def setUp(self):
-#         pass
-
-#     def test_raw_parse():
-#         parser = RawParser()
-
-#         parsed = parser.parse(self.raw)
-#         self.assertEqual(parsed, self.raw)
-
-#     def test_json_parse():
-#         parser = JSONParser()
-
-#         parsed = parser.parse(self.raw)
-#         self.assertEqual(json.dumps(parsed), self.raw)
-
-#     def test_object_parse():
-#         parser = ObjectParser()
-
-#         parsed = parser.parse(self.raw)
-
-#         data = json.loads(self.raw)
-
-#         self.assertEqual(parsed.some_raw_data, data['some_raw_data'])
-
-
-# class TestApi(TestCase):
-
-#     def test_init_default_url(self):
-#         api = Api()
-#         self.assertEqual(api.api_url, ''http://api.peerreach.com/v1')
-
-#     def test_init_lookup_url(self):
-#         api = Api()
-#         self.assertEqual(api.api_user_url, self.api_url + '/user/lookup.json')
-#         self.assertEqual(api.api_multi_user_url, self.api_url + '/multi-user/lookup.json')
-
-#     def test_init_set_url(self):
-#         api = Api(url='http://test')
-#         self.assertEqual(api.api_url, ''http://test')
-
-#     def test_init_default_parser(self):
-#         api = Api()
-#         self.assertTrue(isinstance(api.parser, JSONParser)
-
-#     def test_init_set_parser(self):
-#         class DummyParser:
-#             pass
-#         parser = DummyParser()
-#         api = Api(parser=parser)
-#         self.assertEqual(parser, api.parser)
-
-#     def test_parser(self):
-#         api = Api()
-
-#         raw = "{'aaa': 'bbbb', 'cccc': 123}"
-#         data = api.parse(raw)
-
-#         self.assertEqual(data, json.loads(raw)
-
-#     @mock("requests.get')
-#     def test_get_data(self, get):
-
-#         class Response:
-#             status_code = 200
-#             data = "test response"
-
-#         get.return_value = Response()
-
-
-#         api.Api()
-
-#         test_url = "http://test"
-
-#         data = api.get_url(test_url)
-
-#         self.assertEqual(data, Response().data)
-#         get.assert_called_once_with(url)
-
-
-#     class DummyApi(Api):
-#         self.get_data(url):
-#             self.called_url = url
-#             return SAMPLE_RETURN
-
-#     def test_user_lookup_user_id(self):
-#         api = DummyApi()
-
-#         data = api.user_lookup(user_id=1111)
-#         self.assertEqual(data , SAMPLE_RETURN)
-#         self.assertEqual(api.called_url, "http:/.......")
-
-#     def test_user_lookup_screen_name(self):
-#         api = DummyApi()
-
-#         data = api.user_lookup(screen_name='test_user')
-#         self.assertEqual(data , SAMPLE_RETURN)
-#         self.assertEqual(api.called_url, "http:/.......")
-
-#     def test_user_lookup_no_data(self):
-# 	api = Api()
-
-# 	self.assertRaises(NoArgsException, api.user_lookup)
-
-#     def test_user_lookup_too_data(self):
-# 	api = Api()
-
-# 	self.assertRaises(TooMuchArgsException, api.user_lookup, user_id=1111. screen_name='test')
-
-#     def test_multi_user_lookup_user_ids(self):
-#         api = DummyApi()
-
-#         data = api.multi_user_lookup(user_ids=[1111,2222])
-#         self.assertEqual(data , SAMPLE_RETURN)
-#         self.assertEqual(api.called_url, "http:/.......")
-
-#     def test_multi_user_lookup_screen_names(self):
-#         api = DummyApi()
-
-#         data = api.multi_user_lookup(screen_names=['test1', 'test2'])
-#         self.assertEqual(data , SAMPLE_RETURN)
-#         self.assertEqual(api.called_url, "http:/.......")
-
-#     def test_multi_user_lookup_no_data(self):
-# 	api = Api()
-
-# 	self.assertRaises(NoArgsException, api.multi_user_lookup)
-
-#     def test_multi_user_lookup_too_data(self):
-# 	api = Api()
-
-# 	self.assertRaises(TooMuchArgsException, api.multi_user_lookup, user_ids=1111. screen_names='test')
-
-
-# requirements.txt
-
-# mock
-# coverage
-# nose
-# requests

File peerreach/runner.py

+
+import json
+from optparse import OptionParser
+
+from .api import Api
+from .parsers import RawParser, JSONParser
+import peerreach
+
 
 def main():
 
-   # -r --raw : Output raw data
-   # -i --ids : Use ids instead of screen names
+    usage = "usage: %prog [options] screen_name [screen_name...]"
 
-   args, options = parser.parse_args()
+    parser = OptionParser(usage=usage, version=peerreach.__version__)
+    parser.add_option("-i", "--ids",
+                     action="store_true", dest="ids",
+                     help="Use twitter ids instead of screen name")
+    parser.add_option("-r", "--raw",
+                     action="store_true", dest="raw",
+                     help="Show raw peerreach api output")
 
-   if not args:
-      parser.error()
+    options, args = parser.parse_args()
 
-   if options.raw:
-      parser = RawParser()
-   else:
-      parser = JSONParser()
+    if not args:
+        parser.error("No twitter users provided")
 
-   api = Api(parser=parser)
+    if options.raw:
+        parser = RawParser()
+    else:
+        parser = JSONParser()
 
+    api = Api(parser=parser)
 
-   if len(args) == 1:
-      if option.ids:
-         data = api.user_lookup(user_id=args[0])
-      else:
-         data = api.user_lookup(screen_name=args[0])
-   else:
-      if option.ids:
-         data = api.multi_user_lookup(user_ids=args)
-      else:
-         data = api.multi_user_lookup(screen_names=args)
+    if len(args) == 1:
+        if options.ids:
+            data = api.user_lookup(user_id=args[0])
+        else:
+            data = api.user_lookup(screen_name=args[0])
+    else:
+        if options.ids:
+            data = api.multi_user_lookup(user_ids=args)
+        else:
+            data = api.multi_user_lookup(screen_names=args)
 
-   if options.raw:
-      print data
-   else:
-      pprint.pprint(data)
-
+    if options.raw:
+        print data
+    else:
+        print json.dumps(data, indent=2)
 
 
 if __name__ == '__main__':
-   main()
+    main()

File tests/__init__.py

-

File tests/test_peerreach.py

 
 SAMPLE_RETURN = object()
 
+
 class DummyApi(Api):
     def get_data(self, url):
         self.called_url = url
     def test_get_data(self, get):
 
         class Response:
-            status = 200
-            data = '{"data": "test response"}'
+            status_code = 200
+            text = '{"data": "test response"}'
 
         get.return_value = Response()
 
-
         api = Api()
 
         test_url = "http://test"
 
         data = api.get_data(test_url)
 
-        self.assertEqual(data, json.loads(Response().data))
+        self.assertEqual(data, json.loads(Response().text))
         get.assert_called_once_with(test_url)
 
     def test_user_lookup_user_id(self):
         api = DummyApi()
 
         data = api.user_lookup(user_id=1111)
-        self.assertEqual(data , SAMPLE_RETURN)
+        self.assertEqual(data, SAMPLE_RETURN)
         self.assertEqual(api.called_url, 'http://api.peerreach.com/v1/user/lookup.json?user_id=1111')
 
     def test_user_lookup_screen_name(self):
         api = DummyApi()
 
         data = api.user_lookup(screen_name='test_user')
-        self.assertEqual(data , SAMPLE_RETURN)
+        self.assertEqual(data, SAMPLE_RETURN)
         self.assertEqual(api.called_url, 'http://api.peerreach.com/v1/user/lookup.json?screen_name=test_user')
 
     def test_user_lookup_no_data(self):
     def test_multi_user_lookup_user_ids(self):
         api = DummyApi()
 
-        data = api.multi_user_lookup(user_ids=[1111,2222])
-        self.assertEqual(data , SAMPLE_RETURN)
+        data = api.multi_user_lookup(user_ids=[1111, 2222])
+        self.assertEqual(data, SAMPLE_RETURN)
         self.assertEqual(api.called_url, 'http://api.peerreach.com/v1/multi-user/lookup.json?user_id=1111%2C2222')
 
     def test_multi_user_lookup_screen_names(self):
         api = DummyApi()
 
         data = api.multi_user_lookup(screen_names=['test1', 'test2'])
-        self.assertEqual(data , SAMPLE_RETURN)
+        self.assertEqual(data, SAMPLE_RETURN)
         self.assertEqual(api.called_url, 'http://api.peerreach.com/v1/multi-user/lookup.json?screen_name=test1%2Ctest2')
 
     def test_multi_user_lookup_no_data(self):