<?php
twitter();
function twitter(){
    $unique_id = 1;
    $mismatch = 0;
    $current_post_url = "";
    $current_json = [];
    for($i= 1; $i < 4; $i++){
        mkdir("C:\\ivLocalProjects\\Twitter\\output\\output$i");
        
        $file = fopen("C:\\ivLocalProjects\\Twitter\\File_$i"."_twitter.csv", 'r');
        $res = [];
        $first = true;
        $current_json = [];
        $has_comments = false;
        $comments_index = 1;
        while ($row = fgetcsv($file, 1024, ",")) {
            
            if($first){
                $first =  false;
                $row[0] = "id";
                $columns = array_map('strtolower', $row);
                continue;
            }
            if(count($columns) !== count($row)){
                echo $mismatch."\n";
                $mismatch++;
                continue;
            }
            $json = array_combine($columns, $row);
            
            $unique_id++;
            $current_json = $json;
            $data_new = [];
            
            foreach($current_json as  $key=>$value){
                switch($key){
                    case "tweet_date":                       
                        $key = "date";
                         break;
                         
                    case "tweet_no":
                        $key = "t_id";
                        break;
                        
                    case "tweets":
                        $key = "text";
                        break;
                    case "tweet_username":
                        $key = "user_screen_name";
                        break;
                    case "tweet_user":
                        $key = "author";
                        break;
              
                    case "tweet_user_location":
                        $key = "user_location";
                        break;
                }
            $data_new[$key] = $value;
            $data_new["FullFolderPath"] = "Twitter";
                
                    
                
            }
            $unique_id = $data_new["t_id"];
            
            file_put_contents("C:\\ivLocalProjects\\Twitter\\output\\output$i\\".$unique_id.".txt.json", json_encode($data_new,JSON_UNESCAPED_UNICODE));
           
            
            $json = array_combine($columns, $row);
          }
        
        
    }
}
function facebook(){
    $unique_id = 1;
    $mismatch = 0;
    $current_post_url = "";
    $current_json = [];
    for($i= 1; $i < 7; $i++){
        
        $file = fopen('C:\\Users\\galar\\Downloads\\csv\\facebook_file_'.$i.'.csv', 'r');
        $res = [];
        $first = true;
        $current_json = [];
        $has_comments = false;
        $comments_index = 1;
        while ($row = fgetcsv($file, 1024, ",")) {
            
            if($first){
                $first =  false;
                $row[0] = "id";
                $columns = array_map('strtolower', $row);
                continue;
            }
            if(count($columns) !== count($row)){
                echo $mismatch."\n";
                $mismatch++;
                continue;
            }
            
            $json = array_combine($columns, $row);
            
            $post_url = $json['post_url'];
            //if current line have different post_url we know we went to next post
            if(strcmp($post_url, $current_post_url) !== 0){
                if(empty($current_json)){
                    $current_json = $json;
                    $current_post_url = $post_url;
                    $current_json['text'] = $json['post'];
                    $date = str_replace('/', '-', $json['date']);
                    $current_json['date'] = $date;
                    $current_json['t_type'] = "facebook";
                    
                }
                else{
                    file_put_contents("C:\\Users\\galar\\Downloads\\csv\\output$i\\post_".$unique_id.".json", json_encode($current_json,JSON_PRETTY_PRINT));
                    $unique_id++;
                    $current_json = $json;
                    $has_comments = false;
                    $comments_index = 1;
                    $current_post_url = $post_url;
                    $current_json['text'] = $json['post'];
                    $date = str_replace('/', '-', $json['date']);
                    $current_json['date'] = $date;
                    $current_json['t_type'] = "facebook";
                }
                
            }
            //take care of comments
            if(!empty($json['comments'])){
                if(!$has_comments){
                    $has_comments = true;
                    $current_json['text'] .= "\n\n@@@@@@\n\n";
                }
                $current_json['text'] .= "($comments_index) ".$json['comments']."\n\n";
                $comments_index++;
            }
        }
        
        
    }
}
