def sync_book(bid, cm): # 完结了的图书不更新信息 if Book.objects.filter(id=bid, status=1).count() == 0: page = urllib2.urlopen( "http://wap.cmread.com/r/p/viewdata.jsp?bid=%s&cm=%s&vt=9" % (bid, cm)) data = page.read() try: result = json.loads(data, encoding="utf-8") print result update = Book.objects.filter(id=bid).count() != 0 book = Book() book.pk = int(bid) book.name = result['showName'] book.brief = result['brief'] book.desc = result['desc'] book.cover_url = result['bigCoverLogo'] book.cover_url_small = result['smallCoverLogo'] book.status = result['status'] book.first_cid = result['firstChpaterCid'] book.last_cid = result['lastChapterCid'] book.chapter_size = result['chapterSize'] book.score = result['score'] book.word_size = result['wordSize'] book.click_amount = result['clickValue'] book.kw = result['kw'] book.price = int(float(result['price']) * 100) book.charge_mode = result['chargeMode'] if update: book.save(force_update=update, update_fields=('name', 'brief', 'desc', 'cover_url', 'cover_url_small', 'status', 'first_cid', 'last_cid', 'chapter_size', 'score', 'word_size', 'click_amount', 'kw', 'price', 'charge_mode')) else: book.save(force_insert=True) return True except Exception, e: print e.message
import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "wechatRead.settings_dev") import django django.setup() from books.models import Book, Category from data.book_data import row_data for book in row_data: b = Book() b.name = book['name'] b.author = book['author'] b.cover = book['cover'] b.brief = book['brief'] b.word_count = book['word_count'] b.copyright = book['copyright'] b.rank = book['rank'] b.price = book['price'] b.read_count = book['read_count'] b.is_end = book['is_end'] b.is_index = book['is_index'] b.is_new = book['is_new'] category = Category.objects.filter(name=book['category']) if category: category = category[0] b.category = category b.save()