Skip to content

Commit

Permalink
new feature: upload a photo from link, release 0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
b3nab committed Feb 20, 2018
1 parent 0e5f117 commit fd20d0a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![MIT license](https://img.shields.io/github/license/b3nab/instapy-cli.svg)](https://github.com/b3nab/instapy-cli/blob/master/LICENSE)

Python library and cli used to upload photo on Instagram. W/o a phone!
You can upload a local file or use a link, it do all automagically.

<p align="center">
<img src="docs/instagram-private-banner.png" alt="instagram-private-api" width="650px">
Expand All @@ -30,7 +31,7 @@ So I dedice to start this repo and OpenSource it w/ :heart:

**Use**

`instapy -f <PATH/TO/IMAGE.jpg> -t "Instagram caption here!" -u <username> -p <password>`
`instapy -u USR -p PSW -f FILE/LINK -t 'TEXT CAPTION'`

**Help**

Expand Down
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
|Instapy-Cli version| |MIT license|

Python library and cli used to upload photo on Instagram. W/o a phone!
You can upload a local file or use a link, it do all automagically.


.. image:: https://github.com/b3nab/instapy-cli/raw/master/docs/instagram-private-banner.png
Expand Down Expand Up @@ -35,7 +36,7 @@ Usage

**Use**

``instapy -f <PATH/TO/IMAGE.jpg> -t "Instagram caption here!" -u <username> -p <password>``
``instapy -u USR -p PSW -f FILE/LINK -t 'TEXT CAPTION'``

**Help**

Expand Down
11 changes: 9 additions & 2 deletions instapy_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
import pkg_resources # part of setuptools
version = pkg_resources.require('instapy_cli')[0].version


'''
TODO:
- use instapy_cli.media to download image link and use it for upload and configure_photo
- rewrite main to support file and links for media
'''
def main(args=None):

welcome_msg = 'instapy-cli'
Expand All @@ -20,7 +26,8 @@ def main(args=None):
parser = OptionParser(usage="usage: %prog [options]")
parser.add_option('-u', dest='username', help='username')
parser.add_option('-p', dest='password', help='password')
parser.add_option('-f', dest='file', help='file path')
parser.add_option('-f', dest='file', help='file path or url')
# parser.add_option('-u', dest='url', help='url to media')
parser.add_option('-t', dest='caption', help='caption text')

(options, args) = parser.parse_args(args)
Expand All @@ -31,7 +38,7 @@ def main(args=None):
import getpass
password = getpass.getpass()
if not options.file:
parser.error('File path is required')
parser.error('File path or url link is required to create a media to upload')

with client(options.username, password) as cli:
text = options.caption or ''
Expand Down
20 changes: 16 additions & 4 deletions instapy_cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from platform import python_version

from instapy_cli.api import InstapySession
from instapy_cli.media import Media


'''
TODO:
- use commented part of code to upload a photo from local image or link
- download media and use the path, then remove the media
- create a new module to handle media files and links
'''
class InstapyCli(object):
def __init__(self, username, password):
self._session = InstapySession()
Expand All @@ -14,6 +20,12 @@ def __enter__(self):
def __exit__(self, exc_type, exc_val, exc_tb):
pass

def upload(self, path, caption=''):
media_id = self._session.upload_photo(path)
self._session.configure_photo(media_id, caption)
def upload(self, file, caption=''):
media = Media(file)
path = media.getPath()

try:
media_id = self._session.upload_photo(path)
self._session.configure_photo(media_id, caption)
finally:
media.removeMedia()
39 changes: 39 additions & 0 deletions instapy_cli/media.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os, requests
#import urlparse for Python2 and Python3
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse


class Media(object):
pathFile = None
media = None
isLink = False

def __init__(self, file):
self.media = file
# if file is link
if urlparse(file).scheme in ('http', 'https'):
self.pathFile = self.downloadMediaFromLink(file)
self.isLink = True
# if file is a local file
else:
self.pathFile = file

def getPath(self):
return self.pathFile

def isLink(self):
return self.isLink

def downloadMediaFromLink(self,url):
# print(urlparse(url))
fileName = urlparse(url).path.split('/')[-1]
print(fileName)
r = requests.get(url, allow_redirects=True)
open(fileName, 'wb').write(r.content)
return fileName

def removeMedia(self):
os.remove(self.pathFile)
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def read(*parts):

setup(
name='instapy-cli',
version='0.0.3',
version='0.0.4',
description='Python library and cli used to upload photo on Instagram. W/o a phone!',
long_description=read('README.rst'),
classifiers=[
Expand Down Expand Up @@ -52,5 +52,4 @@ def read(*parts):
'instapy=instapy_cli.__main__:main'
]
},
# python_requires='>=2.7'
)

0 comments on commit fd20d0a

Please sign in to comment.