Пример #1
0
def main(reddit, subreddits):
    print("Begin Main Loop")

    #Infinite Loop
    while True:

        #All the subreddits - loop over them
        for subr in subreddits:

            print("Process on:\t" + subr)

            #outputFileList - List that is being written to the file
            #popularityList - Sub-List of outputFileList that contains the valuable data that's required
            outputFileList = []
            popularityList = []

            #Inner Loop - Goes through posts in subreddit
            for post in reddit.subreddit(subr).top('hour'):

                #Get the variables of the post
                postVars = vars(post)

                #Check the popularity of given post #Params Passed: Upvotes, Downvotes, Number of Comments
                popularity = PopularityIndex.popularityIndex(
                    int(postVars['ups']), int(postVars['downs']),
                    int(postVars['num_comments']))

                #Append the popularity to the popularityList
                popularityList.append(popularity)

            #Rectify the Popularity List
            #Case 1: When less than 10 posts are there per hour
            #Solution 1: Add 0's to make the list have 10 elements
            popularityList.sort(reverse=True)
            if len(popularityList) < 10:
                for i in range(len(popularityList), 10, 1):
                    popularityList.append(0)

            #Case 2: When more than 10 posts are present in an hour
            #Solution 2: Sort the list in Descending order and take top 10
            if len(popularityList) > 10:
                popularityList.sort(reverse=True)
                popularityList = popularityList[0:10]

            #Write popularityList to outputFileList
            outputFileList.append(str(datetime.now()))
            outputFileList.append(popularityList)

            #Write The outputFileList to the File
            subRFileObj = open("Modules/SubredditData/" + subr, 'a')
            subRFileObj.write("$" + str(outputFileList))
            subRFileObj.close()

            Log.SubredditLog(subr)

            #Nap for 9 seconds
            sleep(9)