-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreddit_autopost.py
More file actions
66 lines (58 loc) · 1.87 KB
/
reddit_autopost.py
File metadata and controls
66 lines (58 loc) · 1.87 KB
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
57
58
59
60
61
62
63
64
65
66
import sqlite3
import os
import praw
_useragent = 'Autoposter v0.1 (/u/xoran99)'
class Controller:
_conn = None
_path = None
_sessions = dict()
def __init__(self, path='~/.reddit_autopost.db'):
self._path = os.path.expanduser(path)
def conn(self):
if self._conn == None:
self._conn = sqlite3.connect(path)
return self._conn
def initialize_database(self, path):
"""Create table with appropriate schema in path"""
self.conn().executescript("""
CREATE TABLE config
(
key TEXT,
value TEXT
);
CREATE TABLE posts
(
postid INTEGER PRIMARY KEY AUTOINCREMENT,
poster TEXT NOT NULL,
subreddit TEXT NOT NULL,
datetime TEXT NOT NULL,
selfpost INTEGER NOT NULL DEFAULT 0,
title TEXT NOT NULL,
content TEXT NOT NULL,
time_posted TEXT DEFAULT NULL,
FOREIGN KEY (poster) REFERENCES user(username)
);
CREATE TABLE users
(
username TEXT NOT NULL,
password TEXT NOT NULL
);
INSERT INTO config VALUES ('Database Version', '2013112700');
""")
self.conn().commit()
def makepost(self, username, password, subreddit, title, content, is_selfpost):
if username in self._sessions:
r = self._sessions[username]
else:
r = praw.Reddit(_useragent)
r.login(username, password)
self._sessions[username] = r
url = content
if is_selfpost:
text = content
url = None
else:
url = content
text = None
r.submit(subreddit, title, text, url)
# vim: set sw=4 ts=4 expandtab: