Browse Source

crawler now scrapes in parallel threads

master
rra 7 years ago
parent
commit
09d76040eb
  1. 86
      fedicrawler.py

86
fedicrawler.py

@ -1,53 +1,77 @@
#!/bin/env python3 #!/bin/env python3
# fediscraper v1 # fediscraper v2
import json, requests,threading import json, requests
from multiprocessing.dummy import Pool as ThreadPool
start_url = 'https://post.lurk.org' start_url = 'https://post.lurk.org'
activity = '' activity = ''
peers_info = '/api/v1/instance/peers'
instance_info = '/api/v1/instance' pool = ThreadPool(128)
def get_peers(instance): def get_peers(instance):
r = requests.get('https://'+instance+'/api/v1/instance/peers', timeout=10) try:
r = requests.get('https://'+instance+'/api/v1/instance/peers', timeout=1)
if r.status_code == 200: if r.status_code == 200:
peers = r.json() peers = r.json()
print(instance, 'peers with', len(peers)) print(instance, 'peers with', len(peers))
return(peers) return peers
else: else:
print('fail: got {} on {}'.format(r.status_code, instance)) print('fail: got {} on {}'.format(r.status_code, instance))
return
except Exception as e:
print('fail on',instance, e)
# print(e)
return
instances = {} def get_instance_info(instance):
r = requests.get(start_url+peers_info) instance_info = '/api/v1/instance'
if r. status_code == 200: print('getting info for', instance)
print('200 for '+start_url)
peers = r.json()
print('{} has {} peers'.format(start_url, len(peers)))
for count, peer in enumerate(peers):
#instances.add(peer)
try:
peer_list = get_peers(peer)
if peer_list:
for i in peer_list:
if i not in instances:
try: try:
ii = requests.get('https://'+i+instance_info, timeout=10) ii = requests.get('https://'+instance+instance_info, timeout=10)
info = ii.json() info = ii.json()
print('info request for {} succeeded'.format(instance))
except Exception as e: except Exception as e:
print('failed to query instance info') print('failed to query instance info')
print(e) # print(e)
info = 'error' info = {'error': 'error'}
pass return info
instances[i] = info
print('added {}, n={}'.format(i,len(instances)))
instances = set([])
r = requests.get(start_url+peers_info)
if r. status_code == 200:
start_peers = pool.map(get_peers, r.json())
for i in start_peers:
if not i:
start_peers.remove(i)
else: else:
print(i.status_code, 'on', peer) pool.map(instances.add, i)
except Exception as e:
print('failure for', peer) network = pool.map(get_peers, instances)
# instances[peer] = {'error':e} for peer_list in network:
print(e) if peer_list:
for instance in peer_list:
instances.add(instance)
instance_info = pool.map(get_instance_info, instances)
scrape = {}
instances_list = list(instances)
for count, value in enumerate(instances_list):
scrape[value] = instance_info[count]
print('found {} instances'.format(len(scrape)))
pool.close()
pool.join()
#text = list(filter(None.__ne__, instances))
with open('instance_scrape.json','w') as f: with open('instance_scrape.json','w') as f:
f.write(json.dumps(instances,indent=4)) f.write(json.dumps(instances,indent=4))

Loading…
Cancel
Save