def totalFinderAgents(agentID,agentOrGroup): uri=urlMaker('replace_with_fd_site_name',agentID,[2,10],1,"agent") print("\nURL= {}".format(uri)) apiResponse=makeRequest(uri) responseParser=JsonTraverseParser(apiResponse) total=responseParser.traverse("total") return total
def test_string_and_digit_keys_invalid(self): parser = JsonTraverseParser('[{"a": 0}]') self.assertEqual(parser.traverse('a0.0'), None) self.assertEqual(parser.traverse('0a.a'), None) self.assertEqual(parser.traverse('0.a0'), None) self.assertEqual(parser.traverse('0.0a'), None) self.assertEqual(parser.traverse('A'), None)
def test_dict_data_invalid(self): parser = JsonTraverseParser('{"a": 1}') self.assertEqual(parser.traverse('0'), None) self.assertEqual(parser.traverse('b'), None) self.assertEqual(parser.traverse('a.a'), None) self.assertEqual(parser.traverse('a.0'), None) self.assertEqual(parser.traverse('a.0', force_list=True), [])
def all_senators(): ALL_SENATOR_URL = BASE_URL + "/115/senate/members.json" r = requests.get(url=ALL_SENATOR_URL, headers=HEADERS) data = r.json() json_string = json.dumps(data) parser = JsonTraverseParser(json_string) member_data = parser.traverse("results.members") return member_data
def totalFinder(statusCode,code,agentOrGroup): uri=urlMaker('replace_with_fd_site_name',code,[statusCode],1,agentOrGroup) print("\nURL= {}".format(uri)) apiResponse=makeRequest(uri) #print("Blah Type: {}".format(type(apiResponse))) if agentOrGroup=="group": findCount(statusCode,apiResponse) responseParser=JsonTraverseParser(apiResponse) total=responseParser.traverse("total") return total
def test_string_data(self): parser = JsonTraverseParser('"hello world"') self.assertEqual(parser.traverse(''), "hello world") self.assertEqual(parser.traverse('', force_list=True), ["hello world"]) parser = JsonTraverseParser('{"hello world": ["hello", "world"]}') self.assertEqual(parser.traverse('hello world.0'), "hello") self.assertEqual(parser.traverse('hello world.0', force_list=True), ["hello"]) self.assertEqual(parser.traverse('hello world.0.0'), "h") self.assertEqual(parser.traverse('hello world.0.0', force_list=True), ["h"])
def test_null(self): parser = JsonTraverseParser('') with self.assertRaises(TypeError): parser.traverse(None) parser = JsonTraverseParser(None) self.assertEqual(parser.traverse(''), None) with self.assertRaises(TypeError): parser.traverse(None)
def get_tv_cast(tvmaze_id): """returns cast Args: tvmaze_id ([str]): [description] Returns: [type]: [description] """ api_url = "http://api.tvmaze.com/shows/" + str(tvmaze_id) + "/cast" with urllib.request.urlopen(api_url) as search_result: res = JsonTraverseParser(search_result.read().decode()) cast = res.traverse("person.name") cast_images = res.traverse("person.image.original") return cast, cast_images
def test_dict_list_data(self): parser = JsonTraverseParser('{"a": [0, 1]}') self.assertEqual(parser.traverse('a'), [0, 1]) self.assertEqual(parser.traverse('a', force_list=True), [0, 1]) self.assertEqual(parser.traverse('a.0'), 0) self.assertEqual(parser.traverse('a.0', force_list=True), [0]) self.assertEqual(parser.traverse('a.1'), 1) self.assertEqual(parser.traverse('a.1', force_list=True), [1])
def get_tv_crew(tvmaze_id): """[summary] Args: tvmaze_id ([type]): [description] Returns: [type]: [description] """ api_url = "http://api.tvmaze.com/shows/" + str(tvmaze_id) + "/crew" with urllib.request.urlopen(api_url) as search_result: # results = search_result.read().decode().replace( # "null", "None").replace("false", "False").replace("true", "True") res = JsonTraverseParser(search_result.read().decode()) crew = res.traverse("person.name") crew_images = res.traverse("person.image.original") return crew, crew_images
def test_list_data_invalid(self): parser = JsonTraverseParser('[1]') self.assertEqual(parser.traverse('a'), None) self.assertEqual(parser.traverse('a.0'), None) self.assertEqual(parser.traverse('1'), None) self.assertEqual(parser.traverse('1.a'), None) self.assertEqual(parser.traverse('0.a'), None) self.assertEqual(parser.traverse('0.a', force_list=True), [])
def test_multi_character_keys(self): # [0, 1, 2, ... 99] parser = JsonTraverseParser(str(json.dumps(list(range(0, 100))))) self.assertEqual(parser.traverse('99'), 99) self.assertEqual(parser.traverse('99', force_list=True), [99]) # {"a": 0, "aa": 1, "aaa": 2, ... "aaaa...": 99} parser = JsonTraverseParser( str(json.dumps({"a" * i: i for i in range(0, 100)}))) self.assertEqual(parser.traverse('a' * 99), 99) self.assertEqual(parser.traverse('a' * 99, force_list=True), [99])
def test_bytes(self): with self.assertRaises(TypeError): parser = JsonTraverseParser(b'[]') parser = JsonTraverseParser('') with self.assertRaises(TypeError): parser.traverse(b'a')
def test_list_dict_data_invalid(self): parser = JsonTraverseParser('[{"a": 0}, {"b": 1}]') self.assertEqual(parser.traverse('a.0'), None) self.assertEqual(parser.traverse('2'), None) self.assertEqual(parser.traverse('2', force_list=True), [])
countJobs = '' rota = "" # headers = { # 'X-API-KEY': '' # } ## request API r = requests.get(url=rota) ## JSON PARSE data = r.json() json_string = json.dumps(data) parser = JsonTraverseParser(json_string) ## Contagens jobName = parser.traverse("Jobs.JobName") #Retrieve variable from SH #PARM = sys.argv[1] PARM = "QueueProcessJob" if jobName != None: countJobType = jobName.count(PARM) else: countJobType = 0
def test_list_dict_data(self): parser = JsonTraverseParser('[{"a": 0}, {"b": 1}]') self.assertEqual(parser.traverse('0'), {"a": 0}) self.assertEqual(parser.traverse('0', force_list=True), [{"a": 0}]) self.assertEqual(parser.traverse('0.a'), 0) self.assertEqual(parser.traverse('0.a', force_list=True), [0]) self.assertEqual(parser.traverse('1'), {"b": 1}) self.assertEqual(parser.traverse('1', force_list=True), [{"b": 1}]) self.assertEqual(parser.traverse('1.b'), 1) self.assertEqual(parser.traverse('1.b', force_list=True), [1]) self.assertEqual(parser.traverse('a'), 0) self.assertEqual(parser.traverse('a', force_list=True), [0])
def test_dict_data(self): parser = JsonTraverseParser('{"a": 1}') self.assertEqual(parser.traverse(''), {"a": 1}) self.assertEqual(parser.traverse('', force_list=True), [{"a": 1}]) self.assertEqual(parser.traverse('a'), 1) self.assertEqual(parser.traverse('a', force_list=True), [1])
def get_member_information(member_results): # JsonTraverseParser needs a string json_string = json.dumps(member_results) # Get a JsonTraverseParser Object parser = JsonTraverseParser(json_string) member_id = parser.traverse("results.member_id") first_name = parser.traverse("results.first_name") last_name = parser.traverse("results.last_name") date_of_birth = parser.traverse("results.date_of_birth") url = parser.traverse("results.url") govtrack_id = parser.traverse("results.govtrack_id") cspan_id = parser.traverse("results.cspan_id") votesmart_id = parser.traverse("results.votesmart_id") icpsr_id = parser.traverse("results.icpsr_id") twitter_account = parser.traverse("results.twitter_account") facebook_account = parser.traverse("results.facebook_account") crp_id = parser.traverse("results.crp_id") in_office = parser.traverse("results.in_office") congress = parser.traverse("results.roles.congress") chamber = parser.traverse("results.roles.chamber") title = parser.traverse("results.roles.title") ocd_id = parser.traverse("results.roles.ocd_id") fec_candidate_id = parser.traverse("results.roles.fec_candidate_id") bills_sponsored = parser.traverse("results.roles.bills_sponsored") committee_names = parser.traverse("results.roles.committees.name") sub_committee_names = parser.traverse("results.roles.subcommittees.name") sub_committee_parent_id = parser.traverse( "results.roles.subcommittees.parent_committee_id") legislator_container = [] # Create a legislator legislator_data = { 'member_id': member_id, 'first_name': first_name, 'last_name': last_name, 'date_of_birth': date_of_birth, 'url': url, 'govtrack_id': govtrack_id, 'cspan_id': cspan_id, 'votesmart_id': votesmart_id, 'icpsr_id': icpsr_id, 'twitter_account': twitter_account, 'facebook_account': facebook_account, 'crp_id': crp_id, 'in_office': in_office, 'congress': congress, 'chamber': chamber, 'title': title, 'ocd_id': ocd_id, 'fec_candidate_id': fec_candidate_id, 'bills_sponsored': bills_sponsored, 'committee_names': committee_names, 'sub_committee_names': sub_committee_names, 'sub_committee_parent_id': sub_committee_parent_id } legislator_container.append(legislator_data) pprint.pprint(legislator_container)
def test_list_data(self): parser = JsonTraverseParser('[1]') self.assertEqual(parser.traverse(''), [1]) self.assertEqual(parser.traverse('', force_list=True), [1]) self.assertEqual(parser.traverse('0'), 1) self.assertEqual(parser.traverse('0', force_list=True), [1])
from jsontraverse.parser import JsonTraverseParser import urllib.request x = None with urllib.request.urlopen("https://api.tvmaze.com/shows/27436/seasons") as f: x = JsonTraverseParser(f.read().decode()) print(x)
def test_unstandard_digit_keys_invalid(self): parser = JsonTraverseParser('[{"a": 0}]') self.assertEqual(parser.traverse('00.0'), None) self.assertEqual(parser.traverse('01.0'), None) self.assertEqual(parser.traverse('0001.0'), None) self.assertEqual(parser.traverse('-1.0'), None)
def test_digit_keys_in_dict(self): parser = JsonTraverseParser('[{"0": 0}]') self.assertEqual(parser.traverse('0.0'), 0) self.assertEqual(parser.traverse('0.0', force_list=True), [0])
def totalFinder(statusCode): uri = urlMaker('eneter_fd_site_name', 5, [statusCode], 1) apiResponse = makeRequest(uri) responseParser = JsonTraverseParser(apiResponse) total = responseParser.traverse("total") return total
def test_multiple_return_values(self): parser = JsonTraverseParser( '{"a": [{"b": [{"c": 0}, {"c": 1}]}, {"b": [{"c": 2}, {"c": 3}]}]}' ) self.assertEqual(parser.traverse('a.b.c'), [0, 1, 2, 3]) self.assertEqual(parser.traverse('a.b.c', force_list=True), [0, 1, 2, 3]) parser = JsonTraverseParser( '{"a": [{"b": {"c": 0}}, {"b": [{"c": 1}, {"c": 2}]}]}') self.assertEqual(parser.traverse('a.b.c'), [0, 1, 2]) self.assertEqual(parser.traverse('a.b.c', force_list=True), [0, 1, 2]) parser = JsonTraverseParser('[{"a": 0}, {"a": {"b": 1}}]') self.assertEqual(parser.traverse('a.b'), 1) self.assertEqual(parser.traverse('a.b', force_list=True), [1])
def test_simple_data_uses_custom_impl(self): parser = JsonTraverseParser('{"a": 0}', custom_json_impl=CustomJsonImpl) self.assertEqual(parser.data, CustomJsonImpl.return_value)
def test_empty_data(self): parser = JsonTraverseParser('') self.assertEqual(parser.traverse(''), None) self.assertEqual(parser.traverse('', force_list=True), [])
import requests import json from jsontraverse.parser import JsonTraverseParser import sys rota = "" # headers = { # 'X-API-KEY': '' # } ## request API r = requests.get(url=rota) ## JSON PARSE data = r.json() json_string = json.dumps(data) parser = JsonTraverseParser(json_string) ## Contagent jobName = parser.traverse("Jobs.JobName", force_list=1) if jobName != None: allJobs = len(jobName) else: allJobs = 0 print(allJobs)
import requests import sys from jsontraverse.parser import JsonTraverseParser if len(sys.argv) > 1: r = requests.get(f"https://www.reddit.com/r/{sys.argv[1]}.json", headers = {'User-agent': 'grpy 0.1'}) else: r = requests.get("https://www.reddit.com/r/python.json", headers = {'User-agent': 'grpy 0.1'}) parser = JsonTraverseParser(r.text) if parser.traverse("error") is None: for x in range(1,25): print(f"{x}. " + parser.traverse(f"data.children.{x-1}.data.title")) else: print("Error") print(r.text)
def test_empty_data_still_none(self): parser = JsonTraverseParser('', custom_json_impl=CustomJsonImpl) self.assertEqual(parser.data, None)