5

I'm using PRAW to create a Reddit bot that submits something once a day. After submitting I want to save the url of the submission and write it to a text file.

url = r.submit(subreddit, submission_title, text=submission_text)

The above returns a Submission object, but I want the actual url. Is there a way to get the url from a Submission object, or do I need to do something else to get the url?

Amos
  • 1,154
  • 1
  • 16
  • 35

2 Answers2

7

submission.shortlink (previously .short_link) is what you're looking for, if submission.permalink wasn't good enough.

reddit = praw.Reddit("Amos")
submission = reddit.get_submission(submission_id="XYZ")
print submission.permalink
>>> www.reddit.com/r/subreddit/comments/XYZ
TankorSmash
  • 12,186
  • 6
  • 68
  • 106
6

I see that @TankorSmash has answered your question already, though I thought I might add some fundamental knowledge for future references:

If you use "dir(object)," you'll be able to see both attributes and methods that pertain to the Reddit API (which you may use to test and see all properties that effect the given object being tested). You can ignore everything that starts with an underscore (most likely).

An example would be:

submissionURL = submission.url

Or you can go straight to source where PRAW is getting its data. The variable names are not set by PRAW, they come from this JSON (linked above).

Saroekin
  • 1,175
  • 1
  • 7
  • 20
  • 1
    Hey, I know this is an old thread but I used this and for submissions with images I got an image link and for text based ones I got the comments link, could you please explain how this works.? – Vinay Bharadhwaj Feb 14 '19 at 07:39