Skip to content

Commit

Permalink
Integrated LikedSavedDatabase
Browse files Browse the repository at this point in the history
* LikedSavedDatabase now records submissions and their associated
files after downloading. This will pave the way for more cool features
in the future
* Split up redditUserImageScraper main function to be a bit more readable
  • Loading branch information
makuto committed Jun 1, 2019
1 parent b82319d commit 0925b71
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 146 deletions.
30 changes: 28 additions & 2 deletions LikedSavedDatabase.py
Expand Up @@ -2,6 +2,9 @@
import sqlite3
import submission as Submissions

# Global database
db = None

class LikedSavedDatabase:
def __init__(self, databaseFilePath):
isNewDatabase = not os.path.exists(databaseFilePath)
Expand Down Expand Up @@ -46,6 +49,13 @@ def addSubmission(self, submission):
submission.getAsList())
self.save()

def addSubmissions(self, submissions):
cursor = self.dbConnection.cursor()

cursor.executemany("insert or ignore into Submissions values (NULL,?,?,?,?,?,?,?,?)",
Submissions.getAsList_generator(submissions))
self.save()

def printSubmissions(self):
cursor = self.dbConnection.cursor()

Expand Down Expand Up @@ -73,11 +83,23 @@ def addSubmissionToCollection(self, submissionId, collectionId):
(submissionId, collectionId))
self.save()

def associateFileToSubmission(self, filePath, submissionId):
def associateFileToSubmissionId(self, filePath, submissionId):
cursor = self.dbConnection.cursor()
cursor.execute("insert or ignore into FilesToSubmissions values (?,?)",
(filePath, submissionId))
self.save()

def associateFileToSubmission(self, filePath, submission):
cursor = self.dbConnection.cursor()
cursor.execute("select * from Submissions where postUrl=?", (submission.postUrl,))
submissionInDb = cursor.fetchone()
if submissionInDb:
submissionId = submissionInDb[0]
cursor.execute("insert or ignore into FilesToSubmissions values (?,?)",
(filePath, submissionId))
self.save()
else:
print("DB error: couldn't find submission from post URL {}".format(submission.postUrl))

def getAllSubmissionsInCollection(self, collectionId):
cursor = self.dbConnection.cursor()
Expand Down Expand Up @@ -126,10 +148,14 @@ def testOnRealSubmissions():
print("Couldn't find {}".format(title))
else:
db.addSubmissionToCollection(dbSubmission[0], dbCollection[0])
db.associateFileToSubmission("{}.png".format(title), dbSubmission[0])
db.associateFileToSubmissionId("{}.png".format(title), dbSubmission[0])

print(db.getAllSubmissionsInCollection(dbCollection[0]))
print(db.getAllFiles())

def initializeFromSettings(userSettings):
global db
db = LikedSavedDatabase(userSettings['Database'])

if __name__ == '__main__':
#testDatabase()
Expand Down
12 changes: 4 additions & 8 deletions LikedSavedDownloaderServer.py
Expand Up @@ -12,7 +12,7 @@
import json
import multiprocessing

from utilities import sort_naturally
import utilities
import settings
import redditUserImageScraper

Expand Down Expand Up @@ -44,10 +44,6 @@ def generateSavedImagesCache(outputDir):

print('Finished creating Liked Saved cache ({} images/videos)'.format(len(savedImagesCache)))

def outputPathToServerPath(path):
# This is a little weird
return 'output' + path.split(settings.settings['Output_dir'])[1]

def getRandomImage(filteredImagesCache=None, randomImageFilter=''):
if not savedImagesCache:
generateSavedImagesCache(settings.settings['Output_dir'])
Expand All @@ -59,7 +55,7 @@ def getRandomImage(filteredImagesCache=None, randomImageFilter=''):

print('\tgetRandomImage(): Chose random image {} (filter {})'.format(randomImage, randomImageFilter))

serverPath = outputPathToServerPath(randomImage)
serverPath = utilities.outputPathToServerPath(randomImage)

return randomImage, serverPath

Expand Down Expand Up @@ -377,7 +373,7 @@ def on_message(self, message):
for file in files:
if file.endswith(supportedExtensions):
imagesInFolder.append(os.path.join(root, file))
sort_naturally(imagesInFolder)
utilities.sort_naturally(imagesInFolder)
currentImageIndex = imagesInFolder.index(fullImagePath)
if currentImageIndex >= 0:
action = 'setImage'
Expand All @@ -389,7 +385,7 @@ def on_message(self, message):
nextImageIndex = len(imagesInFolder) - 1

fullImagePath = imagesInFolder[nextImageIndex]
serverImagePath = outputPathToServerPath(fullImagePath)
serverImagePath = utilities.outputPathToServerPath(fullImagePath)

if command == 'setFilter':
newFilter = parsedMessage['filter']
Expand Down
15 changes: 11 additions & 4 deletions imageSaver.py
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-

import LikedSavedDatabase
import imgurpython as imgur
import logger
import os
Expand Down Expand Up @@ -304,8 +305,9 @@ def saveAllImgurAlbums(outputDir, imgurAuth, subredditAlbums, soft_retrieve_imgs

numAlbums = len(albums)
for albumIndex, album in enumerate(albums):
albumTitle = album[0]
albumUrl = cleanImgurAlbumUrl(album[1])
albumSubmission = album[0]
albumTitle = album[1]
albumUrl = cleanImgurAlbumUrl(album[2])
logger.log('\t[' + percentageComplete(albumIndex, numAlbums) + '] '
+ '\t' + albumTitle + ' (' + albumUrl + ')')

Expand Down Expand Up @@ -358,6 +360,8 @@ def saveAllImgurAlbums(outputDir, imgurAuth, subredditAlbums, soft_retrieve_imgs
if not soft_retrieve_imgs:
# Retrieve the image and save it
urlretrieve(imageUrl, saveFilePath)
LikedSavedDatabase.db.associateFileToSubmission(
utilities.outputPathToDatabasePath(saveFilePath), albumSubmission)

logger.log('\t\t[' + percentageComplete(imageIndex, numImages) + '] '
+ ' [save] ' + imageUrl + ' saved to "' + saveAlbumPath + '"')
Expand Down Expand Up @@ -425,9 +429,9 @@ def saveAllImages(outputDir, submissions, imgur_auth = None, only_download_album
else:
# We're going to save Imgur Albums at a separate stage
if subredditDir in imgurAlbumsToSave:
imgurAlbumsToSave[subredditDir].append((submissionTitle, url))
imgurAlbumsToSave[subredditDir].append((submission, submissionTitle, url))
else:
imgurAlbumsToSave[subredditDir] = [(submissionTitle, url)]
imgurAlbumsToSave[subredditDir] = [(submission, submissionTitle, url)]
continue
elif only_download_albums:
continue
Expand Down Expand Up @@ -497,6 +501,9 @@ def saveAllImages(outputDir, submissions, imgur_auth = None, only_download_album
# Retrieve the image and save it
try:
urlretrieve(url, saveFilePath)

LikedSavedDatabase.db.associateFileToSubmission(
utilities.outputPathToDatabasePath(saveFilePath), submission)
except IOError as e:
logger.log('[ERROR] IOError: Url {0} raised exception:\n\t{1} {2}'
.format(url, e.errno, e.strerror))
Expand Down

0 comments on commit 0925b71

Please sign in to comment.