# -*- coding: utf-8 -*-
import sys
import configparser, os
from pathlib import Path
import sched
import time
import twitter

MEDIA_PATH = '.'
SCHED = sched.scheduler(time.time, time.sleep)
SECS = 3

def _main(argv):
    cfg_file = Path("twitter.cfg")
    if not cfg_file.is_file():
        ask_config()
    api = read_config()
    check_for_media(api)

def check_for_media(api):
    print('Checking for media in %s... (every %d secs)' % (MEDIA_PATH, SECS))
    for file in os.listdir(MEDIA_PATH):
        fname, fext = os.path.splitext(file)
        if fext in ('.jpg', '.jpeg', '.gif', '.png', '.mp4'):
            print('%s found, posting on twitter...' % file)
            status = post_media(os.path.join(MEDIA_PATH, file), api)
            print('I posted %s' % status.text)
            media_archive(file)
    SCHED.enter(SECS, 1, check_for_media, (api, ))
    SCHED.run()

def media_archive(file):
    if not os.path.exists(os.path.join(MEDIA_PATH, 'hist\\')): #TODO: history folder must be configurable?
        os.makedirs(os.path.join(MEDIA_PATH, 'hist\\'))
    os.rename(os.path.join(MEDIA_PATH, file), os.path.join(MEDIA_PATH, 'hist\\' + file))

def post_media(media, api):
    return api.PostUpdate('Ignore this tweet', media) #TODO: add logic for the tweet text

def ask_config():
    consumer_key = input('Consumer Key:')
    consumer_secret = input('Consumer Secret: ')
    access_token_key = input('Access Token Key: ')
    access_token_secret = input('Access Token Secret: ')
    global MEDIA_PATH
    global SECS
    MEDIA_PATH = input('Media Path: ')
    SECS = input('Check media path every N seconds: ')
    save_config(consumer_key, consumer_secret, access_token_key, access_token_secret)

def save_config(consumer_key, consumer_secret, access_token_key, access_token_secret):
    config = configparser.RawConfigParser()
    config.add_section('Main')
    config.set('Main', 'consumer_key', consumer_key)
    config.set('Main', 'consumer_secret', consumer_secret)
    config.set('Main', 'access_token_key', access_token_key)
    config.set('Main', 'access_token_secret', access_token_secret)
    config.set('Main', 'media_path', MEDIA_PATH)
    config.set('Main', 'secs', SECS)
    with open('twitter.cfg', 'w') as configfile:
        config.write(configfile)

def read_config():
    config = configparser.ConfigParser()
    _consumer_key = ''
    _consumer_secret = ''
    _access_token_key = ''
    _access_token_secret = ''

    if config.read('twitter.cfg'):
        _consumer_key = config.get('Main', 'consumer_key')
        _consumer_secret = config.get('Main', 'consumer_secret')
        _access_token_key = config.get('Main', 'access_token_key')
        _access_token_secret = config.get('Main', 'access_token_secret')
        global MEDIA_PATH
        global SECS
        MEDIA_PATH = config.get('Main', 'media_path')
        SECS = float(config.get('Main', 'secs'))

    return twitter.Api(consumer_key = _consumer_key,
                      consumer_secret = _consumer_secret,
                      access_token_key = _access_token_key,
                      access_token_secret = _access_token_secret)

if __name__ == '__main__':
    _main(sys.argv)

@@ -122,25 +122,8 @@
 er%0A%0A
-MEDIA_PATH = '.'%0A
 SCHE
@@ -169,16 +169,136 @@
 ep)%0A
-SECS = 3
+%0Aclass ConfigFileObj:%0A    def __init__(self):%0A        self.media_path = ''%0A        self.secs = 3%0A        self.twitter_api = None
 %0A%0Ade
@@ -403,31 +403,8 @@
 g()%0A
-    api = read_config()
 %0A   
@@ -420,19 +420,29 @@
 r_media(
-api
+read_config()
 )%0A%0Adef c
@@ -456,19 +456,26 @@
 r_media(
-api
+config_obj
 ):%0A    p
@@ -532,24 +532,46 @@
  %25 (
-MEDIA_PATH, SECS
+config_obj.media_path, config_obj.secs
 ))%0A 
@@ -596,26 +596,37 @@
 listdir(
-MEDIA_PATH
+config_obj.media_path
 ):%0A     
@@ -832,35 +832,57 @@
 th.join(
-MEDIA_PATH, file), 
+config_obj.media_path, file), config_obj.
 api)%0A   
@@ -973,20 +973,31 @@
 D.enter(
-SECS
+config_obj.secs
 , 1, che
@@ -1011,19 +1011,26 @@
 media, (
-api
+config_obj
 , ))%0A   
@@ -1665,56 +1665,18 @@
     
-global MEDIA_PATH%0A    global SECS%0A    MEDIA_PATH
+media_path
  = i
@@ -1700,20 +1700,20 @@
  ')%0A    
-SECS
+secs
  = input
@@ -1835,16 +1835,34 @@
 n_secret
+, media_path, secs
 )%0A%0Adef s
@@ -1940,16 +1940,34 @@
 n_secret
+, media_path, secs
 ):%0A    c
@@ -2313,26 +2313,26 @@
 _path', 
-MEDIA_PATH
+media_path
 )%0A    co
@@ -2356,20 +2356,20 @@
 'secs', 
-SECS
+secs
 )%0A    wi
@@ -2460,24 +2460,57 @@
 d_config():%0A
+    config_obj = ConfigFileObj()%0A
     config =
@@ -2956,64 +2956,19 @@
     
-global MEDIA_PATH%0A        global SECS%0A        MEDIA_PATH
+_media_path
  = c
@@ -3011,12 +3011,13 @@
     
-SECS
+_secs
  = f
@@ -3054,22 +3054,101 @@
 ))%0A%0A    
-return
+config_obj.media_path = _media_path%0A    config_obj.secs = _secs%0A    config_obj.api = 
  twitter
@@ -3367,16 +3367,39 @@
 ecret)%0A%0A
+    return config_obj%0A%0A
 if __nam

remove global vars