-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsearch.py
68 lines (44 loc) · 1.66 KB
/
search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import datetime
import praw
import timeago
class Job():
def __init__(self,post):
self.title = post.title
self.date = compute_ago(post.created_utc)
self.url = post.url
def compute_ago(created):
date = datetime.datetime.fromtimestamp(int(float(str(created))))
now = datetime.datetime.now()
print (timeago.format(date, now))
return timeago.format(date, now)
client = "" # Client ID
secret = "" # Secret key
# Function to connect to Reddit API
def connect(cl_id, cl_secret):
return praw.Reddit(client_id = cl_id, client_secret = cl_secret, user_agent = "Job Finder by Hugeburger")
def search_reddit(query):
red = connect(client, secret)
# Generating the string for keywords
keys = query.split(",")
subs = ["jobbit", "forhire"]
# Generate string for keys
str_keys = "(title : \"" +keys[0]+ "\""
for k in keys[1:]:
str_keys = str_keys + " OR title:\"" + k + "\""
str_keys = str_keys + ")"
# Generate string for subs
str_subs = "(subreddit:hiring"
for sub in subs:
str_subs = str_subs + " OR subreddit:" + sub
str_subs = str_subs + ") "
# Creating the full search query using Reddit search syntax
full_str = "(title:\"hiring\" OR flair:Hiring) AND " + str_keys + " AND " + str_subs
# making the call
all = red.subreddit("all")
"""
for post in all.search(query = full_str, sort = "new"):
print post.title
post_date = datetime.datetime.utcfromtimestamp(post.created_utc)
print str(post_date.month) + ", " + str(post_date.day) + " in " + str(post_date.year)
"""
return all.search(query = full_str, sort = "new")