PageRenderTime 27ms CodeModel.GetById 10ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/views.py

http://radioappz.googlecode.com/
Python | 107 lines | 104 code | 3 blank | 0 comment | 1 complexity | 34d466a5e3b0b90dcbd67ae17d3ac4c3 MD5 | raw file
  1from django.http import HttpResponseRedirect
  2from django.shortcuts import render_to_response
  3from google.appengine.api import users, memcache
  4from datetime import datetime
  5import time
  6import random
  7import pylast
  8import gdata.youtube
  9import gdata.youtube.service
 10
 11def cb(response):
 12	response["Expires"] = 'Thu, 19 Nov 1981 08:52:00 GMT'
 13	response["Last-Modified"] = str(datetime.now().ctime()) +" GMT"
 14	response["Cache-Control"] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0, must-revalidate'
 15	response["Pragma"] = 'no-cache'
 16	response["P3P"] = 'CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'
 17	return response
 18		
 19def index(request):
 20	
 21	if request.method == 'POST':
 22		API_KEY = ""
 23		API_SECRET = ""
 24		username = ""
 25		password_hash = pylast.md5("")
 26		network = pylast.get_lastfm_network(api_key = API_KEY, api_secret = API_SECRET, username = username, password_hash = password_hash)
 27
 28		s = str(request.POST['text'])
 29		
 30		if not memcache.get(s + 'tops'):
 31			top_songs = []
 32			artist = network.get_artist(s)
 33		
 34			top_tracks = artist.get_top_tracks()
 35			for i in range(0,7):
 36				top_songs.append(top_tracks[i]['item'])
 37			
 38			tops = artist.get_similar(10)
 39			memcache.add(s + 'top_similar', tops, 7200)
 40	
 41			artist1 = network.get_artist(str(tops[0]['item']))
 42			top_tracks = artist1.get_top_tracks()
 43			for i in range(0,10):
 44				top_songs.append(top_tracks[i]['item'])
 45			
 46			artist2 = network.get_artist(str(tops[1]['item']))
 47			top_tracks = artist2.get_top_tracks()
 48			for i in range(0,4):
 49				top_songs.append(top_tracks[i]['item'])
 50			 
 51			artist3 = network.get_artist(str(tops[2]['item']))
 52			top_tracks = artist3.get_top_tracks()
 53			for i in range(0,4):
 54				top_songs.append(top_tracks[i]['item'])
 55				
 56			artist4 = network.get_artist(str(tops[3]['item']))
 57			top_tracks = artist4.get_top_tracks()
 58			for i in range(0,4):
 59				top_songs.append(top_tracks[i]['item'])
 60				
 61			artist5 = network.get_artist(str(tops[4]['item']))
 62			top_tracks = artist5.get_top_tracks()
 63			for i in range(0,2):
 64				top_songs.append(top_tracks[i]['item'])
 65			
 66			artist6 = network.get_artist(str(tops[5]['item']))
 67			top_tracks = artist6.get_top_tracks()
 68			for i in range(0,2):
 69				top_songs.append(top_tracks[i]['item'])
 70			
 71			artist7 = network.get_artist(str(tops[6]['item']))
 72			top_tracks = artist7.get_top_tracks()
 73			for i in range(0,2):
 74				top_songs.append(top_tracks[i]['item'])
 75			
 76			memcache.add(s + 'tops', top_songs, 7200)
 77		
 78		top_songs = memcache.get(s + 'tops')
 79		similar = memcache.get(s + 'top_similar')
 80
 81		random.shuffle(top_songs)
 82		
 83		#google youtubeApi
 84		client = gdata.youtube.service.YouTubeService()
 85		query = gdata.youtube.service.YouTubeVideoQuery()
 86		
 87		query.vq = str(top_songs[0])
 88		query.max_results = '1'
 89		query.start_index = '1'
 90		query.racy = 'exclude'
 91		query.format = '5'
 92		query.orderby = 'relevance'
 93		
 94		video = client.YouTubeQuery(query)
 95		
 96		title = video.entry[0].title.text
 97		url = video.entry[0].media.content[0].url
 98
 99		response = render_to_response('radio.html', {'search': s, 'title': title, 'url': url, 'similar': similar })
100		return cb(response)
101	
102	response = render_to_response('search.html')
103	return cb(response)
104
105def test(request):
106	response = render_to_response('test.html')
107	return cb(response)