· 9 years ago · Oct 30, 2016, 09:42 AM
1#!/usr/bin/perl -w
2
3# WRITTEN by Harris Piech z5075792
4
5use Cwd;
6use DBI;
7use strict;
8use warnings;
9
10# variables
11my $curr_dir = getcwd;
12my $post_uni_id = 0;
13my $comment_uni_id = 0;
14
15# handling arguments
16if (@ARGV != 1){
17 print ("correct usage: \nfsys_to-db [file system directory]\n");
18 exit;
19}
20my $directory = $ARGV[0];
21
22my $database = DBI->connect( "dbi:SQLite:data.dbl" ) || die "Cannot connect: $DBI::errstr";
23$database->do( "CREATE TABLE users ( 'zid', 'full_name', 'profile_pic', 'program', 'birthday', 'email', 'courses', 'mates', 'password', 'home_latitude', 'home_longitude', 'home_suburb' )" );
24$database->do( "CREATE TABLE posts ( 'post_id', 'from', 'message', 'time', 'latitude', 'longtitude' )" );
25$database->do( "CREATE TABLE comments ( 'post_id', 'comment_id', 'from', 'time', 'message' )" );
26$database->do( "CREATE TABLE replies ( 'comment_id', 'from', 'time', 'message' )" );
27
28# port all user information from provided sources
29# looping through each user
30$directory = ($curr_dir . "/" . $directory);
31opendir (DIR, $directory) or die $!;
32my @dir = readdir DIR;
33foreach my $user (@dir) {
34 if ($user =~ /z\d{7}/) { # user id verification
35 my $filename = "$directory" . "$user" . "/" . "user.txt"; # processing user.txt
36 my %user_attr = ();
37 open(my $file, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!";
38 while (my $line = <$file>) {
39 chomp $line;
40 my $attr = $line;
41 $attr =~ s/=.*$//;
42 my $val = $line;
43 $val =~ s/^.*=//;
44 $user_attr{$attr} = $val;
45 }
46 if ( ! exists $user_attr{home_suburb} ) { # Nullify unfilled feilds
47 $user_attr{home_suburb} = "NULL";
48 }
49 if ( ! exists $user_attr{home_longtitude} ) {
50 $user_attr{home_longtitude} = "NULL";
51 }
52 if ( ! exists $user_attr{home_latitude} ) {
53 $user_attr{home_latitude} = "NULL";
54 }
55 my $profile_link = "$directory" . "$user" . "/" . "profile.jpg"; # process profile picture
56 if ( -e $profile_link) {
57 $user_attr{profile_pic} = $profile_link;
58 } else {
59 $user_attr{profile_pic} = "NULL";
60 }
61 $database->do( "INSERT INTO users VALUES ( '$user_attr{zid}', '$user_attr{full_name}', '$user_attr{profile_pic}', '$user_attr{program}', '$user_attr{birthday}', '$user_attr{email}', '$user_attr{courses}', '$user_attr{mates}', '$user_attr{password}', '$user_attr{home_latitude}', '$user_attr{home_longtitude}', '$user_attr{home_suburb}' ) "); # add to user attributes to user table
62
63 # looping through the users posts
64 opendir (PST, "$directory" . "$user" . "/" . "posts") or die $!;
65 my @dir1 = readdir PST;
66 foreach my $user_post (@dir1) {
67 if ($user_post =~ /\d+/) { # verify post subfolder
68 my %post_attr = ();
69 my $filename_post = "$directory" . "$user" . "/" . "posts" . "/" . "$user_post" . "/" . "post.txt";
70 open(my $post, '<:encoding(UTF-8)', $filename_post ) or die "Could not open file '$filename_post' $!";
71 while (my $line = <$post>) {
72 chomp $line;
73 my $attr = $line;
74 $attr =~ s/=.*$//;
75 my $val = $line;
76 $val =~ s/^.*=//;
77 $post_attr{$attr} = $val;
78 }
79 if ( ! exists $post_attr{longtitude} ) {
80 $post_attr{longtitude} = "NULL";
81 }
82 if ( ! exists $post_attr{latitude} ) {
83 $post_attr{latitude} = "NULL";
84 }
85 $post_attr{message} =~ s/'/''/g; # this took wayyy too long to figure out
86 $post_attr{post_id} = $post_uni_id;
87 #$database->do( "CREATE TABLE posts ( 'post_id', 'from', 'message', 'time', 'latitude', 'longtitude' )" );
88 $database->do( "INSERT INTO posts VALUES ( '$post_attr{post_id}', '$post_attr{from}', '$post_attr{message}', '$post_attr{time}', '$post_attr{latitude}', '$post_attr{longtitude}' ) ");
89
90 # looping through the users posts comments
91 if (opendir (COMM, "$directory$user/posts/$user_post/comments") ) {
92 my %comm_attr = ();
93 my @dir2 = readdir COMM;
94 foreach my $post_comm (@dir2) {
95 if ($post_comm =~ /\d+/) {
96 #print $post_comm, " $directory$user/posts/$user_post/comments", "\n";
97 my $filename_comment = "$directory$user/posts/$user_post/comments/$post_comm/comment.txt";
98 open(my $comment, '<:encoding(UTF-8)', $filename_comment ) or die "Could not open file '$filename_comment' $!";
99 while (my $line = <$comment>) {
100 chomp $line;
101 my $attr = $line;
102 $attr =~ s/=.*$//;
103 my $val = $line;
104 $val =~ s/^.*=//;
105 $comm_attr{$attr} = $val;
106 }
107 if (! exists $comm_attr{message}) {
108 $comm_attr{message} = "NULL";
109 }
110 $comm_attr{message} =~ s/'/''/g;
111 $comm_attr{comment_id} = $comment_uni_id;
112 $database->do( "INSERT INTO comments VALUES ( '$post_attr{post_id}', '$comm_attr{comment_id}', '$comm_attr{from}', '$comm_attr{time}', '$comm_attr{message}' )" );
113 # looping through the users posts comments replies
114
115 }
116 }
117 $comment_uni_id++;
118 }
119 $post_uni_id ++;
120 }
121
122 }
123 closedir PST;
124 }
125}
126closedir DIR;
127$database->disconnect;