Created
October 8, 2015 18:33
-
-
Save krysits/4c7426f50a2c58d00691 to your computer and use it in GitHub Desktop.
Dumps all ebooks data on subject query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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