def parser request try last_twit_id Twit objects order_by -twit_id twi

 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
def parser(request):
try:
last_twit_id = Twit.objects.order_by('-twit_id')[0].twit_id
except:
last_twit_id = 0
#Get twits.
data = StringIO.StringIO()
url = 'http://search.twitter.com/search.json?q=%23pytest'
curl = pycurl.Curl()
curl.setopt(pycurl.URL, url)
curl.setopt(pycurl.WRITEFUNCTION, data.write)
curl.perform()
result_json = data.getvalue()
result = json.loads(result_json)
#Adding new twits
for tw in result['results']:
if(tw['id'] > last_twit_id):
text = tw['text'].split('*')
if(len(text) == 4): # check twits style
#check_url(text[3])
data = {}
artwork_url, data['genre'], data['year'] = get_info(text[1],text[2])
data['album'] = text[2]
data['artist'] = text[1]
data['url'] = ''
data['user'] = tw['from_user']
data['twit_id'] = tw['id']
data['created_at'] = tw['created_at']
data['modered'] = True
data['deleted'] = False
data['artwork'] = artwork(artwork_url, data['artist'],
data['album'])
#check_url(text[3])
p = Twit(**data)
p.save()
return HttpResponseRedirect('/')