Example #1
0
def main():
    reload(sys)
    sys.setdefaultencoding('utf8')

    df1 = pd.read_csv('segoutput_old.csv', index_col=False)
    df1 = df1.set_index(['segment_id'])

    df2 = pd.read_csv('segoutput.csv', index_col=False)
    df2 = df2.set_index(['segment_id'])
    try:
        main_logger(df2, df1)
        # strava1 main_logger (warlog creation)
        res = requests.get("https://nosnch.in/ae58837141")
    except Exception as e:
        print 'Error: ' + str(e)
        pass

    # strava1_segment main
    res = requests.get("https://nosnch.in/26ba53ff3d")
def main(argv):
    timeframe = None
    segoutput = 'segoutput.csv'
    if len(argv) > 1:
        timeframe = argv[1]
        segoutput = 'segoutput_' + argv[1] + '.csv'

    print('read ' + segoutput)
    #skip "segment_name" since nan causes diff
    usecols = [
        "id", "latitude", "longitude", "name", "type", "color", "segment_id",
        "url"
    ]
    df1 = pd.read_csv(segoutput, index_col=False, usecols=usecols)
    df1 = df1.set_index(['segment_id'])

    club = 202883
    client = Client(access_token='99c2994556a29905b96eb4197996854041ca47ca')

    segoutfile = open(segoutput, 'w')
    segoutfile.write(
        'id,latitude,longitude,name,type,color,segment_name,segment_id,url' +
        '\n')
    segoutputlist = []

    friend_df = pd.read_csv('friend_colour_new.csv', index_col=False)
    friend_count_dict = {}

    #Column #2 Segment_name
    s = pd.read_csv('segment_details.csv', index_col=2)
    for idx, segment in s.iterrows():
        num = int(segment.no)
        j = int(segment.id)

        try:
            leaderboard = retry_get_leaderboard(client, j, club, timeframe)
            if not leaderboard:
                topguy = 'UNCLAIMED'
                topguy_id = 0
            else:
                topguy = leaderboard[0].athlete_name

            if not topguy in friend_df['shortname'].values:
                new_friend = {
                    'name': topguy,
                    'id': 'xxx',
                    'colour': '646464',
                    'shortname': topguy
                }
                friend_df = friend_df.append(new_friend, ignore_index=True)

            if topguy in friend_count_dict:
                friend_count_dict[topguy] += 1
            else:
                friend_count_dict[topguy] = 1

            for z in segment_details(num, segment, topguy, friend_df):
                segoutfile.write(str(z) + ',')
            segoutfile.write('\n')

        except Exception as e:
            print(str(e))
            badoutfile = open('bad_segments.csv', 'a+')
            badoutfile.write(str(j) + ',' + '\n')
            badoutfile.close()
            pass

        time.sleep(1.5)  #Strava limit 600/15mins

    segoutfile.close()

    now = datetime.datetime.now().strftime("%Y%m%d-%H%M")
    copyfile(
        segoutput,
        'segment_history/' + segoutput.replace('.csv', '_' + now + '.csv'))

    friend_df.to_csv('friend_colour_new.csv', index=False)

    #segment count output
    segmentcount = 'segmentcount.csv'
    if len(argv) > 1:
        segmentcount = 'segmentcount_' + argv[1] + '.csv'
    segcountoutfile = open(segmentcount, 'w')
    segcountoutfile.write('name,colour,count' + '\n')
    for x in friend_count_dict:
        if x != 'UNCLAIMED':
            print(str(x), ': ', str(friend_count_dict[x]))
            segcountoutfile.write(
                str(friend_df.loc[friend_df['shortname'] == x,
                                  'name'].values[0]) + ',' +
                str(friend_df.loc[friend_df['shortname'] == x,
                                  'colour'].values[0]) + ',' +
                str(friend_count_dict[x]) + '\n')
    segcountoutfile.write('\n')
    segcountoutfile.close()

    #segment count over time output
    segmentcountovertime = 'segmentcountovertime.csv'
    if len(argv) > 1:
        segmentcountovertime = 'segmentcountovertime_' + argv[1] + '.csv'
    segcountovertimefile = open(segmentcountovertime, 'a+')
    nowdate = datetime.datetime.now().strftime('%Y-%m-%d')
    for x in friend_count_dict:
        if x != 'UNCLAIMED':
            segcountovertimefile.write(
                str(nowdate) + ',' +
                str(friend_df.loc[friend_df['shortname'] == x,
                                  'name'].values[0]) + ',' +
                str(friend_df.loc[friend_df['shortname'] == x,
                                  'colour'].values[0]) + ',' +
                str(friend_count_dict[x]) + '\n')
    segcountovertimefile.close()

    # read newly created segoutput.csv (df2) and compare it to original (df1):
    time.sleep(5)
    df2 = pd.read_csv(segoutput, index_col=False, usecols=usecols)
    df2 = df2.set_index(['segment_id'])
    try:
        main_logger(df2, df1, timeframe)
    except Exception as e:
        print('Error: ', str(e))
        pass
