Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pickle
- class PostClass:
- def __init__(self,userID,text,Privacy=None,link=None): #we say there MUST be a user, as well as content.
- #if the user is "none", then it's a post from the user to nobody, btw.
- if userID:
- self.Poster = userID #Users use an ID lookup.
- else:
- self.Poster = None
- self.Text = text #simple enough, I'd think.
- self.Link = link #an optional link to use.
- self.Privacy = Privacy #1 is default.
- def show(self):
- print(self.Text)
- if self.Link:
- print("External Link: {}".format(self.Link))
- class User:
- def __init__(self,Name,passWord,Privacy=1): #an ID of 0 = new user.
- self.Name = Name
- self.Password = passWord
- #the below ( Friends, and Blocked ) are a list of id's.
- self.Friends = [self] #<- Having Friends[0] equal to self, provides some nice functionality. o:
- self.Blocked = []
- self.Requests = []
- self.Posts = []
- self.ID = User.ID #static variable
- User.ID = User.ID + 1
- #the naming was difficult, because 'post' can be either a noun, or a verb... :\
- def post(self,text,userID=0,Privacy=None,link=None):
- self.Friends[userID].Posts.insert(0,PostClass(self,text,link))
- def showPost(self,postID,viewer,isFeed=True):
- if not isFeed:
- if self.Posts[postID].Privacy:
- if viewer in Post.Privacy:
- print("{} posted:".format(self.Name))
- self.Posts[postID].show()
- return True
- else: return False
- else:
- print("{} posted:".format(self.Name))
- self.Posts[postID].show()
- return True
- else:
- for Post in self.Posts:
- #if Post.Poster:
- # continue
- if Post.Privacy:
- if viewer in Post.Privacy:
- print("{} posted:".format(self.Name))
- Post.show()
- else: return False
- else:
- print("{} posted:".format(self.Name))
- Post.show()
- return True
- return false
- def showFeed(self,userID=0):
- print("I have {} friends.".format(len(self.Friends)))
- for i in range(1,10):
- if( i >= len(self.Friends) ): break
- print("Friend number {}'s name is {}".format(i,self.Friends[i].Name))
- print("They have {} posts.".format(len(self.Friends[i].Posts)))
- if userID == 0: #home. grab one post from 20 friends.
- for i in range(1,20):
- if i >= len(self.Friends): break
- #try until we show one
- x = 0
- while not self.Friends[i].showPost(x,self.ID):
- print("{} is showing feed {}::{}.".format(self.Name, self.Friends[i].Name, x))
- if len(self.Friends[i].Posts)<=x: break
- else: x = x + 1
- else:
- for i in range(1,20):
- if i >= len(self.Friends): break
- x = 0
- while not self.Friends[userID].showPost(x,self.ID):
- if len(self.Friends[userID].Posts)<=x: break
- else: x = x + 1
- def friend(self,targeted): targeted.Requests.append(self)
- def denyRequest(self,indice): del self.Requests[indice]
- def acceptRequest(self,indice): self.Friends.append(self.Requests.pop(indice).Friends.append(self))
- def saveUsers(dat,fname):
- try:
- f = open(fname,'wb')
- pickle.dump(dat,f)
- except IOError as e:
- print("Couldn't open {} for saving the users. :l".format(fname))
- def loadUsers(fname):
- try:
- f = open(fname,'rb')
- ret = pickle.load(f)
- except IOError as e:
- ret = []
- return ret
- Users = loadUsers('fbdata.dat')
- User.ID = 0
- import atexit
- atexit.register(saveUsers,dat=Users,fname='fbdata.dat')
Add Comment
Please, Sign In to add comment