In this video, we will write a script which gets all the posts submitted by the user in the last 24
hours in a particular subreddit in any subreddits, actually.
But as an example, we will work on this subreddits.
This subreddit is a boat blowing glass, so creating glass.
Which looks like this more or less.
And I chose this subreddit because this is a relatively low number of submissions per 24 hours, so
it's not a very popular subreddit, which means it's a good subreddit to experiment with.
For example, we see that in the last 24 hours, there are about four or five submissions by users.
So we're going to check that if we also go to these four or five submissions in our Python codes.
That way, we can evaluate the accuracy of our scripts.
So to do this, you need to have a Reddit account and also creates an app in Reddit.
I showed how to do that in the previous video, and also you need to create or edit instance.
Using that glass and putting all your dots in here, I also showed this how to do in the previous video.
So now what we need to do is this time we need to create a subreddit instance which comes from Reddit.
So that's valuable.
Come here, Dot Sub.
Read it.
So this now gets as string a subreddit.
The subreddit is the name as a stream goes the title of a name of a subreddit.
You can see it here glassblowing, and you can also seeds after the or slash glassblowing.
So glassblowing goes here.
Glass blowing.
Then once we have a subreddit, we want to iterate for posts in subreddits, that's new.
The new methods will give us the new posts in that subreddits.
So that will give us a list of posts.
Then we need to apply a condition, something like if a post is in the recent 24 hours, then we print
it out.
So before we apply that, we first need to get to the time, the current time and the time when a submission
was created.
So let's first give the current time current time a variable is equal to date time dot UTC.
Now we also have the now methods, but be aware that this method gives you the local time off your computer
or of the report server, but UTC will give you the UTC time in the UTC system.
So this is good.
So when you are doing comparisons between different times, as we are doing now, and of course, we
need to import from dates.
Time, imports date.
Time, so that goes here.
No, that's the current time.
And what is the.
Posts created time or post time that is post Dot.
Created so the post is one of the posts in the subreddits, one of these submissions, that's one it
will be this one also and also be aware that there are ads.
So this is an ad you see promoted here.
This will not be included in the list of submissions because it is not a submission.
It is in that.
So post time post creators, well, that's trying to print out current time and post time to see what
we actually get.
So around that?
And this is the ultimate so current time, is this one you?
And post time is this one.
So they are in a very different formats, four from each other.
So this is like a timestamp in seconds v seconds, ah, from 1st of January 1973.
So it's still a timestamp, but it's in seconds and it starts from a particular point in time.
We need to convert this to into a common format and then get the difference out of them.
So I think it makes more sense to convert this into data time to do that.
So that is the post time we get post creates.
It's of when we put that post created as inputs of date time dot UTC from time to time.
So that is a method of date time which gets seconds as inputs and in three turns this kind of formats.
So now if we run this again, we should see the same format.
So that is the current time, and that is the time when the post was created.
So there are many posts in here for each pair.
We need to calculate the Daily Star.
So the difference between the time where the post was created and the current time so that we check
if 24 hours have passed or not.
So let's say delta time is equal to current time minus post time.
And let's bring the notes.
So this is what we get, yeah, I start from.
Here, some of the most recent pause was made before four hours and 54 minutes, thirty four seconds,
then another post was submitted before eight hours and 25 minutes ago, more or less 10 hours 12.
And then we have one day and so one.
So let's filter them out by getting only reports that are earlier than 24 hours for that we needs and
if conditional, so if delta time is less or equal to 24 hours.
But be careful, how do you represent 24 hours?
You cannot just say 24 like that, right?
Because you want to compare a delta time type with a delta time type.
But this is not a delta time type.
This is an integer.
So what you want to do instead is you need the same type of object.
So for that, we need to import from the time we need time, they'll tell us when we can create a time
delta object, which has the same type as this object.
So this object is created implicitly through the difference of two different time stamps, but still,
you get the time delta from that difference.
So now we can see how worse equals twenty.
You see, this gets this whole worse argument, and it also has other arguments you can pass or if you
want.
It's a more accurate delta, such as 24 hours, one hour, two minutes and 30 seconds.
You could do that by adding arguments here like, I mean, it's equal to that and so on.
But for us, in this case, it's 24 hours.
So if that is the case, then print paused but paused, as you know, simply gives you that idea of
the post.
So perhaps you won the title and then you want self text the body of the post the content.
So we were on that.
But first, maybe you want to uncommon that there and to run again.
So this is what we get.
So we have one force here to in here.
Which corresponds with the.
So another one is the title of this.
And the title of that post.
So another one this on.
Now, if you want to save these into a list instead, you'd have to create an entire list before the
loop.
So like all outputs or just posts 24 posts like that, that is equal to an empty list of when instead
of printing these titles and self-tests out, you want to refer to posts.
24 hours does append and keep adding, perhaps pulse or post the title and post the self text.
And of course, you can keep adding information that you want to be included, like posts created.
And so on, and then you can print out the entire post 24 hours list and see what formats we get.
So there we go.
So the list starts here.
You see the square parentheses and then we have the title.
So the first table with the title of the first post, there's no content in that post, just a picture
and then you have the timestamp in seconds.
So if you want to convert that into a date time in human friendly date time, you want to apply again
that method to post created or just say here.
Post.
Time and run again.
So small tongue.
Oh, that's the date time.
And of course, you can also further convert it into a string using the SDR f time methods as a so won't
you see that we got these posts in a list?
And of course, you could go even further and store them in a text file?
And so we can do that by opening a text file with open outputs, the T60 in rights mode as a file,
and we indent all of this for a loop under the with context manager.
So for posting subreddits, if they also time I saw less than 24 hours, then we'd say under the conditional
here indented, we see file rights.
And three rides, a knife string, perhaps so, which has posted the title, a brake line post dot self.
Text, let me expand this.
Perhaps a double brake line of a van will store the next title and post, so we don't need to putting
that out this time we just run.
This is odd for the taxi.
And that's the results.
NATO, X-File.
So the title and it doesn't have any content.
Then we have the title of the next post, its contents in here.
The title of the other post and so on.
So that's the complete code for this lecture.
Thank you.
And I'll talk to you later.