0

This is a twitter image bot that is called on every two hours to post a picture from a folder, the files are numbered consecutively and the current number is stored in a text file so it can stay constant between the runs. The image file types vary between .jpg and .gif and i dont know how to account for this in the picture() function of my code.

import os
from twython import Twython 
from twython import TwythonStreamer

APP_KEY = ''
APP_SECRET = ''
OAUTH_TOKEN = ''
OAUTH_TOKEN_SECRET = ''

f = open('pictures.txt', 'r+')
z = f.read()

def picture():
    picture = open('/0/' + 'picture' + str(z))
    f.write(str(z)+'\n')
    global z
    z += 1
    promote(picture)
    f.write(z)
    f.close


def promote(photo):
    twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
    twitter.update_status_with_media(status='', media=photo)

picture()
  • What error message does it give when you run it. Add that bit of information as well – Nobi Jun 28 '14 at 13:21
  • @cmrnrb - did you read [this answer](http://stackoverflow.com/a/24462987/21945) to your previous, extremely similar, question? – mhawke Jun 28 '14 at 13:32

1 Answers1

0

Since your previous question has been put on hold, I post this answer again.

Use glob to find files matching a prefix, imghdr to check the file type (twitter doesn't support all image files), and make sure that you convert the image sequence number to an int when you read it in, and to a string when you update the file. The file update requires seeking to the beginning of the file first, and this assumes that the sequence number will always increase.

import imghdr
from glob import glob

SUPPORTED_IMG_TYPES = 'gif jpeg png'.split()
IMG_SEQ_FILE = '/0/pictures.txt'
GLOB_PATTERN = '/0/picture%d.*'

def send_to_twitter(filename):
    print "sent %s to twitter" % filename
    return True

with open(IMG_SEQ_FILE, 'r+') as f:
    seq = int(f.readline().strip())
    for name in glob(GLOB_PATTERN % seq):
        img_type = imghdr.what(name)
        if img_type in SUPPORTED_IMG_TYPES:
            if send_to_twitter(name):
                f.seek(0)
                seq += 1
                f.write(str(seq))
                break
        else:
            if not img_type:
               print "%s is not an image file" % name
            else:
               print "%s unsupported image type: %s" % (name, img_type)

All you need to do is add your code to send the image file data to twitter.

mhawke
  • 84,695
  • 9
  • 117
  • 138
  • when i run this nothing past for name in glob seems to run, nothing is printed and the text file isnt updated at all? thank you –  Jun 29 '14 at 16:34
  • Are there any files matching the glob pattern in /0, e.g is there a file named "picture1.jpg"? Have you initialised the sequence file, and is it in /0/pictures.txt? – mhawke Jun 30 '14 at 02:32
  • yeah i have the pictures and i have the pictures file which has only the number '0' in it –  Jun 30 '14 at 10:48
  • Is there a file named `/0/picture0.jpg`, or `/0/picture0.gif` etc? If so, does your application have permission to read the file? – mhawke Jun 30 '14 at 11:50
  • yeah those files are in there and it was able to read and write to the file before when i had some other code –  Jun 30 '14 at 14:08
  • Try adding some `print` statements to your code, e.g. after `seq` is read from the file, and also print out the value of `glob(GLOB_PATTERN % seq)` before the for loop starts – mhawke Jun 30 '14 at 14:39
  • yeah i tried that earlier, it could print the seq and it could print the file path but it cant print anything further, like the image type –  Jun 30 '14 at 15:08
  • Well. what was the result of `print glob(GLOB_PATTERN % seq)` ? – mhawke Jul 01 '14 at 00:11
  • when i print `seq` it prints 0, when i print `(GLOB_PATTERN % seq)` it prints `/0/picture0.*` but when i print `glob(GLOB_PATTERN % seq)` it prints `[]` –  Jul 01 '14 at 11:00
  • What is the output of `ls /0/picture0.*` ? How about `ls /0`? Are you sure that you have read access to those files for the user that is executing your script? – mhawke Jul 01 '14 at 11:14