Esempio n. 1
0
	def put_station(self, id, shortname, name, link, type, full, thumb):
		station = Station(
			key_name = id,
			shortname = shortname,
			name = name,
			link = link,
			type = type,
			full = full,
			thumb = thumb,
			online = False,
			active = datetime.utcnow(),
		)
		station.put()
		logging.info("Station put in datastore")
		
		memcache.set(self._memcache_station_id, station)
		logging.info("Station put in memcache")
		
		# Put station in proxy
		self._station = station
		
		# Increment admin counter of stations
		admin_proxy = AdminApi()
		admin_proxy.increment_stations_counter()
		logging.info("Counter of stations incremented")
		
		# Mail admins
		self.mail()
Esempio n. 2
0
	def post(self):
		key_name = self.request.get("key_name")
		shortname = self.request.get("shortname")[:30].lower()
		background = json.loads(self.request.get("background"))
		full = background["src_full"]
		thumb = background["src_thumb"]
				
		# We have to check if shortname is ok
		forbidden_characters = re.search("[^a-zA-Z0-9_]", shortname)
		existing_station = Station.all().filter("shortname", shortname).get()
		
		if(forbidden_characters or existing_station or len(shortname) < 4 or len(full) == 0 or len(thumb) == 0):
			logging.info("Forbidden characters or Existing station")
			self.error(403)
		else:
			if key_name == self.user_proxy.user.key().name():
				# Station associated with User, we have to know first if associated station was created
				self.saveProfile(key_name, shortname, self.user_proxy.user.first_name + ' ' + self.user_proxy.user.last_name, None, "user", full, thumb)
				
			elif self.user_proxy.is_admin_of(key_name):
				# We fetch some information about the facebook page
				graph = facebook.GraphAPI(self.user_proxy.access_token)
				page_information = graph.get_object(key_name)
				user_profiles = self.user_proxy.profiles

				# Save the profile
				self.saveProfile(key_name, shortname, page_information["name"], page_information["link"], "page", full, thumb)

			else:
				logging.info("User not admin")
				self.error(403)
Esempio n. 3
0
	def post(self):
		shortname = self.request.get("shortname")[:30].lower()
		
		availability = False
		existing_station = Station.all().filter("shortname", shortname).get()
		if not existing_station:
			availability = True
			
		self.response.out.write(json.dumps({"availability": availability}))
Esempio n. 4
0
	def process(self, page_id):
		station = Station.get_by_key_name(page_id)

		if(station):
			self.station_proxy = StationApi(station.shortname)
			
			# Temporarily, we don't display any Facebook app. Instead we make a redirection to the station on phonoblaster.com
			self.render("station/page/redirect.html", None)					
		else:
			self.render("station/page/404.html", None)
Esempio n. 5
0
	def post(self):
		cursor = self.request.get("cursor")
		
		q = Station.all()
		q.order("updated")
		
		# Is there a cursor?
		if(cursor):
			logging.info("Cursor found")
			q.with_cursor(start_cursor = cursor)
		else:
			logging.info("No cursor")
		
		stations = q.fetch(50)
		
		to_put = []
		done = False
		for s in stations:
			if s.active is None:
				s.active = s.updated
				to_put.append(s)
			else:
				done = True
				break
		
		db.put(to_put)
		logging.info("Station entities updated")
			
		if not done:
			logging.info("Starting another task")
			
			new_cursor = q.cursor()
			task = Task(
				url = "/taskqueue/upgrade",
				params = {
					'cursor': new_cursor,
				},
			)
			task.add(queue_name = "upgrade-queue")
		else:
			logging.info("No more station to update")
			
			subject = "Upgrade of station entities done"
			body = "Everything is OK"
			
			task = Task(
				url = "/taskqueue/mail",
				params = {
					"to": "*****@*****.**",
					"subject": subject,
					"body": body,
				}
			)
			task.add(queue_name = "worker-queue")