Example #3
0
def main():
    reload(sys)  
    sys.setdefaultencoding('utf8')
    
    df1 = pd.read_csv('segoutput.csv',index_col=False)
    df1 = df1.set_index(['segment_id'])
    
    segmentlist = []
    file = open('segments.csv')
    reader = csv.DictReader(file)
    for line in reader:
        segmentlist.append(line["Segment Id"])

    #get rid of badsegments
    badsegments = []
    badinfile = open('bad_segments.csv')
    badreader = csv.DictReader(badinfile)
    for line in badreader:
        badsegments.append(line["Segment Id"])
    print 'Bad Segments: '+str(badsegments)
    
    for x in badsegments:
        if x in segmentlist:
            segmentlist.remove(x)
        
    client = Client(access_token='76824abf6abf903eb3d8b0bde83625135c0be0ec')
    athlete = client.get_athlete()
    print("Hello, {}. I know your email is {}".format(athlete.firstname, athlete.email))
    josh_friends = client.get_athlete_friends(5991862)
    print "Starting...."        
    #colors
    colours = ['575757','FFCDF3','FFEE33','FF9233','29D0D0','8126C0','814A19','1D6914','2A4BD7','AD2323','000000','88C6ED','C7258E']
    
    segoutfile = open('segoutput.csv', 'w')
    segoutfile.write('id,latitude,longitude,name,type,color,segment_name,segment_id,url'+'\n')
    segoutputlist = []

    friend_colour_dict = {}
    friend_colour_file = open('friend_colour.csv')
    colourreader = csv.DictReader(friend_colour_file)
    for line in colourreader:
        friend_colour_dict[line["name"]] = line["colour"]

    friend_count_dict = {}
           
    
    for num,j in enumerate(segmentlist):
        time.sleep(3)
        segment = retry_get_segment(client,j)
                        
        try:
            leaderboard = retry_get_leaderboard(client,j)
            if not leaderboard:
                topguy = 'UNCLAIMED'
            else:
                topguy = leaderboard[0].athlete_name
                            
            if not topguy in friend_colour_dict:
                friend_colour_dict[topguy] = colours.pop()
                print str(topguy)+' not in friend_colour_dict, popping colour: '+ str(friend_colour_dict[topguy])

            if topguy in friend_count_dict:
                friend_count_dict[topguy] += 1
            else:
                friend_count_dict[topguy] = 1

                      
            
            for z in segment_details(num,segment,topguy,friend_colour_dict):
                segoutfile.write(str(z)+',')
            segoutfile.write('\n')
            
   
        except Exception:
            #badoutfile = open('bad_segments.csv', 'a+')
            #badoutfile.write(str(j)+','+'\n')
            #badoutfile.close()
            pass

    
    
    segoutfile.close()
    
    #segment count output
    segcountoutfile = open('segmentcount.csv', 'w')
    segcountoutfile.write('name,colour,count'+'\n')
    for x in friend_count_dict:
        if x != 'UNCLAIMED':
            print str(x)+': '+str(friend_count_dict[x])
            segcountoutfile.write(str(x)+','+str(friend_colour_dict[x])+','+str(friend_count_dict[x])+'\n')
    segcountoutfile.write('\n')
    segcountoutfile.close()
    json_convert_segmentcount()
    


    #segment count over time output
    segcountovertimefile = open('segmentcountovertime.csv', 'a+')
    nowdate = datetime.datetime.now().strftime('%Y-%m-%d')
    for x in friend_count_dict:
        if x != 'UNCLAIMED':
            segcountovertimefile.write(str(nowdate)+','+str(x)+','+str(friend_colour_dict[x])+','+str(friend_count_dict[x])+'\n')
    segcountovertimefile.close()
    trim_count_overtime()
    json_convert_trim_count_overtime()

    time.sleep(5)
    #read newly created segoutput.csv (df2) and compare it to original (df1):
    df2 = pd.read_csv('segoutput.csv',index_col=False)
    df2 = df2.set_index(['segment_id'])  
    try:
        main_logger(df2,df1)
        #strava1 main_logger (warlog creation)
        res = requests.get("https://nosnch.in/ae58837141")
    except Exception as e:
        print 'Error: '+str(e)
        pass

    # strava1_segment main
    res = requests.get("https://nosnch.in/26ba53ff3d")
