Hacker news

In [1]:
from csv import reader
opened_file = open("hacker_news.csv")
read_file = reader(opened_file)
hn = list(read_file)
headers = hn[0]

print(hn[:4])
[['id', 'title', 'url', 'num_points', 'num_comments', 'author', 'created_at'], ['12224879', 'Interactive Dynamic Video', 'http://www.interactivedynamicvideo.com/', '386', '52', 'ne0phyte', '8/4/2016 11:52'], ['10975351', 'How to Use Open Source and Shut the Fuck Up at the Same Time', 'http://hueniverse.com/2016/01/26/how-to-use-open-source-and-shut-the-fuck-up-at-the-same-time/', '39', '10', 'josep2', '1/26/2016 19:30'], ['11964716', "Florida DJs May Face Felony for April Fools' Water Joke", 'http://www.thewire.com/entertainment/2013/04/florida-djs-april-fools-water-joke/63798/', '2', '1', 'vezycash', '6/23/2016 22:20']]
In [2]:
headers = hn[0]
hn = hn[1:]
print(headers)
print(hn[:4])
['id', 'title', 'url', 'num_points', 'num_comments', 'author', 'created_at']
[['12224879', 'Interactive Dynamic Video', 'http://www.interactivedynamicvideo.com/', '386', '52', 'ne0phyte', '8/4/2016 11:52'], ['10975351', 'How to Use Open Source and Shut the Fuck Up at the Same Time', 'http://hueniverse.com/2016/01/26/how-to-use-open-source-and-shut-the-fuck-up-at-the-same-time/', '39', '10', 'josep2', '1/26/2016 19:30'], ['11964716', "Florida DJs May Face Felony for April Fools' Water Joke", 'http://www.thewire.com/entertainment/2013/04/florida-djs-april-fools-water-joke/63798/', '2', '1', 'vezycash', '6/23/2016 22:20'], ['11919867', 'Technology ventures: From Idea to Enterprise', 'https://www.amazon.com/Technology-Ventures-Enterprise-Thomas-Byers/dp/0073523429', '3', '1', 'hswarna', '6/17/2016 0:01']]
In [3]:
ask_posts = []
show_posts = []
other_posts = []
for row in hn: 
    title = row[1]
    if title.lower().startswith("ask hn"):
        ask_posts.append(row)
    elif title.lower().startswith("show hn"):
        show_posts.append(row)
    else:
        other_posts.append(row)
        
print(len(ask_posts))
print(len(show_posts))
print(len(other_posts))
print(ask_posts[4])
1744
1162
17194
['10394168', 'Ask HN: Someone offered to buy my browser extension from me. What now?', '', '28', '17', 'roykolak', '10/15/2015 16:38']
In [4]:
total_ask_comments = 0
for row in ask_posts:
    num_comments = int(row[4])
    total_ask_comments += num_comments
    
avg_ask_comments = total_ask_comments/len(ask_posts)
print(avg_ask_comments)
14.038417431192661
In [5]:
total_show_comments = 0
for row in show_posts:
    num_comments_ = int(row[4])
    total_show_comments += num_comments
    
avg_show_comments = total_show_comments/len(show_posts)
print(avg_show_comments)
2.0

Numero de comentarios en ask_posts vs show_posts

Segun los resultados antes mostrados, los pots de tipo ask reciben mas comentarios.

Esto se puede deber a que al ser una pregunta, invita mas a la interaccion entre los usuarios y fomenta la participación.

In [6]:
print("coments_avg ask pots: " + str(avg_ask_comments) + " vs " + "coments avg show pots: " + str(avg_show_comments))
coments_avg ask pots: 14.038417431192661 vs coments avg show pots: 2.0
In [7]:
import datetime as dt 
result_list = []
for row in ask_posts:
    created_at = dt.datetime.strptime(row[-1], "%m/%d/%Y %H:%M")
    num_comments_n = int(row[4])
    result_list.append([created_at,num_comments_n])
In [8]:
counts_by_hour = {}
comments_by_hour = {}
for row in result_list: 
    hour_key = row[0].hour
    str_hour_key = str(hour_key)
    time_hour = dt.datetime.strptime(str_hour_key, "%H")
    time_hour_strf = time_hour.strftime("%H")
    if time_hour_strf in counts_by_hour:
        counts_by_hour[time_hour_strf] +=1
        comments_by_hour[time_hour_strf] += row[1]
    else:
        counts_by_hour[time_hour_strf] = 1 
        comments_by_hour[time_hour_strf] = row[1]
        
