def get_positions_today_json(file): today = datetime.datetime.now() today = today.strftime("%Y-%h-%d") tomorrow = datetime.datetime.today() + datetime.timedelta(days=1) tomorrow = tomorrow.strftime("%Y-%h-%d") lines_today = subprocess.check_output("grep " + "'" + today + "' "+file, shell=True) lines_tomorrow = subprocess.check_output("grep " + "'" + today + "' "+file, shell=True) lines = lines_today + lines_tomorrow lines = lines.split("\n") json_export = {} i = 0 for line in lines: line = line.split(",") line = map(lambda l: l.strip(), line) if len(line) < 5: continue line = map(lambda l: l.strip(), line) line = {"date" : line[0], "azi" : line[3], "elev" : line[4], "delta" : line[5], "deldot" : line[6]} date_json = strptime(line["date"], "%Y-%h-%d %H:%M") date_json = str(date_json[0]) + str(date_json[1]) + str(date_json[2]) + str(date_json[3]) + str(date_json[4]) json_export.update({int(date_json):line}) import json return json.dumps(json_export)
def get_interval_and_start_time(request): context = {} context = get_names_for_all_column_headers() move_interval = request.GET.get('move_interval', None) datetime_start = request.GET.get('datetime_start', None) interval = request.GET.get('interval', None) if not interval: interval = 10*60 #default interval is 10 minutes else: interval = int(interval)*60 #converting minutes (more suitable) to seconds if not datetime_start or datetime_start.lower() == 'now': datetime_start = utils.strftime(utils.get_local_now(), "%Y-%m-%d %H:%M:%S") start_timestamp = utils.datelocal_totimestamp(utils.strptime(datetime_start, "%Y-%m-%d %H:%M:%S")) if move_interval and move_interval == 'back': start_timestamp -= interval elif move_interval and move_interval == 'forward': start_timestamp += interval context['datetime_start'] = datetime_start context['start_timestamp'] = start_timestamp context['interval'] = interval return context
def get_positions_today_json(file): today = datetime.datetime.now() today = today.strftime("%Y-%h-%d") tomorrow = datetime.datetime.today() + datetime.timedelta(days=1) tomorrow = tomorrow.strftime("%Y-%h-%d") lines_today = subprocess.check_output("grep " + "'" + today + "' " + file, shell=True) lines_tomorrow = subprocess.check_output("grep " + "'" + today + "' " + file, shell=True) lines = lines_today + lines_tomorrow lines = lines.split("\n") json_export = {} i = 0 for line in lines: line = line.split(",") line = map(lambda l: l.strip(), line) if len(line) < 5: continue line = map(lambda l: l.strip(), line) line = { "date": line[0], "azi": line[3], "elev": line[4], "delta": line[5], "deldot": line[6] } date_json = strptime(line["date"], "%Y-%h-%d %H:%M") date_json = str(date_json[0]) + str(date_json[1]) + str( date_json[2]) + str(date_json[3]) + str(date_json[4]) json_export.update({int(date_json): line}) import json return json.dumps(json_export)
def set_time_parameters(start_timestamp, end_timestamp, datetime_from, datetime_to, interval, move_interval): # dictionary used to update time fields in form submited with back or forward buttons update_form_time = {} update_form_time['datetime_start'] = None update_form_time['datetime_end'] = None update_form_time['start_timestamp'] = None update_form_time['end_timestamp'] = None # A good URL has start and end_timestamp filled in. # If not, we proceed through a hiearchy to determine the time to use if start_timestamp and end_timestamp: update_form_time['start_timestamp'] = start_timestamp start_timestamp = int(start_timestamp) update_form_time['end_timestamp'] = end_timestamp end_timestamp = int(end_timestamp) else: # data type conversions if start_timestamp: update_form_time['start_timestamp'] = start_timestamp start_timestamp = int(start_timestamp) if end_timestamp: update_form_time['end_timestamp'] = end_timestamp if end_timestamp.lower() == "now": end_timestamp = int(time.time()) # set end_timestamp to now else: end_timestamp = int(end_timestamp) if interval: interval = int(interval) if datetime_to.lower() == "now": datetime_to = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # set datetime_to to current datetime # datetime format trumps UNIX timestamp format if both are given # datetimes if datetime_from and datetime_to: start_timestamp = utils.datelocal_totimestamp(utils.strptime(datetime_from, '%Y-%m-%d %H:%M:%S')) end_timestamp = utils.datelocal_totimestamp(utils.strptime(datetime_to, '%Y-%m-%d %H:%M:%S')) elif datetime_from and interval: update_form_time['datetime_start'] = datetime_from start_timestamp = utils.datelocal_totimestamp(utils.strptime(datetime_from, '%Y-%m-%d %H:%M:%S')) end_timestamp = start_timestamp + interval elif datetime_to and interval: update_form_time['datetime_end'] = datetime_to end_timestamp = utils.datelocal_totimestamp(utils.strptime(datetime_to, '%Y-%m-%d %H:%M:%S')) start_timestamp = end_timestamp - interval # UNIX timestamps elif start_timestamp and end_timestamp: pass elif start_timestamp and interval: end_timestamp = start_timestamp + interval elif end_timestamp and interval: start_timestamp = end_timestamp - interval else: return (0,0) #return HttpResponseBadRequest("Please double check your date/time values.") # this doesn't work - what to do instead? # back or forward buttons interval = end_timestamp - start_timestamp # TOOD: get rid of and interval once i determine how interval is taken care of if move_interval and interval: if move_interval == 'back': start_timestamp -= interval end_timestamp -= interval elif move_interval == 'forward': start_timestamp += interval end_timestamp += interval if update_form_time['start_timestamp'] and update_form_time['end_timestamp']: update_form_time['start_timestamp'] = start_timestamp update_form_time['end_timestamp'] = end_timestamp else: if update_form_time['datetime_start'] and interval: update_form_time['datetime_start'] = datetime.datetime.fromtimestamp(start_timestamp).strftime('%Y-%m-%d %H:%M:%S') elif update_form_time['datetime_end'] and interval: update_form_time['datetime_end'] = datetime.datetime.fromtimestamp(end_timestamp).strftime('%Y-%m-%d %H:%M:%S') elif update_form_time['start_timestamp'] and interval: update_form_time['start_timestamp'] = start_timestamp elif update_form_time['end_timestamp'] and interval: update_form_time['end_timestamp'] = end_timestamp return (start_timestamp, end_timestamp, update_form_time)
import csv import json import cPickle from utils import is_recent, strptime field_name = ['id', 'name', 'url', 'artist', 'duration', 'listeners', 'playcount', 'streamable', 'total_shouts', 'releasedate'] f = open("../data/recent_tracks.pkl", "rb") tracks = cPickle.load(f) with open('../results/tracks_info.csv', 'wb') as csvfile: w = csv.DictWriter(csvfile, field_name, extrasaction='ignore') first = dict((f, f) for f in field_name) w.writerow(first) for t in tracks: t['artist'] = t['artist']['name'] releasedate = t['releasedate'].strip() if not releasedate or not is_recent(strptime(releasedate)): continue for i in t: value = t[i] if isinstance(value, unicode): t[i] = value.encode('utf-8') w.writerow(t)