Skip to content

Instantly share code, notes, and snippets.

@ap-Codkelden
Created December 12, 2014 18:47
Show Gist options
  • Save ap-Codkelden/dc92bc72008c14c5775e to your computer and use it in GitHub Desktop.
Save ap-Codkelden/dc92bc72008c14c5775e to your computer and use it in GitHub Desktop.
Point Gpah (unfinished)
#!/usr/bin/python
import argparse
import requests
import json
import time
import sqlite3
"""
Подписки пользователя
Запрос
GET /api/user/<login>/subscriptions
Ответ
Пример запроса:
http://point.im/api/user/skobkin-ru/subscriptions
Пример ответа:
[
{
"login": "al1k",
"id": 171,
"avatar": "al1k.jpg?r=8993",
"name": "al1k"
},
...
]
sqlite3.IntegrityError: UNIQUE constraint failed: users.login - один. логины
sqlite3.IntegrityError: UNIQUE constraint failed: users.id - повтор
"""
class Error(Exception):
pass
class PointAPIError(Error):
def __init__(self, code):
self.message = "Error occured with HTTP status code {0}".format(code)
def __str__(self):
return self.message
class Sqlite3IntegrityError(Error):
def __init__(self, message):
print(message)
class PointAPI(object):
def __init__(self, user, password):
self.API='http://point.im/api/'
self.user = user
self.password = password
self.session = requests.Session()
self.cookie = None
def login(self):
login_values = {'login' : self.user, 'password' : self.password}
req = requests.request('POST', "https://point.im/api/login", data=login_values)
print(req)
self.cookie = {'user': req.json()['token']}
print(self.cookie)
def api_request(self, url, cookies=None):
if not cookies:
cookies = self.cookie
return requests.request('GET', url=url, cookies=cookies).json()
def GetUser(self, user_login):
try:
url = '{0}user/{1}'.format(self.API, user_login)
print("URL: ", url)
response = self.api_request(url)
if 'code' in response:
raise PointAPIError(response)
print('response: ',[ response ])
return [ response ]
except:
raise
def GetFriends(self, user_login):
try:
url = '{0}user/{1}/subscriptions'.format(self.API, user_login)
print("URL: ", url)
response = self.api_request(url)
if 'code' in response:
raise PointAPIError(response)
return response
except:
raise
class DataBase(object):
def __init__(self):
self.connection = sqlite3.connect('data.db')
self.cursor = self.connection.cursor()
self.CreateDB()
def Query(self, query):
try:
self.cursor.execute(query)
return self.cursor.fetchone()
except:
raise
def InsertUser(self, id, login):
try:
self.Query(query = "INSERT INTO users VALUES ({0}, '{1}', {2});".format(id, login, 0))
self.connection.commit()
return 0
except sqlite3.IntegrityError as e:
#raise Sqlite3IntegrityError(e.message)
print("User @{0} already in DB, skip.".format(login))
pass
return 1
except:
raise
def MarkAsProcessed(self, id):
self.Query(query = "UPDATE users SET processed = 1 WHERE id = {0};".format(id))
self.connection.commit()
def GetMark(self, id):
r = self.Query(query = "SELECT processed FROM users WHERE id = {0};".format(id))
print("Mark is: ", r)
if not r:
return 0
else:
return(r[0])
def InsertFriend(self, id_user, friend_id, friend_login):
try:
self.Query(query = "INSERT INTO friends VALUES ({0}, {1}, '{2}');".format(id_user, friend_id, friend_login))
self.connection.commit()
except sqlite3.IntegrityError as e:
raise Sqlite3IntegrityError(e.message)
except:
raise
def CreateDB(self):
sql_string = """CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, login TEXT UNIQUE, processed INTEGER);"""
self.Query(sql_string)
sql_string = """CREATE TABLE IF NOT EXISTS friends (user INTEGER, friend_id INTEGER, friend_login TEXT, FOREIGN KEY(user) REFERENCES users(id));"""
self.Query(sql_string)
self.connection.commit()
if __name__ == "__main__":
depth = 3
user_login = 'ap-Codkelden'
user_pass = 'password'
p = PointAPI(user_login,user_pass)
p.login()
d=DataBase()
p_user = p.GetUser(user_login)
while depth >= 0 :
for u in p_user:
c = d.GetMark(u['id'])
print("Mark is: ", c)
if c == 0:
#time.sleep(0.25)
d.InsertUser(u['id'], u['login'])
print("Got user {0}".format(u['login']))
print("INFO: depth level is {0}".format(depth))
p_fr = p.GetFriends( u['login'])
friends_count = initial_friends_count = len(p_fr)
print("Got friends of {0}".format(u['login']))
for z in p_fr:
time.sleep(0.25)
d.InsertUser(z['id'], z['login'])
friends_count -= 1
d.InsertFriend(u['id'], z['id'], z['login'])
print("@{0} inserted, {1} items of {2} left.".format(z['login'], friends_count, initial_friends_count))
d.MarkAsProcessed(u['id'])
else:
print(">>> Subscriptions of @{0} already in database - skip. <<<".format(u['login']))
p_user = p_fr
depth -= 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment