From 325ed493aeed4a12a87880c3a9ff5ba4fc19c90b Mon Sep 17 00:00:00 2001 From: muhammadtaqi Date: Sat, 22 Jul 2017 13:23:10 +0500 Subject: [PATCH] init commit --- .gitignore | 86 +------------------ .idea/inspectionProfiles/Project_Default.xml | 64 ++++++++++++++ .../inspectionProfiles/profiles_settings.xml | 7 ++ .idea/vcs.xml | 6 ++ Procfile | 1 + "src/ \ttransferdata.py" | 22 +++++ src/__init__.py | 0 src/main.py | 17 ++++ src/radiorec.py | 59 +++++++++++++ src/transferdata.py | 33 +++++++ 10 files changed, 210 insertions(+), 85 deletions(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/vcs.xml create mode 100644 Procfile create mode 100644 "src/ \ttransferdata.py" create mode 100644 src/__init__.py create mode 100644 src/main.py create mode 100644 src/radiorec.py create mode 100644 src/transferdata.py diff --git a/.gitignore b/.gitignore index 7bbc71c..149830d 100644 --- a/.gitignore +++ b/.gitignore @@ -6,96 +6,12 @@ __pycache__/ # C extensions *.so -# Distribution / packaging -.Python -env/ -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -*.egg-info/ -.installed.cfg -*.egg - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - # Installer logs pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -.hypothesis/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# pyenv -.python-version - -# celery beat schedule file -celerybeat-schedule - -# SageMath parsed files -*.sage.py # dotenv .env # virtualenv -.venv venv/ -ENV/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ +.idea diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..b755c67 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,64 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..3b31283 --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..e2f4168 --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: gunicorn src/main:app --log-file=- diff --git "a/src/ \ttransferdata.py" "b/src/ \ttransferdata.py" new file mode 100644 index 0000000..d5b7849 --- /dev/null +++ "b/src/ \ttransferdata.py" @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +import dropbox + +class TransferData: + def __init__(self, access_token): + self.access_token = access_token + + def upload_file(self, file_from=None, file_to=None): + """upload a file to Dropbox using API v2 + """ + dbx = dropbox.Dropbox(self.access_token) + + with open(file_from, 'rb') as f: + dbx.files_upload(f.read(), file_to) + + #---------------------------------------------------------------------- + def get_account_info(self): + """ + Returns the account information, such as user's display name, + quota, email address, etc + """ + return self.client.account_info() diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..aab22a4 --- /dev/null +++ b/src/main.py @@ -0,0 +1,17 @@ +import os +from radiorec import record_radio +from transferdata import TransferData + + +def main(): + radio_record = record_radio('http://audio.samaafm.com:8008/1') + file_name = radio_record.record_radio() + drop_box_access_token = os.environ.get('access_token') + target_dir = os.environ.get('target_dir') + upload_to = target_dir + os.sep + file_name + transfer_data = TransferData(drop_box_access_token) + transfer_data.get_account_info() + transfer_data.upload_file(file_name, upload_to) + +if __name__ == '__main__': + main() diff --git a/src/radiorec.py b/src/radiorec.py new file mode 100644 index 0000000..c8f93a1 --- /dev/null +++ b/src/radiorec.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +""" +radiorec.py – Recording internet radio streams and Upload to SoundCloud +Copyright (C) 2017 Muhammad Taqi + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. +You should have received a copy of the GNU General Public License +along with this program. If not, see . +""" + +# import libraries +import datetime +import urllib.request + + +class record_radio: + + def __init__(self, stream_url): + self.stream_url = stream_url + + def __enter__(self): + print('__enter__') + + def __exit__(self, typ, value, tb): + print('__exit__') + + def init(self): + print("init") + + def start(self): + print("start") + + def stop(self): + print("stop") + + def finish(self): + print("finish") + + def record_radio(self): + d = datetime.date.today() + filename = "recording" + d.strftime("%Y%m%d")+".ogg" + stream = urllib.request.urlopen(self.stream_url) + start_time = datetime.datetime.now() + dest = open(filename, 'wb') + while (datetime.datetime.now() - start_time).seconds <= 30: + print((datetime.datetime.now() - start_time).seconds) + dest.write(stream.read(1024)) + return filename + + + diff --git a/src/transferdata.py b/src/transferdata.py new file mode 100644 index 0000000..a72212a --- /dev/null +++ b/src/transferdata.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 + +import dropbox + + +class TransferData: + + def __init__(self, access_token): + self.access_token = access_token + + def upload_file(self, file_from=None, file_to=None): + """ + upload a file to Dropbox using API v2 + :param file_from: + :param file_to: + :return: + """ + client = dropbox.Dropbox(self.access_token) + try: + with open(file_from, 'rb') as f: + meta = client.files_upload(f.read(), file_to, mute=True) + print("Uploaded " + file_to) + except: + print("Failed to upload " + file_to) + + def get_account_info(self): + """ + Returns the account information, such as user's display name, quota, + email address, etc + :return: + """ + client = dropbox.Dropbox(self.access_token) + print(client.users_get_current_account())