Skip to content

Instantly share code, notes, and snippets.

@elfsternberg
Created December 13, 2018 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elfsternberg/a88d5225578d9543392dcb35a369b432 to your computer and use it in GitHub Desktop.
Save elfsternberg/a88d5225578d9543392dcb35a369b432 to your computer and use it in GitHub Desktop.
Pick a few random images from Tumblr based on the day they were posted
#!/usr/bin/env python3
# TUMBLON
#
# With Tumblr shutting down, I wanted to at least keep some semblance
# of the experience of Tumblr around. Since I was able to download
# all of my tumblr blogs into separate local repos thanks to
# Beat Bolli's wonderful tumblr_backup tool
# (https://github.com/bbolli/tumblr-utils/blob/master/tumblr_backup.md),
# I just wanted it to spit out a few random images everyday I could
# feed into an image viewer.
#
# It's mostly a nostalgia tool. But it's straightforward and reliable.
import os, re, random, itertools
def get_files():
return [i for i in os.listdir(".") if i.find('tumblr_') == 0]
def get_prefix(files):
return random.choice(list(set([i[0:10] for i in files])))
def get_initial_list(files, prefix):
return [i for i in files if i[0:10] == prefix]
m_re = re.compile(r'(\d+)\.(jpg|png|gif)')
def isort(images):
def getsize(i):
m = m_re.search(i)
if m:
return -1 * int(m.group(1), 10)
return 0
return sorted(images, key=getsize)
def group_list(files):
def key(f): return '_'.join(f.split('_')[0:2])
return [isort(list(i[1]))[0] for i in (itertools.groupby(files, key))]
files = get_files()
print("\n".join(group_list(get_initial_list(files, get_prefix(files)))))
# COPYRIGHT:
# This program is copyrighted (c) 2018
# Elf M. Sternberg (elf.sternberg@gmail.com)
#
# LICENSE:
# https://creativecommons.org/licenses/by/4.0/
#
# This is free software released under the CC-BY license. Users of
# this software enjoy the following rights and responsibilities:
#
# Share — You may copy and redistribute the material in any medium or format
#
# Adapt — You may remix, transform, and build upon the material for
# any purpose, even commercially.
#
# Attribution — You must give appropriate credit, provide a link to
# the license, and indicate if changes were made. You may do so in any
# reasonable manner, but not in any way that suggests the licensor
# endorses you or your use.
#
# You may not employ technological measures or legal terms that
# legally prevent others from doing anything the license permits. THE
# SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment