def reply(): number = request.values.get('From', None)[1:] nmedia = request.values.get('NumMedia', None) mediatype = request.values.get('MediaContentType0',None) if (nmedia > 0) & (mediatype[0:5] == 'image'): url = request.values.get('MediaUrl0', None) print(url) if mediatype[6:] == 'jpeg': extension = '.jpeg' elif mediatype[6:] == 'png': extension = '.png' path = os.path.realpath('../pyconv/images/') + "/" + str(number) + extension print(path) resp = twilio.twiml.Response() image = requests.get(url, stream = True) if image.status_code == 200: with open(path, 'wb') as f: image.raw.decode_content = True copyfileobj(image.raw, f) #sp = subprocess.Popen([os.path.abspath(os.curdir) + "/./conv"]) clean.resizeToSquare(path, os.path.abspath('..') + "/pyconv/images/" + str(number) + extension) ####result = check_output(['lua','-l','dummy', '-e', 'evalPic("%s")' %(path)], cwd = os.path.abspath('..') + "/pyconv") result = cnn.predict(os.path.abspath('..') + "/pyconv/net.pkl", os.path.abspath(os.curdir) + "/images/image.pkl") send_sms.sendMessage(result, "+" + number) return str(resp)
def startWatchingCourse(): while True: coursesdatalist = websoc.requestCourses(COURSECODES) print(coursesdataToString(coursesdatalist)) # ULTIMATE GOAL: compare contents of coursesdatalist with database to see changes # if status change, send sms # for now, just text all OPEN classes openclasses = [] for classtuple in coursesdatalist: if classtuple[-1] != 'FULL': openclasses.append(classtuple) # send_sms.sendMessage(coursesdatalist, verbose=False) # option 1: text entire class list if len(openclasses) > 0: print(openclasses) send_sms.sendMessage(openclasses, verbose=False) # option 2: text open classes only (if there are any) time.sleep(TIME_BETWEEN_REQUESTS) # perform total check every 5 minutes
def sendNotification(request): if request.user.is_authenticated(): # pdb.set_trace() className = str(request.POST.get('class_name')) note = request.POST.get('note') subject = "{cn} is canceled".format(cn=className) msg_body = 'Dear students, our class is canceled.\n\n\n{note}'.format(note=note) recipient = AddClass.objects.filter(class_name=className)[0].students notif = Notification() notif.class_name = className notif.timeStamp = datetime.datetime.now().strftime("%A, %d. %B %Y %I:%M %p") notif.note=note notif.save() for rec in recipient.all(): send_email(subject, msg_body, str(rec.email)) sendMessage(className, rec.number) # return render(request, 'notify.html', {"notifyMsg":"Messages sent successfully."}) return render(request, 'addclass.html',{'user':request.user, 'returnlist': AddClass.objects.all(), 'studentDetailsForm':StudentDetails(), 'notifyForm':SendNotification(), "notifyMessage":"Messages sent successfully"})
def check(flist, fout, ticker='GOOGL', min_price=710.0, max_price=805.0, stock_exchange='NYSE', history_stock_info=None, isPreMarket=False, map_for_rsi={}): price = (max_price + min_price) / 2.0 googl = None if flist == None: fout.write('List of stocks is empty!!!! \n') fout.flush() return for a in flist: if a['StockSymbol'] == ticker: googl = a break if googl == None or not googl: print 'error broker ticker: ', ticker sys.stdout.flush() return False price = 0.0 day_start_price = 0.0 div = -1.0 #print ticker try: price = float(googl['LastTradeWithCurrency']) day_start_price = -1.0 if len(googl['PreviousClosePrice'].strip()) > 0.0: day_start_price = float(googl['PreviousClosePrice']) if 'Dividend' in googl and len(googl['Dividend'].strip()) > 0.0: div = float(googl['Dividend']) except ValueError: #print 'CRASH: ',googl['LastTradeWithCurrency'] #print 'CRASH:',googl['PreviousClosePrice'] #sys.stdout.flush() if googl['LastTradeWithCurrency'].strip() == '': return False price = float( googl['LastTradeWithCurrency'].strip('CHF').strip('$').replace( '€', '').strip('GBX').replace(',', '')) day_start_price = float(googl['PreviousClosePrice'].strip('CHF').strip( '$').strip('GBX').replace('€', '').replace(',', '')) if 'Dividend' in googl and len(googl['Dividend'].strip()) > 0.0: div = float( googl['Dividend'].strip('CHF').strip('$').strip('GBX').replace( '€', '').replace(',', '')) if isPreMarket: a, b, c = fetchPreMarket(fout, ticker, stock_exchange) price = c if c < 0.0: return False percent_change = 0.0 if day_start_price > 0.0: #\033[1;31mbold red text\033[0m\n #\033[1;32mbold green text\033[0m\n percent_change = 100.0 * (price - day_start_price) / day_start_price line = 'Price for ' if percent_change > 0.0: line += '\033[1;32m ' + ticker + ' \033[0m is: %0.2f and change is \033[1;32m %0.2f \033[0m' % ( price, percent_change) else: line += '\033[1;31m ' + ticker + ' \033[0m is: %0.2f and change is \033[1;31m %0.2f \033[0m' % ( price, percent_change) #line='Price for '+ticker+' is: %0.2f ' %price #,' at time: ',time.localtime() if history_stock_info != None: hist = None for a in history_stock_info: if a['StockSymbol'] == ticker: hist = a break if hist != None: old_price = float(hist['LastTradeWithCurrency'].strip('GBX').strip( 'CHF').strip('$').strip(',').replace(',', '')) old_price_perct_change = 0.0 if old_price > 0.0: old_price_perct_change = 100.0 * (price - old_price) / old_price if old_price_perct_change > 0.0: line += '. First followed at %0.2f. Change of \033[1;32m +%0.2f \033[0m.' % ( old_price, old_price_perct_change) else: line += '. First followed at %0.2f. Change of \033[1;31m %0.2f \033[0m.' % ( old_price, old_price_perct_change) # check the position relative to the MA if ticker in map_for_rsi: map_for_rsi[ticker].AddDividend(div, day_start_price) map_for_rsi[ticker].percent_change = percent_change line += map_for_rsi[ticker].AddMAString(price) fout.write(line + '\n') # check the position relative to RSI if ticker in map_for_rsi: fout.write(map_for_rsi[ticker].Decision(price)) if not (price < max_price and price > min_price): fout.write('Found what we were looking for....\n') print googl message = 'Stock: ' + ticker + ' is at %0.2f. ' % price if price < min_price: message += 'This is below threshold of %0.2f.' % min_price message += ' Recommend to BUY stock.' if price > max_price: message += 'This is above threshold of %0.2f.' % max_price message += ' Recommend to Sell stock.' if ticker in LIST_OF_MESSAGES: if (LIST_OF_MESSAGES[ticker] * 0.98) > price and price < min_price: # resend if change is larger than 2 % in decrease from last message LIST_OF_MESSAGES[ticker] = price elif (LIST_OF_MESSAGES[ticker] * 1.02) < price and price > max_price: # resend if change is larger than 2 % in decrease from last message LIST_OF_MESSAGES[ticker] = price else: # otherwise no new message should be sent return else: LIST_OF_MESSAGES[ticker] = price if SENDMESSAGE: send_sms.sendMessage(message) #c1 = ROOT.TCanvas("c1","testbeam efficiency",50,50,600,600); #c1.Draw() #c1.Update() #c1.WaitPrimitive() return price
def check(googl, fout, ticker='GOOGL',min_price=710.0, max_price=805.0, history_stock=None): price =(max_price+min_price)/2.0 try: googl.refresh() price =-1.0 day_start_price=-1.0 try: price = float(googl.get_price().strip('CHF').strip('GBX')) day_start_price = float(googl.get_prev_close().strip('CHF').strip('GBX')) except: print 'ticker...did not get price: ',ticker print 'price: ',googl.get_price() print 'googl.get_prev_close(): ',googl.get_prev_close() percent_change = 0.0 if day_start_price>0.0: #\033[1;31mbold red text\033[0m\n #\033[1;32mbold green text\033[0m\n percent_change=100.0*(price-day_start_price)/day_start_price line = 'Price for ' if percent_change>0.0: line+='\033[1;32m '+ticker+' \033[0m is: %0.2f and change is \033[1;32m %0.2f \033[0m' %(price,percent_change) else: line+='\033[1;31m '+ticker+' \033[0m is: %0.2f and change is \033[1;31m %0.2f \033[0m' %(price,percent_change) # change in recent history if history_stock!=None: chg = 0.0 if history_stock.get_price()>0.0: chg = 100.0*(price-float(history_stock.get_price()))/float(history_stock.get_price()) line+='. Was originally %0.2f. Change: %0.2f' %(float(history_stock.get_price()), chg) fout.write(line+'\n'); except (urllib2.HTTPError,AttributeError,yahoo_finance.YQLQueryError,urllib2.URLError): fout.write( 'Failed this round!!!! for '+ticker+' \n') fout.flush() time.sleep(5.0) return False if not (price < max_price and price>min_price): fout.write( 'Found what we were looking for....\n' ) print googl message = 'yahoo finance. Prices delayed by 15 min. Stock: '+ticker+' is at %0.2f. ' %price if price<min_price: message+='This is below threshold of %0.2f.' %min_price message+=' Recommend to BUY stock.' if price>max_price: message+='This is above threshold of %0.2f.' %max_price message+=' Recommend to Sell stock.' if ticker in LIST_OF_MESSAGES: if (LIST_OF_MESSAGES[ticker]*0.98)>price and price<min_price: # resend if change is larger than 2 % in decrease from last message LIST_OF_MESSAGES[ticker] = price elif (LIST_OF_MESSAGES[ticker]*1.02)<price and price>max_price: # resend if change is larger than 2 % in decrease from last message LIST_OF_MESSAGES[ticker] = price else: # otherwise no new message should be sent return else: LIST_OF_MESSAGES[ticker] = price if price>0.0: if SENDMESSAGE: send_sms.sendMessage(message)
def touched_face(): IoT_influxdb.on_message(1) send_sms.sendMessage()