generated from davidstanke/instapuller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
154 lines (121 loc) · 4.76 KB
/
app.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import config
import json
import logging
import os
import requests
import sqlalchemy
import random
import time
from bs4 import BeautifulSoup
from flask import Flask, request, render_template, flash, redirect, url_for
# from google.cloud import pubsub_v1
from sqlalchemy import create_engine, exc, Table, Column, Integer, String, MetaData, ForeignKey, Sequence, BigInteger, DATETIME, func
from sqlalchemy.orm import sessionmaker
from pymysql.err import IntegrityError
# Classes for this application
from models import Post, Media, Base
# Setup Flask Web App
app = Flask(__name__)
app.secret_key = "replace_this_if_you_care"
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger()
URL = 'http://imginn.com/'
# make a session factory
Session = sessionmaker(bind=config.db)
# project_id = "serverless-ux-playground"
# topic_name = "instapuller-media-download-request"
# publisher = pubsub_v1.PublisherClient()
# topic_path = publisher.topic_path(project_id, topic_name)
@app.route('/')
def displayPosts():
session = Session()
posts = session.query(Post).order_by(Post.date_added.desc())
return render_template('index.html', data=posts)
@app.route('/addUser')
def processPosts():
username = request.args.get('username')
if (username == None):
return_string = "dude, you gotta provide a username"
else:
headers = {'user-agent': 'my-app/0.0.1'}
page = requests.get(URL + username, headers=headers)
if (page.status_code == 200):
soup = BeautifulSoup(page.content, 'html.parser')
items = soup.find_all('div', class_='item')
collection = getPosts(items, username)
# commit to DB
session = Session()
for post in collection:
logger.debug(post.shortcode)
# check if post id already exists
post_record = session.query(Post).filter(Post.post_id == post.post_id)
if(post_record.count()):
logger.info("post already exits: " + str(post.post_id))
else:
session.add(post)
# dispatchMediaDownloadRequest(post)
session.commit()
return_string = (
"added Instgram user: " + username
+ " (" + str(len(collection)) + " items)")
else:
return_string = (
"Problem retrieving results: "
+ URL + username + " returns "
+ str(page.status_code) + " " + str(page.reason))
flash(return_string)
return redirect(url_for('displayPosts'))
def dispatchMediaDownloadRequest(post):
# Dispatch media request to pubsub topic
# data must be a bytestring.
logger.info("Dispatch request to pubsub: %s", post)
# publisher.publish(topic_path, data=(json.dumps(post)).encode("utf-8"))
def convertShortCodeToPostID(shortcode):
return_code = []
for char in shortcode:
return_code.append(str(ord(char)))
return ''.join(map(str, return_code[:7]))
def getPosts(postList, username):
postCollection = []
for post in postList:
thisPost = Post()
thisPost.username = username
thisPost.shortcode = post.find('a')['href'][3:-1]
thisPost.direct_link = "https://www.instagram.com/p/" + thisPost.shortcode
try:
thisPost.caption = post.find('img')["alt"]
except:
thisPost.caption = ""
thisPost.display_url = post.find_all('a')[1]['href']
thisPost.thumbnail_src = post.find('img')["data-src"]
thisPost.post_id = convertShortCodeToPostID(thisPost.shortcode)
postCollection.append(thisPost)
return postCollection
@app.route('/stats')
def showStats():
session = Session()
result = session.query(Post.username, func.count(Post.post_id)).group_by(Post.username).all()
stats = []
for row in result:
stats.append((row[0], row[1]))
return render_template('stats.html', rows=stats)
@app.route('/usernames')
def get_usernames():
session = Session()
result = session.query(Post.username).group_by(Post.username).order_by(Post.username)
users = []
for row in result:
users.append(row[0])
return json.dumps(users)
@app.route('/purgeall')
def purge_all():
session = Session()
posts = session.query(Post).delete()
media = session.query(Media).delete()
session.commit()
flash('All gone! Deleted ' + str(posts) + ' posts and ' + str(media) + ' media entries.')
return redirect(url_for('displayPosts'))
if __name__ == "__main__":
# create initial database tables (if not already present)
Base.metadata.create_all(config.db)
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))