Example #1
0
	def buffer(self):
		if not hasattr(self, "_buffer"):
			self._buffer = memcache.get(self._memcache_station_buffer_id)
			if self._buffer is None:
				station = self.station
				
				if station is not None and station.broadcasts is not None:
					keys = station.broadcasts
			 		broadcasts_entities = db.get(keys)
			 		broadcasts = Broadcast.get_extended_broadcasts(broadcasts_entities, self.station)
				else:
					broadcasts = []
				
				timestamp = self.station.timestamp

				self._buffer = {
					'broadcasts': broadcasts,
					'timestamp': timestamp
				}
				
				memcache.add(self._memcache_station_buffer_id, self._buffer)
				logging.info("Buffer put in memcache")
			else:
				logging.info("Buffer retrieved from memcache")
		
		return self._buffer
Example #2
0
	def put_buffer(self, buffer):
		station = self.station
		new_timestamp = buffer['timestamp']
		new_broadcasts = buffer['broadcasts']

		#Retrieving broadcasts from datastore
		key_names = []
		for i in xrange(0,len(new_broadcasts)):
			b = new_broadcasts[i]
			key_names.append(b['key_name'])

		broadcasts = Broadcast.get_by_key_name(key_names)
		b_keys = []

		for i in xrange(0,len(broadcasts)):
			b_key = broadcasts[i]
			b_keys.append(b_key.key())

		station.timestamp = new_timestamp
		station.broadcasts = b_keys
		station.active = datetime.utcnow()
		station.put()
		logging.info("Buffer put in datastore")

		self._buffer = {
			'broadcasts':new_broadcasts,
			'timestamp': new_timestamp
		}
		memcache.set(self._memcache_station_id, station)
		logging.info("Station updated in memcache")
		
		memcache.set(self._memcache_station_buffer_id, self._buffer)
		logging.info("Buffer updated in memcache")
Example #3
0
	def get(self, key_name):
		broadcast = Broadcast.get_by_key_name(key_name)
		
		if(broadcast):
			m = re.match(r"(\w+).(\w+)", key_name)
			shortname = m.group(1)			
			self.station_proxy = StationApi(shortname)
			
			user_agent = self.request.headers["User-Agent"]
			facebook_agent = "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)"
			
			if(user_agent != facebook_agent):
				# Redirect to station
				self.redirect("/" + shortname)
			else:
				# Facebook linter
				station = self.station_proxy.station
				template_values = {
					"broadcast" : broadcast,
				}
				self.render("station/facebook/broadcast.html", template_values)
		else:
			# 404 error
			self.render("station/404.html", None)
Example #4
0
	def add_track_to_buffer(self,incoming_track):
		buffer = self.reorder_buffer(self.buffer)
		new_broadcasts = buffer['broadcasts'][::]  # Copy array and resting broadcasts
		timestamp = buffer['timestamp']
		room = self.room_in_buffer()

		# Edge Case, if adding track to position 1 5 seconds before the live track ends, we reject the operation.
		# This is due to the latency of Pubnub.
		if(len(new_broadcasts) == 1):
			# We need to check if the live track ends in the next 5 seconds
			live_broadcast = new_broadcasts[0]
			live_broadcast_duration = live_broadcast['duration']
			start = timegm(datetime.utcnow().utctimetuple()) - timegm(timestamp.utctimetuple())
			time_before_end = live_broadcast_duration-start

			if(time_before_end< 5):
				# Rejecting action
				logging.info("Rejecting operation because of an edge case (adding)")
				return None
		# End of edge case
		
		extended_broadcast = None
		if(room > 0):
			logging.info("Room in buffer")
			track = Track.get_or_insert(incoming_track, self.station)

			if track:
				logging.info("Track found")

				submitter_key = None
				if(incoming_track["track_submitter_key_name"] != self.station.key().name()):
					submitter_key_name = incoming_track["track_submitter_key_name"]
					submitter_key = db.Key.from_path("Station", submitter_key_name)

				if(track.youtube_id):		
					new_broadcast = Broadcast(
						key_name = incoming_track["key_name"],
						track = track.key(),
						youtube_id = track.youtube_id,
						youtube_title = track.youtube_title,
						youtube_duration = track.youtube_duration,
						station = self.station.key(),
						submitter = submitter_key,
					)
				else:
					new_broadcast = Broadcast(
						key_name = incoming_track["key_name"],
						track = track.key(),
						soundcloud_id = track.soundcloud_id,
						soundcloud_title = track.soundcloud_title,
						soundcloud_duration = track.soundcloud_duration,
						soundcloud_thumbnail = track.soundcloud_thumbnail,
						station = self.station.key(),
						submitter = submitter_key,
					)
				
				new_broadcast.put()
				logging.info("New broadcast put in datastore")

				extended_broadcast = Broadcast.get_extended_broadcasts([new_broadcast], self.station)[0]

				# Injecting traks in buffer
				new_broadcasts.append(extended_broadcast)
				new_buffer = {
					'broadcasts': new_broadcasts,
					'timestamp': timestamp
				}

				# Save data
				self.put_buffer(new_buffer)
			else:
				logging.info("Track not found")
		else:
			logging.info("There is no more room in the buffer.")
				
		return extended_broadcast
Example #5
0
	def delete(self):
		cursor = self.request.get("cursor")
		track_id = self.request.get("track_id")
		typeModel = self.request.get("type")

		if(track_id):
			track = Track.get_by_id(int(track_id))
			logging.info("Getting from datastore track with track_id = " + track_id)

			if(track):
				logging.info("Track found")

				if(typeModel == "broadcast"):
					# Deleting broadcasts associated to track
					query = Broadcast.all()
					query.filter("track", track)

					if(cursor):
						logging.info("Cursor found")
						query.with_cursor(start_cursor = cursor)

					broadcasts = query.fetch(100)
					logging.info("Fetched : %d Broadcast(s) from datastore."%(len(broadcasts)))
					
					if(len(broadcasts)>0):
						db.delete(broadcasts)
						logging.info("Deleted : %d Broadcast(s) from datastore."%(len(broadcasts)))

						task = Task(
							method = 'DELETE',
							url = "/taskqueue/deletetrack",
							params = {
								"cursor": cursor,
								"track_id": track_id,
								"type": typeModel,
							}
						)
						task.add(queue_name="worker-queue")
					else:
						task = Task(
							method = 'DELETE',
							url = "/taskqueue/deletetrack",
							params = {
								"track_id": track_id,
								"type": "like",
							}
						)
						task.add(queue_name="worker-queue")

				elif (typeModel == "like"):
					# Deleting likes associated to track
					query = Like.all()
					query.filter("track", track)
					
					if(cursor):
						logging.info("Cursor found")
						query.with_cursor(start_cursor = cursor)

					like = query.get()

					if like is None:
						logging.info("No like for this track, deleting track.")
						track.delete()
					else:
						try:
							listener_proxy = StationApi(like.listener.shortname)
							listener_proxy.decrement_likes_counter()
							logging.info("Listener Like counter decremented")
						except:
							logging.info("User has not created a station (likes used to be linked to user to stations)")
						
						Track.decrement_likes_counter(track.key().id())
						logging.info("Track likes counter decremented")

						like.delete()
						logging.info("Like deleted from datastore")

						cursor = query.cursor()

						task = Task(
							method = 'DELETE',
							url = "/taskqueue/deletetrack",
							params = {
								"cursor": cursor,
								"track_id": track_id,
								"type": typeModel,
							}
						)
						task.add(queue_name="worker-queue")
						
				response = True
		else:
			response = False
		self.response.out.write(json.dumps({ "response": response }))