Esempio n. 6
0
	def station(self):
		if not hasattr(self, "_station"):
			self._station = memcache.get(self._memcache_station_id)
			if self._station is None:
				logging.info("Station not in memcache")
				self._station = Station.all().filter("shortname", self._shortname).get()
				if(self._station):
					logging.info("Station exists")
					memcache.set(self._memcache_station_id, self._station)
					logging.info("Station put in memcache")
				else:
					logging.info("Station does not exist")
			else:
				logging.info("Station already in memcache")	
		return self._station
Esempio n. 7
0
	def get(self):
		if(self.user_proxy):
			user = self.user_proxy.user
			user_profile = StationApi(self.user_proxy.profile["shortname"])
			user_broadcasts = user_profile.reorder_buffer(user_profile.buffer)["broadcasts"]
			user_live_broadcast = None
			if(len(user_broadcasts)>0):
				user_live_broadcast = user_broadcasts[0]

			live_broadcasts = []
			latest_active_stations = []
			
			q = Station.all()
			q.order("-active")
			stations = q.fetch(30)
			logging.info(str(len(stations))+" latest stations retrieved from datastore")

			all_broadcasts_keys = []
			for i in xrange(0,len(stations)):
				all_broadcasts_keys.extend(stations[i].broadcasts)
			
			all_broadcasts = db.get(all_broadcasts_keys)
			logging.info(str(len(all_broadcasts))+" broadcasts associated with stations retrieved from datastore")			
			
			couple = {}
			for s in stations:
				name = str(s.key().name())
				couple[name] = {
					"station": s,
					"broadcasts": [],
					"live": None,
				}
				
			for b in all_broadcasts:
				name = str(Broadcast.station.get_value_for_datastore(b).name())
				couple[name]["broadcasts"].append(b)
			logging.info("Broadcasts group by station")
						
			# What is the live track for each station?
			now = datetime.utcnow()
			for k, v in couple.iteritems():
				station = v["station"]
				broadcasts = v["broadcasts"]
				
				timestamp = station.timestamp
				
				elapsed = 0
				live = None
				
				total_duration = 0
				for broadcast in broadcasts:
					if(broadcast.youtube_duration):
						total_duration += broadcast.youtube_duration
					else:
						if(broadcast.soundcloud_duration):
							total_duration += broadcast.soundcloud_duration
						else:
							total_duration += 0
				
				# Check if buffer is not empty
				if total_duration > 0:
					offset = (timegm(now.utctimetuple()) - timegm(timestamp.utctimetuple())) % total_duration
					for broadcast in broadcasts:
						if(broadcast.youtube_id):
							id = broadcast.youtube_id
							title = broadcast.youtube_title
							duration = broadcast.youtube_duration
							thumbnail = "https://i.ytimg.com/vi/" + broadcast.youtube_id + "/default.jpg"
						else:
							id = broadcast.soundcloud_id
							title = broadcast.soundcloud_title
							duration = broadcast.soundcloud_duration
							thumbnail = broadcast.soundcloud_thumbnail
													
						# Current broadcast math pattern below
						if elapsed + duration > offset:
							live = {
								'id': id,
								'title': title,
								'duration': duration,
								'thumbnail': thumbnail,
							}
							
							couple[k]["live"] = live							
							break

						# We must keep browsing the list before finding the current track
						else:
							elapsed += duration
			logging.info("Live broadcast determined for each station")
			
			for s in stations:
				name = s.key().name()
				if(couple[name]["live"]):
					latest_active_stations.append(couple[name]["station"])
					live_broadcasts.append(couple[name]["live"])
			
			# Display all the user stations
			template_values = {
				"number_of_sessions": user_profile.number_of_sessions,
				"live": user_live_broadcast,
				"feed": zip(latest_active_stations, live_broadcasts),
			}
			self.render("home.html", template_values)
			
		else:
			self.render("welcome.html", None)