Sinisterly
Reddit User Information (Python 3.3.x) - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: Python (https://sinister.li/Forum-Python)
+--- Thread: Reddit User Information (Python 3.3.x) (/Thread-Reddit-User-Information-Python-3-3-x)



Reddit User Information (Python 3.3.x) - Shebang - 06-18-2014

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



RE: Reddit User Information (Python 3.3.x) - Adorapuff - 06-18-2014

There's some pretty sweet python wrappers for reddit like https://praw.readthedocs.org/en/v2.1.16/


RE: Reddit User Information (Python 3.3.x) - Shebang - 06-18-2014

(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.


Reddit User Information (Python 3.3.x) - Adorapuff - 06-18-2014

(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.


RE: Reddit User Information (Python 3.3.x) - Shebang - 06-18-2014

(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".