Skip to content

Instantly share code, notes, and snippets.

@krysits
Created October 8, 2015 18:33
Show Gist options
  • Save krysits/4c7426f50a2c58d00691 to your computer and use it in GitHub Desktop.
Save krysits/4c7426f50a2c58d00691 to your computer and use it in GitHub Desktop.
Dumps all ebooks data on subject query
import urllib.request
import json
from pymongo import MongoClient
queryString = 'python'
ur = 'http://it-ebooks-api.info/v1/'
buks = []
def gf(tech):
fo = open(str(tech) + '.txt','r')
for line in fo:
lStr = line.replace('\\n','')
buks.append(lStr)
return buks
def getFile(urla):
response = urllib.request.urlopen(urla)
data = json.loads(response.readall().decode('utf-8'))
return data
def getBookByID(bookID):
urla = ur + 'book/' +str(bookID)
return getFile(urla)
def saveAllBooks():
client = MongoClient()
mydb = client.test
cnt = 0
for buknr in buks:
aBook = getBookByID(buknr)
postID = mydb.ebookz.insert(aBook)
if(postID):
cnt = cnt + 1
print(cnt)
gf(queryString)
saveAllBooks()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment