coding utf-8 from mufularo base models import Photo RosbestPhotoRaitin

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# -*- coding: utf-8 -*-
from mufularo.base.models import Photo, RosbestPhotoRaiting
from urllib2 import urlopen
from django.core.exceptions import ObjectDoesNotExist
import re
class RB:
userlink = "http://rosbest.ru/users/397"
def parseUPage(self):
page = urlopen(self.userlink).read()
pattern = re.compile('''<a href="/photos/comments/([^"]+)" class="thumb" title="([^"]+)"><img src="([^"]+)" alt="([^"]+)"/></a>''')
photos = pattern.findall(page)
return photos
def parsePhotos(self, photos):
rateP = re.compile('curvalue:"([^"]+)"')
result = ""
for photo in photos:
link = "http://rosbest.ru/photos/comments/%s" % (photo[0])
try:
photo = Photo.objects.get(label=photo[3])
page = urlopen(link).read()
rate = rateP.findall(page)
if len(rate) > 0:
if photo.rosbestphotoraiting_set.all():
rbRat = photo.rosbestphotoraiting_set.all()[0]
rbRat.rate = str(rate[0])
else:
rbRat = RosbestPhotoRaiting(rate=str(rate[0]))
rbRat.save()
rbRat.photo.add(photo)
rbRat.save()
result+="%s: %s<br/>"%(photo.label, str(rate[0]))
#ratings.append(rateP.findall(page))
except ObjectDoesNotExist:
pass
return result
def test():
rb = RB()
return rb.parsePhotos(rb.parseUPage())