Last active
August 29, 2015 14:22
-
-
Save ZiTAL/ad2818a61184dfcc53d2 to your computer and use it in GitHub Desktop.
python: Twitterrera argazki aleatoidxuek idxeteko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
# -*- coding: utf-8 -*- | |
# | |
# pip install TwitterAPI | |
from TwitterAPI import TwitterAPI | |
from os import path | |
from os import listdir | |
from random import randint | |
from sys import exit | |
# twitterren APIdxen informaziñue https://apps.twitter.com/ | |
CONSUMER_KEY = '***************************' | |
CONSUMER_SECRET = '***************************' | |
ACCESS_TOKEN_KEY = '***************************' | |
ACCESS_TOKEN_SECRET = '***************************' | |
# ze motatako irudidxek okinguz kontutan | |
allow_extension = ['.jpg', '.png'] | |
# irudiaren path-a hartu | |
image_path = path.dirname(path.realpath(__file__))+"/images" | |
if path.isdir(image_path): | |
files = listdir(image_path) | |
else: | |
print "Karpeta "+image_path+" ez da existitzen" | |
exit() | |
# irudi danak images dict-en sartun | |
images = {} | |
j = 0 | |
for i in files: | |
file_path = path.join(image_path, i) | |
if path.isfile(file_path): | |
extension = path.splitext(file_path)[1] | |
if extension.lower() in allow_extension: | |
images[j] = file_path | |
j = j+1 | |
# ikusi ia baten bat badauen | |
len = len(images.keys()) | |
if len>0: | |
r = randint(0, j-1) | |
else: | |
print "Ez da irudirik aurkitu "+image_path+" karpetan" | |
exit() | |
# twitterrera argazkidxe idxen | |
api = TwitterAPI(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET) | |
file = open(images[r], 'rb') | |
data = file.read() | |
r = api.request('statuses/update_with_media', {'status':'#zitalbot'}, {'media[]':data}) | |
if r.status_code == 200: | |
print "Txioa ondo bidali da ;)" | |
else: | |
print "Errorea txioa bidaltzerakoan status code: "+r.status_code | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment