def test_image(self): logger.info('start testing test_image...') post = Post.objects.get(title='test_post') # tag1 no author image1 = Image(content_object=post, image_src='www.caogecym.com/img1.jpg') image1.save() # tag2 with author image2 = Image(content_object=post, image_src='www.caogecym.com/img2.jpg') image2.save() self.assertEqual(len(post.images.all()), 2)
def handle(self, *args, **options): q = ("select max(id) from forum_messagestore ") cursor = connection.cursor() cursor.execute(q) row = cursor.fetchone() max_id = row[0] print "Max id = %s" % max_id q = ( "select regexp_matches(text, 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+') from forum_messagestore where image_ex=False" ) cursor = connection.cursor() cursor.execute(q) for row in cursor.fetchall(): for k in row: url = k[0].lower() if (url.endswith('.jpg') or url.endswith('.jpeg') \ or url.endswith('.png') or url.endswith('.gif') \ or url.endswith('.svg')): print k[0], type(k[0]) try: ch = Image.objects.get(image_url=k[0]) except Image.DoesNotExist: nim = Image(image_url=k[0]) nim.save() """ """ q = ("update forum_messagestore set image_ex=True where id < %s" % max_id) cursor = connection.cursor() cursor.execute(q) transaction.commit_unless_managed() print "Loading images..." for nim in Image.objects.filter(is_exists=True, image__isnull=True): print "Loading image %s" % nim.image_url try: result = urllib.urlretrieve(nim.image_url) except: print traceback.format_exc() if not result: print "Can't load image:%s" % nim.image_url nim.is_exists = False else: nim.image.save(os.path.basename(nim.image_url), File(open(result[0]), 'rb')) nim.save()
post.like_count = like_count logger.error('ERROR: %s' % e.message) except DatabaseError, e: logger.error('ERROR: %s' % e.message) except Exception, e: logger.error('ERROR: %s' % e.message) else: # load images thread_imgs = body.findAll('img') for img in thread_imgs: for key in ad_list: if key in img['src']: continue if img['src']: image = Image(content_object=post, image_src=img['src']) image.save() logger.info('post %s images recorded successfully \n' % post.id) def create_torrent(self, post_id, site, url, title, *args, **options): if site == SiteType.RMDOWN: try: r = requests.get(url) except requests.ConnectionError, e: logger.error('ERROR: %s' % e.message) return '' soup = BeautifulSoup(r.content, from_encoding="utf-8") ref = soup.findAll('input')[0]['value'] reff = soup.findAll('input')[1]['value'] form_data = [('ref', ref), ('reff', reff),