-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkAllPosts.py
executable file
·115 lines (85 loc) · 3.17 KB
/
markAllPosts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python
import os
import sys
def getStreamNames(argUsername):
userStreams = []
#Get all file names in the message directory
for fileName in os.listdir("./messages"):
#Search for all files containing user data
if "StreamUsers" in fileName:
fileName = "./messages/" + fileName
file = open(fileName, 'r')
lines = file.readlines()
#Search for a line that matches the username of the active user and save the index that tells the program how many posts in that strea have already been read
for username in lines:
username = username.strip('\n')
fullName = username
count = len(username)
for character in reversed(username):
count = count - 1
if character == " ":
username = username[:count]
break
if argUsername == username:
fileName = fileName[11:-15]
userStreams.append(fileName)
file.close()
return userStreams
def markPosts(username, streamName, numOfPosts):
#Mark all posts read
if (streamName == "all"):
streamList = getStreamNames(username)
for fileName in streamList:
userFileName = "./messages/" + fileName + "StreamUsers.txt"
dataFileName = "./messages/" + fileName + "StreamData.txt"
oldUserFile = open(userFileName, 'r')
dataFile = open(dataFileName, 'r')
newUserFile = open("./messages/temp.txt", 'w')
lines = oldUserFile.readlines()
dataLineCount = dataFile.readlines()
for user in lines:
user = user.strip('\n')
count = len(user)
for character in reversed(user):
count = count - 1
if character == " ":
usernameShort = user[:count]
#If the usernames match, upate the read count to the highest possible value (total number of posts in the stream)
if (username == usernameShort):
newUserFile.write(usernameShort + " " + str(len(dataLineCount)) + "\n")
else:
newUserFile.write(user + "\n")
break
oldUserFile.close()
newUserFile.close()
dataFile.close()
os.rename("./messages/temp.txt", userFileName)
#Mark all posts in the specified stream read
else:
userFileName = "./messages/" + streamName + "StreamUsers.txt"
dataFileName = "./messages/" + streamName + "StreamData.txt"
userFile = open(userFileName, 'r')
dataFile = open(dataFileName, 'r')
newFile = open("./messages/temp.txt", 'w')
lines = userFile.readlines()
dataLineCount = dataFile.readlines()
#Search for a line that matches the username of the active user and save the index that tells the program how many posts in that strea have already been read
for user in lines:
user = user.strip('\n')
count = len(user)
#Looping through each line backwards, when the first space is found, everything to the right is the post read count; everything to the left is the username
for character in reversed(user):
count = count - 1
if character == " ":
usernameShort = user[:count]
if (username == usernameShort):
newFile.write(user[:count] + " " + str(len(dataLineCount)) + "\n")
else:
newFile.write(user + "\n")
userFile.close()
newFile.close()
os.rename("./messages/temp.txt", userFileName);
def main():
markPosts(sys.argv[2], sys.argv[1], sys.argv[3]);
if __name__ == "__main__":
main()