lecture11_practice

.html

School

University of California, Davis *

*We aren’t endorsed by this school

Course

1

Subject

Computer Science

Date

Feb 20, 2024

Type

html

Pages

4

Uploaded by DukeFly4004

Report
Practice: Find the most viral story The code below copies the code from today's demo. Log into Reddit (PRAW) Our code starts with our Redddit PRAW setup: In [13]: import praw import datetime In [14]: %run reddit_keys.py In [15]: # Give the praw code your reddit account info so # it can perform reddit actions reddit = praw.Reddit( username=username, password=password, client_id=client_id, client_secret=client_secret, user_agent="a custom python script for user /" + str(username) ) print(reddit.read_only) reddit.read_only = True False Try to find the most viral story you can! In the code below, we're checking the "news" subreddit, for the top 10 ("limit=10") most hot (".hot") stories. But, we don't need to just do current hot stories; we have options! By replacing the hot() function with top(), rising(), or controversial() functions, we can find the highest ranked posts (top), the posts that are gaining upvotes most quickly (rising), or the ones with the worst upvote/downvote ratios (controversial). These are all just different algorithms reddit uses to serve posts (more on that in the next lecture). You also have options. The "limit" parameter tells the function how many posts to give us. We can also us the "time_filter" parameter, which accepts a string, to tell us what time period we want posts from. For example, if we want the most 5 controversial posts of the year, we can do messages = reddit.subreddit("controversial").top(limit=5, time_filter="year"). The options for time_filter are "day", "week", "month", "year", and "all". Modify the code below, and try to find the most viral news story you can! In [16]: # Look up the subreddit "cuteanimals", then find the "hot" list, getting up to 1 submission messages = reddit.subreddit("news").hot(limit=10) # get the first submission off the list (should only be one anyway) messages_list = list(messages) for submission in messages_list: # display the subject and body of the message, so we can see what we found display("latest message subject: " + str(submission.title)) display("latest message body: " + str(submission.url)) display("latest message time: " +
str(datetime.datetime.fromtimestamp(submission.created_utc))) duplicate_submissions = list(submission.duplicates(params={'crossposts_only': True, 'limit': 100})) ordered_duplicate_submissions = sorted( duplicate_submissions, key=lambda x: x.created_utc ) print("duplicates:") for duplicate_submission in ordered_duplicate_submissions: print(" subreddit: " + str(duplicate_submission.subreddit)) print() 'latest message subject: FCC declares AI-generated voices in robocalls are illegal' 'latest message body: https://www.cbsnews.com/news/fcc-declares-robocalls-illegal/' 'latest message time: 2024-02-08 16:15:45' duplicates: subreddit: u_InterestingMemory325 'latest message subject: US court bans three weedkillers and finds EPA broke law in approval process' 'latest message body: https://www.theguardian.com/environment/2024/feb/07/us- weedkiller-ban-dicamba-epa' 'latest message time: 2024-02-08 17:29:36' duplicates: subreddit: organic subreddit: richguysayswords subreddit: sdrawkcabtidder "latest message subject: Sheriff: Camp not fully cooperating after apparent 'suspicious' death of 12-year-old" 'latest message body: https://wlos.com/news/local/trails-carolina-camp-not-fully- cooperating-after-suspicious-death-of-12-year-old-boy-on-premesis-transylvania-sheriff- chuck-owenby' 'latest message time: 2024-02-08 12:28:52' duplicates: subreddit: NorthCarolina subreddit: sdrawkcabtidder 'latest message subject: 5 Marines aboard helicopter that went down outside San Diego are confirmed dead, military says' 'latest message body: https://apnews.com/article/marine-helicopter-found-san-diego- mountains-a66c3e8565204c43a189301015ef41a6? taid=65c4d18639616d00012c6c33&utm_campaign=TrueAnthem&utm_medium=AP&utm_source=Twitter' 'latest message time: 2024-02-08 13:18:39' duplicates: subreddit: PerpetualPixelNews 'latest message subject: Alabama station in disbelief after 200-foot radio tower stolen' 'latest message body: https://www.nbcnews.com/news/us-news/alabama-station-disbelief-
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help