def mostFollowed(): urlList = [] urlListSort = [] csv_writer = csv.writer(stdout) if (len(sys.argv) <2 ): mostfoll = URLsMostFollowed(['log.txt']) else: mostfoll = URLsMostFollowed([sys.argv[1]]) """URLsMostFollowed is defined in most_followed.py. It takes a json data file and outputs URLs (key) and number of hits (value)""" with mostfoll.make_runner() as runner: runner.run() for line in runner.stream_output(): key_value = mostfoll.parse_output_line(line) """urlList will contain all the title words (key) along with the number of hits (value)""" urlList.append(key_value) """Sort urlList on the number of hits and store it in descending order in urlListSort""" urlListSort = sorted(urlList, key=operator.itemgetter(1), reverse=True) i = 0 """Take the top ten items from the sorted list and write it in csv format""" while (i < len(urlListSort)): csv_writer.writerow(urlListSort[i]) i=i+1 return True
def mostFollowed(): urlList = [] urlListSort = [] csv_writer = csv.writer(stdout) if (len(sys.argv) < 2): mostfoll = URLsMostFollowed(['log.txt']) else: mostfoll = URLsMostFollowed([sys.argv[1]]) """URLsMostFollowed is defined in most_followed.py. It takes a json data file and outputs URLs (key) and number of hits (value)""" with mostfoll.make_runner() as runner: runner.run() for line in runner.stream_output(): key_value = mostfoll.parse_output_line(line) """urlList will contain all the title words (key) along with the number of hits (value)""" urlList.append(key_value) """Sort urlList on the number of hits and store it in descending order in urlListSort""" urlListSort = sorted(urlList, key=operator.itemgetter(1), reverse=True) i = 0 """Take the top ten items from the sorted list and write it in csv format""" while (i < len(urlListSort)): csv_writer.writerow(urlListSort[i]) i = i + 1 return True
def comments(): try: mostfoll_bool = mostFollowed() app.logger.debug(mostfoll_bool) dictobj = {} urlList = [] mostfoll = URLsMostFollowed(['log.txt']) with mostfoll.make_runner() as runner: runner.run() for line in runner.stream_output(): key_value = mostfoll.parse_output_line(line) urlList.append(key_value) urlListSort = sorted(urlList, key=operator.itemgetter(1), reverse=True) dictobj['most'] = urlListSort browserList = [] browaction = BrowserAgentAction(['log.txt']) with browaction.make_runner() as runner: runner.run() for line in runner.stream_output(): key_value = browaction.parse_output_line(line) browserList.append(key_value) dictobj['browser'] = browserList urlToday = [] urlYest = [] percChange = [] urlTrendList = [] today = str(datetime.datetime.now().date()) yesterday = str( (datetime.datetime.now() - datetime.timedelta(1)).date()) trending = TrendingURL(['log.txt']) with trending.make_runner() as runner: runner.run() for line in runner.stream_output(): key_value = trending.parse_output_line(line) logdate = key_value[0][0].split()[0] if (logdate == today): urlToday.append(key_value) elif (logdate == yesterday): urlYest.append(key_value) i = 0 while (i < len(urlToday)): j = 0 while (j < len(urlYest)): if (urlToday[i][0][1] == urlYest[j][0][1]): pChange = ((urlToday[i][1] - urlYest[j][1]) * 100) / (urlYest[j][1]) if (pChange > 0): percChange.append([urlToday[i][0][1], pChange]) j = j + 1 i = i + 1 percChangeSort = sorted(percChange, key=operator.itemgetter(1), reverse=True) dictobj['trend'] = percChangeSort app.logger.debug("Entered GET1") jsonobj = flask.jsonify(dictobj) return jsonobj except: import traceback traceback.print_exc() raise Exception('something is wrong')
def comments(): try: mostfoll_bool = mostFollowed() app.logger.debug(mostfoll_bool) dictobj ={}; urlList = [] mostfoll = URLsMostFollowed(['log.txt']) with mostfoll.make_runner() as runner: runner.run() for line in runner.stream_output(): key_value = mostfoll.parse_output_line(line) urlList.append(key_value) urlListSort = sorted(urlList, key=operator.itemgetter(1), reverse=True) dictobj['most']=urlListSort browserList =[]; browaction = BrowserAgentAction(['log.txt']) with browaction.make_runner() as runner: runner.run() for line in runner.stream_output(): key_value = browaction.parse_output_line(line) browserList.append(key_value) dictobj['browser']=browserList urlToday = [] urlYest = [] percChange = [] urlTrendList = [] today = str(datetime.datetime.now().date()) yesterday = str((datetime.datetime.now() - datetime.timedelta(1)).date()) trending = TrendingURL(['log.txt']) with trending.make_runner() as runner: runner.run() for line in runner.stream_output(): key_value = trending.parse_output_line(line) logdate = key_value[0][0].split()[0] if (logdate == today): urlToday.append(key_value) elif (logdate == yesterday): urlYest.append(key_value) i = 0 while(i<len(urlToday)): j = 0 while(j<len(urlYest)): if (urlToday[i][0][1] == urlYest[j][0][1]): pChange = ((urlToday[i][1] - urlYest[j][1]) * 100) / (urlYest[j][1]) if (pChange > 0): percChange.append([urlToday[i][0][1], pChange]) j = j + 1 i = i + 1 percChangeSort = sorted(percChange, key=operator.itemgetter(1), reverse=True) dictobj['trend']=percChangeSort app.logger.debug("Entered GET1") jsonobj = flask.jsonify(dictobj) return jsonobj except: import traceback traceback.print_exc() raise Exception('something is wrong')