chevron_left chevron_right
Login Register invert_colors photo_library


Stay updated and chat with others! - Join the Discord!
Thread Rating:
  • 0 Vote(s) - 0 Average


Reddit User Information (Python 3.3.x) filter_list
Author
Message
Reddit User Information (Python 3.3.x) #1
Imports:
Code:
import re
from urllib import parse, request, error

Function:
Code:
def reddit_info(user):
    try:
        aurl = "http://www.reddit.com/user/" + user
        data = str(request.urlopen(aurl).read(), encoding="utf-8")
        trophies_matches = re.findall("trophy-name", data)
        trophies = []
        num = len(trophies_matches)
        if num == 0:
            trophies.append("None")
        else:
            for i in range(1, num + 1):
                trophies.append(data.split("trophy-name\">")[i].split("<")[0])
        output = "Submit Karma:\t" + data.split("<span class=\"karma\">")[1].split("<")[0] + "\r\n"
        output += "Comment Karma:\t" + data.split("<span class=\"karma comment-karma\">")[1].split("<")[0] +"\r\n"
        output += "Trophies:\r\n"
        for t in trophies:
            output += " - " + t + "\r\n"
        return output
    except Exception as e:
        return str(e)
Excuse my mixing of string manipulation and regular expression use (regarding the re.findall usage), I had originally started writing this with Regex but realized it wouldn't work, so I changed it.

Example Usage:
Code:
print(reddit_info("_vargas_"))

Example Output:
Code:
Submit Karma:    34,160
Comment Karma:    1,683,990
Trophies:
- Gilding II
- Three-Year Club
- ComboCommenter
- Team Periwinkle
- reddit gold
- Verified Email
- ComboCommenter
- ComboCommenter
- ComboCommenter
[Image: CDUAq9d.png]

Reply

RE: Reddit User Information (Python 3.3.x) #2
There's some pretty sweet python wrappers for reddit like https://praw.readthedocs.org/en/v2.1.16/
#MakeSinisterlySexyAgain

Reply

RE: Reddit User Information (Python 3.3.x) #3
(06-18-2014, 05:03 PM)Adorapuff Wrote: There's some pretty sweet python wrappers for reddit like https://praw.readthedocs.org/en/v2.1.16/

I've used PRAW before, but I wanted to try doing this myself (it's actually part of my summative assignment for my AP CompSci course in school) since the whole app isn't based around Reddit-related tools.
[Image: CDUAq9d.png]

Reply

Reddit User Information (Python 3.3.x) #4
(06-18-2014, 05:26 PM).Shebang Wrote: I've used PRAW before, but I wanted to try doing this myself (it's actually part of my summative assignment for my AP CompSci course in school) since the whole app isn't based around Reddit-related tools.

The ap comp sci I take next year is all java from what I heard.
#MakeSinisterlySexyAgain

Reply

RE: Reddit User Information (Python 3.3.x) #5
(06-18-2014, 06:48 PM)Adorapuff Wrote: The ap comp sci I take next year is all java from what I heard.

Mine was as well, but my teacher basically said "You have 2 weeks to learn a new language. Tell me what you want to learn and what you'll make and hand it in".
[Image: CDUAq9d.png]

Reply






Users browsing this thread: 1 Guest(s)