In [9]:
print(counts_by_hour)
{'09': 45, '13': 85, '10': 59, '14': 107, '16': 108, '23': 68, '12': 73, '17': 100, '15': 116, '21': 109, '20': 80, '02': 58, '18': 109, '03': 54, '05': 46, '19': 110, '01': 60, '22': 71, '08': 48, '04': 47, '00': 55, '06': 44, '07': 34, '11': 58}
In [10]:
print(comments_by_hour)
{'09': 251, '13': 1253, '10': 793, '14': 1416, '16': 1814, '23': 543, '12': 687, '17': 1146, '15': 4477, '21': 1745, '20': 1722, '02': 1381, '18': 1439, '03': 421, '05': 464, '19': 1188, '01': 683, '22': 479, '08': 492, '04': 337, '00': 447, '06': 397, '07': 267, '11': 641}
In [11]:
avg_by_hour = []
for row in counts_by_hour and comments_by_hour:
    hour = row
    avg_coments = comments_by_hour[hour]/counts_by_hour[hour]
    avg_by_hour.append([hour,avg_coments])
        
print(avg_by_hour)
[['09', 5.5777777777777775], ['13', 14.741176470588234], ['10', 13.440677966101696], ['14', 13.233644859813085], ['16', 16.796296296296298], ['23', 7.985294117647059], ['12', 9.41095890410959], ['17', 11.46], ['15', 38.5948275862069], ['21', 16.009174311926607], ['20', 21.525], ['02', 23.810344827586206], ['18', 13.20183486238532], ['03', 7.796296296296297], ['05', 10.08695652173913], ['19', 10.8], ['01', 11.383333333333333], ['22', 6.746478873239437], ['08', 10.25], ['04', 7.170212765957447], ['00', 8.127272727272727], ['06', 9.022727272727273], ['07', 7.852941176470588], ['11', 11.051724137931034]]
In [12]:
swap_avg_by_hour = []
for row in avg_by_hour:
    swap_avg_by_hour.append([row[1],row[0]])
print(swap_avg_by_hour)
[[5.5777777777777775, '09'], [14.741176470588234, '13'], [13.440677966101696, '10'], [13.233644859813085, '14'], [16.796296296296298, '16'], [7.985294117647059, '23'], [9.41095890410959, '12'], [11.46, '17'], [38.5948275862069, '15'], [16.009174311926607, '21'], [21.525, '20'], [23.810344827586206, '02'], [13.20183486238532, '18'], [7.796296296296297, '03'], [10.08695652173913, '05'], [10.8, '19'], [11.383333333333333, '01'], [6.746478873239437, '22'], [10.25, '08'], [7.170212765957447, '04'], [8.127272727272727, '00'], [9.022727272727273, '06'], [7.852941176470588, '07'], [11.051724137931034, '11']]
In [13]:
sorted_swap = sorted(swap_avg_by_hour, reverse = True)
print(sorted_swap[:4])
[[38.5948275862069, '15'], [23.810344827586206, '02'], [21.525, '20'], [16.796296296296298, '16']]

Creo una variable que almacena mi fecha y hora local. strftime_now_dt

La usare mas adelante para determinar segun mi región en que hora deberia postear en base a la región fuente.

Mi region local es Europe/Madrid

In [14]:
import pytz

local_naive = dt.datetime.now()
strftime_now = local_naive.strftime("%H:%M")
strftime_now_dt = dt.datetime.strptime(strftime_now,"%H:%M")
In [15]:
import pytz
avg_coments_per_post = []
best_hour_my_timezones = []
for row in sorted_swap[:5]:
    format_dt_time = str(row[1])
    object_t_hour = dt.datetime.strptime(format_dt_time,"%H")
    strf_object = object_t_hour.strftime("%H:%M")
    creating_timedelta= dt.timedelta(hours = int(row[1]))
    test_test = strftime_now_dt - creating_timedelta
    best_hour_my_timezone = test_test.strftime("%H:%M")
    best_hour_my_timezones.append(best_hour_my_timezone)
    format_str_coments = "{}: {:.2f} average comments per post".format(best_hour_my_timezone,row[0])
    avg_coments_per_post.append(format_str_coments)

    

Las 5 mejores horas donde se generan mas numero de comentarios por post

A continuación muestra las mejores horas adaptadas a mi región

In [16]:
for row in avg_coments_per_post:
    print(row)
11:06: 38.59 average comments per post
00:06: 23.81 average comments per post
06:06: 21.52 average comments per post
10:06: 16.80 average comments per post
05:06: 16.01 average comments per post