Example #4
0
def main():
    #reload(sys)
    #sys.setdefaultencoding('utf8')

    df1 = pd.read_csv('segoutput.csv', index_col=False)
    df1 = df1.set_index(['segment_id'])

    segmentlist = []
    file = open('segments.csv')
    reader = csv.DictReader(file)
    for line in reader:
        segmentlist.append(line["Segment Id"])

    club = 202883
    client = Client(access_token='76824abf6abf903eb3d8b0bde83625135c0be0ec')

    segoutfile = open('segoutput.csv', 'w')
    segoutfile.write(
        'id,latitude,longitude,name,type,color,segment_name,segment_id,url' +
        '\n')
    segoutputlist = []

    friend_df = pd.read_csv('friend_colour_new.csv', index_col=False)

    friend_count_dict = {}

    for num, j in enumerate(segmentlist):
        time.sleep(3)
        segment = retry_get_segment(client, j)

        try:
            leaderboard = retry_get_leaderboard(client, j, club)
            if not leaderboard:
                topguy = 'UNCLAIMED'
                topguy_id = 0

            else:
                topguy = leaderboard[0].athlete_name

                #topguy_id = leaderboard[0].athlete_id

            if not topguy in friend_df['shortname'].values:
                new_friend = {
                    'name': topguy,
                    'id': 'xxx',
                    'colour': '646464',
                    'shortname': topguy
                }
                friend_df = friend_df.append(new_friend, ignore_index=True)

            if topguy in friend_count_dict:
                friend_count_dict[topguy] += 1
            else:
                friend_count_dict[topguy] = 1

            for z in segment_details(num, segment, topguy, friend_df):
                segoutfile.write(str(z) + ',')
            segoutfile.write('\n')

        except Exception:
            badoutfile = open('bad_segments.csv', 'a+')
            badoutfile.write(str(j) + ',' + '\n')
            badoutfile.close()
            pass

    segoutfile.close()
    friend_df.to_csv('friend_colour_new.csv', index=False)

    #segment count output
    segcountoutfile = open('segmentcount.csv', 'w')
    segcountoutfile.write('name,colour,count' + '\n')
    for x in friend_count_dict:
        if x != 'UNCLAIMED':
            print(str(x) + ': ' + str(friend_count_dict[x]))
            segcountoutfile.write(
                str(friend_df.loc[friend_df['shortname'] == x,
                                  'name'].values[0]) + ',' +
                str(friend_df.loc[friend_df['shortname'] == x,
                                  'colour'].values[0]) + ',' +
                str(friend_count_dict[x]) + '\n')
    segcountoutfile.write('\n')
    segcountoutfile.close()

    #segment count over time output
    segcountovertimefile = open('segmentcountovertime.csv', 'a+')
    nowdate = datetime.datetime.now().strftime('%Y-%m-%d')
    for x in friend_count_dict:
        if x != 'UNCLAIMED':
            segcountovertimefile.write(
                str(nowdate) + ',' +
                str(friend_df.loc[friend_df['shortname'] == x,
                                  'name'].values[0]) + ',' +
                str(friend_df.loc[friend_df['shortname'] == x,
                                  'colour'].values[0]) + ',' +
                str(friend_count_dict[x]) + '\n')
    segcountovertimefile.close()

    time.sleep(5)

    #read newly created segoutput.csv (df2) and compare it to original (df1):
    df2 = pd.read_csv('segoutput.csv', index_col=False)
    df2 = df2.set_index(['segment_id'])
    try:
        main_logger(df2, df1)
    except Exception as e:
        print('Error: ' + str(e))
        pass