You can get the Danbooru source code using Subversion. Run svn co svn://donmai.us/danbooru/trunk for the latest copy.
svn co svn://donmai.us/danbooru/trunk
All Danbooru code is released under a FreeBSD license.