This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.py
91 lines (76 loc) · 2.63 KB
/
database.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/python
from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData ,text, BigInteger, Boolean
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
engine = create_engine("postgresql://jacob:jacob@localhost/jacob")
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()
class User(Base):
__tablename__ = "users"
id = Column(BigInteger, primary_key=True)
screen_name = Column(String(1000), nullable=False)
processed = Column(Boolean,nullable=False)
#User query functions
def add_user(id,name,processed):
new_user = User(id=id,screen_name=name,processed=processed)
session.add(new_user)
session.commit()
try:
pass
except:
session.rollback()
print "Error"
def get_users():
result = []
data = session.query(User).all()
for item in data:
result.extend([item.screen_name])
return result
def get_user_from_id(id):
result = session.query(User).filter_by(id=id).all()
return result
def get_user_from_screen_name(screen_name):
result = session.query(User).filter_by(screen_name=screen_name).all()
return result
Base.metadata.create_all(engine)
#Friend functions
def make_new_friend_table(screen_name):
metadata = MetaData(bind=engine)
user_table = Table(
"_" + screen_name + "_friends",
metadata,
Column("id", String(100), primary_key=True),
Column("screen_name", String(100), nullable=False)
)
metadata.create_all()
def get_friends(screen_name):
statement = text("select * from %s" % "_" + screen_name + "_friends")
result = engine.execute(statement)
data = []
for row in result:
data.append(row[1])
return data
def add_friend(table,id,screen_name):
statement = text("insert into %s VALUES ('%s','%s')" % ("_" + table + "_friends",id,screen_name))
result = engine.execute(statement)
#Follower functions
def make_new_follower_table(screen_name):
metadata = MetaData(bind=engine)
user_table = Table(
"_" + screen_name + "_followers",
metadata,
Column("id", String(100), primary_key=True),
Column("screen_name", String(100), nullable=False)
)
metadata.create_all()
def get_followers(screen_name):
statement = text("select * from %s" % "_" + screen_name + "_followers")
result = engine.execute(statement)
data = []
for row in result:
data.append(row[1])
return data
def add_follower(table,id,screen_name):
statement = text("insert into %s VALUES ('%s','%s')" % ("_" + table + "_followers",id,screen_name))
result = engine.execute(statement)