Skip to content

Instantly share code, notes, and snippets.

@ztiromoritz
Created October 13, 2012 21:36
Show Gist options
  • Select an option

  • Save ztiromoritz/3886215 to your computer and use it in GitHub Desktop.

Select an option

Save ztiromoritz/3886215 to your computer and use it in GitHub Desktop.
Find JAR in Central via SHA1
#!/usr/bin/python
# Using REST-API of http://search.maven.org to find artifacts by sha1sum
import sys
import json
import urllib2
import hashlib
def hashfile(filepath):
sha1 = hashlib.sha1()
f = open(filepath, 'rb')
try:
sha1.update(f.read())
finally:
f.close()
return sha1.hexdigest()
def request( hash ):
url = 'http://search.maven.org/solrsearch/select?q=1:"' + \
hash+'"&wt=json&rows=20'
response = urllib2.urlopen(url).read()
return json.loads(response);
def getAllPaths( doc ):
prefix='http://search.maven.org/remotecontent?filepath=';
group=doc["g"]
groupPath=group.replace(".","/")
name=doc["a"]
version=doc["v"]
fullPrefix = prefix + groupPath + "/" + name + "/" + \
version + "/" + name + "-" + version
result=[]
for ec in doc["ec"]:
result.append( fullPrefix + ec )
return result
filename = sys.argv[1]
sha1=hashfile( sys.argv[1] )
obj = request( sha1 )
num = obj["response"]["numFound"]
print sha1 + ' ' + filename
if num > 0:
for path in getAllPaths( obj["response"]["docs"][0] ):
print "\t" + path;
else:
print "NOT FOUND"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment